コード例 #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("********************");
            Console.WriteLine("* Tic-Tac-Toe Game *");
            Console.WriteLine("********************");
            var game = new TicTacToe();

            Console.WriteLine("The cell numbers for the game are shown below.");

            Play(game);
        }
コード例 #2
0
        static void Play(TicTacToe game)
        {
            string playAgain   = "y";
            bool   sessionDone = false;

            do
            {
                game.Draw();
                for (int i = 0; i < arr.Length; i++)
                {
                    arr[i] = ' ';
                }
                do

                {
                    if (player % 2 == 0)//checking the chance of the player

                    {
                        Console.Write("Enter cell number (1-9) for player O: ");
                    }

                    else

                    {
                        Console.Write("Enter cell number (1-9) for player X: ");
                    }



                    choice = int.Parse(Console.ReadLine());//Taking users choice


                    // checking that position where user want to run is marked (with X or O) or not

                    if (arr[choice] != 'X' && arr[choice] != 'O')

                    {
                        if (player % 2 == 0) //if chance is of player 2 then mark O else mark X

                        {
                            arr[choice] = 'O';

                            player++;
                        }

                        else

                        {
                            arr[choice] = 'X';

                            player++;
                        }
                    }

                    else //If there is any possition where user want to run and that is already marked then show message and load board again

                    {
                        Console.WriteLine("Sorry the row {0} is already marked with {1}", choice, arr[choice]);

                        Console.WriteLine("\n");
                    }
                    Board();
                    flag = CheckWin();             // calling of check win
                } while (flag != 1 && flag != -1); // This loof will be run until all cell of the grid is not marked with X and O or some player is not win


                if (flag == 1)// if flag value is 1 then some one has win or means who played marked last time which has win

                {
                    Console.WriteLine("Player {0} wins!", (arr[choice]));
                }

                else// if flag value is -1 the match will be draw and no one is winner

                {
                    Console.WriteLine("It's a Draw!");
                }

                do
                {
                    Console.Write("Do you want to guess another word? Enter y or n > ");
                    playAgain = Console.ReadLine();
                    Console.WriteLine("");
                    if ((playAgain == "y") || (playAgain == "n"))
                    {
                        sessionDone = true;
                    }
                    else
                    {
                        Console.WriteLine($"\"{playAgain}\" is not a valid choice. Try Again.");
                        sessionDone = false;
                    }
                }while (sessionDone != true);
            }while (playAgain != "n");
            Console.WriteLine("Good-bye and thanks for playing my Tic-Tac-Toe game.");
        }