예제 #1
0
        /// <summary>
        /// Check if there is a winner
        /// </summary>
        /// <param name="board">current game board</param>
        /// <returns>bool</returns>
        public bool CheckForWinner(Board board)
        {
            int[][] winners = new int[][]
            {
                new[] { 1, 2, 3 },
                new[] { 4, 5, 6 },
                new[] { 7, 8, 9 },
                new[] { 1, 4, 7 },
                new[] { 2, 5, 8 },
                new[] { 3, 6, 9 },
                new[] { 1, 5, 9 },
                new[] { 3, 5, 7 }
            };

            // Confirm winner found & congratulate the winner
            for (int i = 0; i < winners.Length; i++)
            {
                Position p1 = Player.NumberPositions(winners[i][0]);
                Position p2 = Player.NumberPositions(winners[i][1]);
                Position p3 = Player.NumberPositions(winners[i][2]);
                string   a  = Board.GameBoard[p1.Row, p1.Column];
                string   b  = Board.GameBoard[p2.Row, p2.Column];
                string   c  = Board.GameBoard[p3.Row, p3.Column];

                if (a == "X" && b == "X" && c == "X")
                {
                    Console.WriteLine("Congrats, Player One!");
                    Console.Write("You win!");
                    return(true);
                }
                else if (a == "O" && b == "O" && c == "O")
                {
                    Console.WriteLine("Congrats, Player Two!");
                    Console.Write("You win!");
                    return(true);
                }
            }
            return(false);
        }