예제 #1
0
        public static void PlayTicTacToe()
        {
            TicOrTac[,] tictacArray = new TicOrTac[3, 3];
            //Initialize the array values to be Blank-  0
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    tictacArray[i, j] = TicOrTac.Empty;
                }
            }
            // tictacArray[0,0] = TicOrTac.Empty;
            bool winner = false;

            int[]       userInputIndex = new int[2];
            TicOrTac    userInput;
            WinnerState winnerState;

            PrintTicTac(tictacArray);

            int  k         = 0;
            bool userState = true;

            while (!winner)
            {
                userInputIndex = SelectRandomIndex();
                userInput      = userState ? TicOrTac.X : TicOrTac.O;

                Console.WriteLine("{2} selected index values : ({0}, {1}) and the Input is:{3}", userInputIndex[0], userInputIndex[1], userState? "User 1" : "User 2", (TicOrTac)userInput);
                tictacArray[userInputIndex[0], userInputIndex[1]] = userInput;
                PrintTicTac(tictacArray);
                // winnerState = CheckTicTacWinner(tictacArray); //or use this method
                if (userState)
                {
                    winnerState = CheckWinnerX(userInputIndex[0], userInputIndex[1]);
                }
                else
                {
                    winnerState = CheckWinnerO(userInputIndex[0], userInputIndex[1]);
                }
                if (winnerState != WinnerState.None)
                {
                    winner = true;
                    Console.WriteLine($"{winnerState} has won the game!!");
                }
                if (k == 8 && !winner)
                {
                    Console.WriteLine("No one has won the game");
                    break;
                }
                k++;
                userState = !userState;
            }
        }
예제 #2
0
 public static TicOrTac Toggle(TicOrTac TicTac)
 {
     return TicTac == TicOrTac.Tic ? TicOrTac.Tac : TicOrTac.Tic;
 }