public void Play() { // set starting player to the player who is Os // this.currentPlayer = (this.players[0].Token == Piece.PieceValue.O) ? (byte)0 : (byte)1; foreach (Player p in this.players) { this.currentPlayer = p; if (p.Token == Piece.PieceValue.O) { break; } } // draw the empty board this.DrawBoard(); Console.WriteLine(); // player this game until there is a winner while (!this.Over) { // get a reference to the current player this.currentPlayer = (Player)players.Current; // player selects a move ushort move = this.currentPlayer.Move(this.theBoard); // mark move on the board this.theBoard.Place(new Piece(this.currentPlayer.Token), move); // draw the board on the screen Console.WriteLine("Round {0}: {1} chooses square {2}.", this.theBoard.Moves, this.currentPlayer.Token.ToString(), move); this.DrawBoard(); Console.WriteLine(); // check to see if the most recent player won if (this.Over = theBoard.Win(this.currentPlayer.Token)) { this.currentPlayer.AddPoints(1); this.PrintWinner(); } // check to see if the game is a draw else if (this.Over = theBoard.Full()) { this.PrintDraw(); } // otherwise, move to next player else { players.MoveNext(); } } }
protected static bool ImmediateBlock(Piece inPiece, TicTacToeBoard inBoard, ref ushort bestMove) { Piece p = (inPiece.Value == Piece.PieceValue.X) ? new Piece(Piece.PieceValue.X) : new Piece(Piece.PieceValue.O); for (ushort i = 0; i < inBoard.Length; ++i) { // create a clone of the current board TicTacToeBoard tempBoard = new TicTacToeBoard(inBoard); tempBoard.Place(p, i); if (tempBoard.Win(p.Value)) { bestMove = i; return(true); } } return(false); }
protected static bool ImmediateWin(Piece inPiece, TicTacToeBoard inBoard, ref ushort bestMove) { for (ushort i = 0; i < inBoard.Length; ++i) { // create a clone of the current board TicTacToeBoard tempBoard = new TicTacToeBoard(inBoard); // place a piece on each sqaure in the new board tempBoard.Place(inPiece, i); // and see if it results in an immediate win if (tempBoard.Win(inPiece.Value)) { bestMove = i; return(true); } } return(false); }