Ejemplo n.º 1
0
        /// <summary>
        /// Checks if there is a win for the player with the corresponding token.
        /// If there is a win it passes the token back,
        /// passes a empty token if there isn't a win.
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        private TicTacToeToken TryFinalizePlayer(TicTacToeToken token)
        {
            if (HorizontalWin(token) || VerticalWin(token) || LeftDiagonalWin(token) || RightDiagonalWin(token))
            {
                return(token);
            }

            return(TicTacToeToken.Empty);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Return true if there is a right diagonal win for the corresponding token
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        private bool RightDiagonalWin(TicTacToeToken token)
        {
            if (TicTacToeBoard[2] == token && TicTacToeBoard[4] == token && TicTacToeBoard[6] == token)
            {
                TicTacToeWinPositions[0] = 2;
                TicTacToeWinPositions[1] = 4;
                TicTacToeWinPositions[2] = 6;
                return(true);
            }

            return(false);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Return true if there is a left diagonal win for the corresponding token
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        private bool LeftDiagonalWin(TicTacToeToken token)
        {
            if (TicTacToeBoard[0] == token && TicTacToeBoard[4] == token && TicTacToeBoard[8] == token)
            {
                TicTacToeWinPositions[0] = 0;
                TicTacToeWinPositions[1] = 4;
                TicTacToeWinPositions[2] = 8;
                return(true);
            }

            return(false);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// The default tic tac toe game constructor
        /// The first turn is for the X token.
        /// </summary>
        public TicTacToeGame()
        {
            CurrentTicTacToePlayer = TicTacToeToken.XToken;
            TicTacToeBoard         = new TicTacToeToken[9];
            TicTacToeWinPositions  = new int[3];

            ResetBoard();

            Player1Wins = 0;
            Player2Wins = 0;
            Ties        = 0;

            Paused = false;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Return true if there is a vertical win for the corresponding token
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        private bool VerticalWin(TicTacToeToken token)
        {
            for (int column = 0; column < 3; column++)
            {
                if (TicTacToeBoard[column] == token && TicTacToeBoard[column + 3] == token && TicTacToeBoard[column + 6] == token)
                {
                    TicTacToeWinPositions[0] = column;
                    TicTacToeWinPositions[1] = column + 3;
                    TicTacToeWinPositions[2] = column + 6;
                    return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Return true if there is a horizontal win for the corresponding token
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        private bool HorizontalWin(TicTacToeToken token)
        {
            for (int row = 0; row < 7; row += 3)
            {
                if (TicTacToeBoard[row] == token && TicTacToeBoard[row + 1] == token && TicTacToeBoard[row + 2] == token)
                {
                    TicTacToeWinPositions[0] = row;
                    TicTacToeWinPositions[1] = row + 1;
                    TicTacToeWinPositions[2] = row + 2;
                    return(true);
                }
            }

            return(false);
        }