"Programming WPF" Translation 5th Chapter 5. Data templates and styles

Source: Internet
Author: User

Let's imagine a more entertaining version of what we want to achieve TTT (this is the most important feature of most games). For example, a variant of TTT allows a player to occupy only 3 squares at a time, removing the first step in the fourth step, removing the second step in the next fifth step, and so on. To implement this variant, we need to keep track of each step sequentially-we can take advantage of the Playmover class, as in example 5-20.

Example 5-20

namespace TicTacToe {
  public class PlayerMove {
    private string playerName;
    public string PlayerName {
      get { return playerName; }
      set { playerName = value; }
    }

    private int moveNumber;
    public int MoveNumber {
      get { return moveNumber; }
      set { moveNumber = value; }
    }

    public PlayerMove(string playerName, int moveNumber) {
      this.playerName = playerName;
      this.moveNumber = moveNumber;
    }
  }
}

Now, instead of using a simple string for the content of each button object, we will show such a change in a playmover instance in example 5-20.

Example 5-21

namespace TicTacToe {
  public partial class Window1 : Window {

    int moveNumber;

    void NewGame( ) {

      this.moveNumber = 0;
    }

    void cell_Click(object sender, RoutedEventArgs e) {

      // Set button content
      //button.Content = this.CurrentPlayer;
      button.Content =
        new PlayerMove(this.CurrentPlayer, ++this.moveNumber);

    }

  }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.