简体   繁体   中英

C# how to restart the console application

I am a begginer, and Im trying to make a Tic-Tac-Toe console game.But when O player has won, it shows, only when X player has made a decision.So that, there can be a situation when 2 players have won.I tried to use if statement to stop aplication but it doesn't help. I think I don't need to restart the application, just start Main again. But how??

class Program
{
    static void Main()
    {
        string Player1;
        string Player2;
        int turnPlayer1;
        int turnPlayer2;
        while (CheckTheWinner() == false)
        {
            BoardPlay();
            Console.Clear();
                BoardPlay();
            for ( turnPlayer1 = 0; turnPlayer1 < 1; turnPlayer1++)
            {
                Console.Write("Player O: Choose your field:");
                Player1 = Console.ReadLine();
                Player1Choice(Player1);
            }
            CheckTheWinner();
            Console.Clear();
            BoardPlay();
            for (int i = 0; i < 1; i++)
            {
                Console.Write("Player X: Choose your field:");
                Player2 = Console.ReadLine();
                Player2choice(Player2);
            }
            CheckTheWinner();
            Console.Clear();
            BoardPlay();
        }
    }



    public static void Player1Choice(string P1)
    {
        string o = "O";
        for (int i = 0; i < position.GetLength(0); i++)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            for (int j = 0; j <= 2; j++)
            {
                if (position[i, j] == P1)
                {
                    position[i, j] = o;  
                }
            }
        };
    }



    public static void Player2choice(string P2)
    {
        string x = "X";
        for (int i = 0; i < position.GetLength(0); i++)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            for (int j = 0; j <= 2; j++)
            {
                if (position[i, j] == P2)
                {
                    position[i, j] = x;
                }

            }
        };
    }

    static bool CheckTheWinner()
    {
        for (int i = 0; i < 3; i++)
        {
            if (position[i, 0] == position[i, 1] && position[i, 1] == position[i, 2])
            {
                return true;
            }
            if (position[0, i] == position[1, i] && position[1, i] == position[2, i])
            {
                return true;
            }
        }
        if (position[0, 0] == position[1, 1] && position[1, 1] == position[2, 2])
        {
            return true;
        }
        if (position[0, 2] == position[1, 1] && position[1, 1] == position[2, 0])
        {
            return true;
        }
        return false;
    }

"Starting a new game" is basically "repeating the operation of having a game". Use loops to repeat operations.

First, define the loop condition. For example, a boolean flag indicating to keep playing:

var keepPlaying = true;

Then loop as long as that flag is true:

var keepPlaying = true;
while (keepPlaying)
{
    // all of the logic currently in your Main method
}

Then all you need to do is prompt the player if they want another game and update the variable accordingly. For example:

var keepPlaying = true;
while (keepPlaying)
{
    // all of the logic currently in your Main method

    Console.Write("Would you like to play again? (Y/N): ");
    var playerReply = Console.ReadLine();
    if (playerReply == "N")
        keepPlaying = false;
}

You could perhaps simplify the logic a bit, this is mainly for demonstration. You can also add some logic to check the input, you can add a closing message if the player chooses to quit, add other options, etc.

But the point is that if you want to repeat the game, it's not a matter of restarting the application or re-invoking the Main method, it's just a matter of encapsulating what you want to repeat in a loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM