Esempio n. 1
0
        /// <summary>
        /// Makes a move on this Tic-Tac-Toe board at the specified move position
        /// </summary>
        /// <param name="move">The move to make</param>
        /// <returns>A reference to this Tic-Tac-Toe board</returns>
        public override Board MakeMove(Move move)
        {
            TTTMove m = (TTTMove)move;

            //Make the move if possible, if a move has already been made at this position then throw an invalid move exception
            if (boardContents[m.X, m.Y] == 0)
            {
                //Make the move on this board
                boardContents[m.X, m.Y] = CurrentPlayer;

                //Remove the move just performed from the list of possible moves
                possibleMoves.Remove(m);

                //Determine if there is a winner
                DetermineWinner(move);

                //Swap out the current player
                CurrentPlayer = NextPlayer;

                //Call the MakeMove method on the Board base class, to save the last move made on this board
                return(base.MakeMove(move));
            }
            else
            {
                throw new InvalidMoveException("Move has already been made at: " + m.ToString());
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Determine if the current game is over <para/>
        /// Uses knowledge of the last move to save computation time
        /// </summary>
        /// <param name="move">The last move made</param>
        protected override void DetermineWinner(Move move)
        {
            TTTMove m = (TTTMove)move;

            //Check the row and column of the last move
            if (boardContents[0, m.Y] == boardContents[1, m.Y] && boardContents[1, m.Y] == boardContents[2, m.Y] || boardContents[m.X, 0] == boardContents[m.X, 1] && boardContents[m.X, 1] == boardContents[m.X, 2])
            {
                winner = CurrentPlayer;
                return;
            }

            //Then check each diagonal
            if (boardContents[0, 0] != 0 && boardContents[0, 0] == boardContents[1, 1] && boardContents[0, 0] == boardContents[2, 2] || boardContents[0, 2] != 0 && boardContents[0, 2] == boardContents[1, 1] && boardContents[0, 2] == boardContents[2, 0])
            {
                winner = CurrentPlayer;
                return;
            }

            //If there is still no winner and the board is full, the game is a tie, otherwise no winner yet
            if (PossibleMoves().Count == 0)
            {
                winner = 0;
            }
        }