Example #1
0
        private bool IsSelectedPlayerWinner(Players player)
        {
            string playerKey = player.ToString("G");

            for (int cellIndex = 0; cellIndex < 9; cellIndex += 3)
            {
                if (BoardCells[cellIndex] == playerKey && BoardCells[cellIndex + 1] == playerKey && BoardCells[cellIndex + 2] == playerKey)
                {
                    return(true);
                }
            }
            for (int cellIndex = 0; cellIndex < 3; cellIndex++)
            {
                if (BoardCells[cellIndex] == playerKey && BoardCells[cellIndex + 3] == playerKey && BoardCells[cellIndex + 6] == playerKey)
                {
                    return(true);
                }
            }
            if (BoardCells[0] == playerKey && BoardCells[4] == playerKey && BoardCells[8] == playerKey)
            {
                return(true);
            }
            if (BoardCells[2] == playerKey && BoardCells[4] == playerKey && BoardCells[6] == playerKey)
            {
                return(true);
            }
            return(false);
        }
Example #2
0
        private bool IsPlayerAWinner(Players currentPlayer)
        {
            bool   isAWinner  = false;
            string playerMark = currentPlayer.ToString();

            // Diagonal
            isAWinner = isAWinner || GameBoard.Is3InARow(playerMark, 0, 4, 8);
            isAWinner = isAWinner || GameBoard.Is3InARow(playerMark, 2, 4, 6);
            // Rows
            isAWinner = isAWinner || GameBoard.Is3InARow(playerMark, 0, 1, 2);
            isAWinner = isAWinner || GameBoard.Is3InARow(playerMark, 3, 4, 5);
            isAWinner = isAWinner || GameBoard.Is3InARow(playerMark, 6, 7, 8);
            // Cols
            isAWinner = isAWinner || GameBoard.Is3InARow(playerMark, 0, 3, 6);
            isAWinner = isAWinner || GameBoard.Is3InARow(playerMark, 1, 4, 7);
            isAWinner = isAWinner || GameBoard.Is3InARow(playerMark, 2, 5, 8);

            return(isAWinner);
        }