Ejemplo n.º 1
0
        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();
                }
            }
        }
Ejemplo n.º 2
0
        protected static MoveStrategy LookAhead(Piece inPiece, TicTacToeBoard inBoard, ref ushort bestMove)
        {
            // unused dummy value to seend the algorithm
            ushort aDummy = 0;
            // temporary strategy
            MoveStrategy aResponse;
            // the strategy to return
            MoveStrategy outValue = (inPiece.Value == Piece.PieceValue.X) ? MoveStrategy.OWin - 1 : MoveStrategy.XWin + 1;

            // first check to see if the game is a draw
            if (inBoard.Full())
            {
                outValue = MoveStrategy.Draw;
            }
            // return any move that results in an immediate win
            else if (Computer.ImmediateWin(inPiece, inBoard, ref bestMove))
            {
                outValue = (inPiece.Value == Piece.PieceValue.X) ? MoveStrategy.XWin : MoveStrategy.OWin;
            }
            // return any move that will result in blocking the other player from an immediate win on his next turn
            else if (Computer.ImmediateBlock(inPiece, inBoard, ref bestMove))
            {
                outValue = (inPiece.Value == Piece.PieceValue.X) ? MoveStrategy.XBlock : MoveStrategy.OBlock;
            }
            // no immediate wins so look for a promising move
            else
            {
                for (ushort i = 0; i < inBoard.Length; ++i)
                {
                    if (!inBoard.At(i))
                    {
                        inBoard.Place(inPiece, i);

                        // swap pieces and go down a move
                        Piece aPiece = (inPiece.Value == Piece.PieceValue.X)  ? new Piece(Piece.PieceValue.O) : new Piece(Piece.PieceValue.X);

                        // call look ahead recursively with a copy of the board each time
                        aResponse = LookAhead(aPiece, new TicTacToeBoard(inBoard), ref aDummy);

                        // determine the best move
                        switch (inPiece.Value)
                        {
                        case Piece.PieceValue.X:
                            if (aResponse > outValue)
                            {
                                outValue = aResponse;
                                bestMove = i;
                            }
                            break;

                        case Piece.PieceValue.O:
                            if (aResponse < outValue)
                            {
                                outValue = aResponse;
                                bestMove = i;
                            }
                            break;

                        default:
                            break;
                        } // end switch
                    }     // end if
                }         // end for loop
            }             // end else

            // return the best move strategy
            return(outValue);
        }