C# Tictatcoe游戏出现故障,无法将字符串添加到右侧网格,有时代码会自动播放

C# Tictatcoe游戏出现故障,无法将字符串添加到右侧网格,有时代码会自动播放,c#,C#,我做了一个Tictatcoe游戏,用一个函数来简化代码,我的函数playx和playo似乎工作不太正常,然后我尝试编辑我的代码,但它也工作不正常,我只是无法在正确的网格中编写代码,有时它会在两个网格中编写第二个代码 using System; namespace tictactoe { class MainClass { static string[,] game = new string[3, 3]; public static void M

我做了一个Tictatcoe游戏,用一个函数来简化代码,我的函数playx和playo似乎工作不太正常,然后我尝试编辑我的代码,但它也工作不正常,我只是无法在正确的网格中编写代码,有时它会在两个网格中编写第二个代码

using System;

namespace tictactoe
{
    class MainClass
    {
        static string[,] game = new string[3, 3];

        public static void Main(string[] args)
        {

            Console.WriteLine("Welcome to tictactoe,please play enter to play ");

            Console.Clear();
            int n = 0;

            while (n < 9)
            {
                print();
                playx();


                if (1 == checkifwon())
                {
                    Console.WriteLine("player y won");
                }
                else if (2 == checkifwon())
                {
                    Console.WriteLine("player x won");
                }
                print();
                playo();

                if (1 == checkifwon())
                {
                    Console.WriteLine("player y won");
                }
                else if (2 == checkifwon())
                {
                    Console.WriteLine("player x won");
                }
                print();

            }

            Console.ReadKey();

        }

        static void print()
        {
            Console.WriteLine(game[0, 0] + "|" + game[0, 1] + "|" + game[0, 2]);

            Console.WriteLine(game[0, 0] + "|" + game[1, 1] + "|" + game[2, 2]);

            Console.WriteLine(game[2, 0] + "|" + game[2, 1] + "|" + game[2, 2]);

        }

        static int checkifwon()
        {


            if (lineWin("o", game[0, 0], game[0, 1], game[0, 2]) ||
             lineWin("o", game[1, 0], game[1, 1], game[1, 2]) ||
             lineWin("o", game[2, 0], game[2, 1], game[2, 2]) ||
             lineWin("o", game[0, 0], game[0, 1], game[0, 2]) ||
             lineWin("o", game[1, 0], game[1, 1], game[1, 2]) ||
             lineWin("o", game[2, 0], game[2, 1], game[2, 2]) ||
             lineWin("o", game[0, 0], game[1, 1], game[2, 2]) ||
             lineWin("o", game[2, 0], game[1, 1], game[0, 2])
            )
            {
                return 1;
            }
            else if (lineWin("x", game[0, 0], game[0, 1], game[0, 2]) ||
             lineWin("x", game[1, 0], game[1, 1], game[1, 2]) ||
             lineWin("x", game[2, 0], game[2, 1], game[2, 2]) ||
             lineWin("x", game[0, 0], game[0, 1], game[0, 2]) ||
             lineWin("x", game[1, 0], game[1, 1], game[1, 2]) ||
             lineWin("x", game[2, 0], game[2, 1], game[2, 2]) ||
             lineWin("x", game[0, 0], game[1, 1], game[2, 2]) ||
             lineWin("x", game[2, 0], game[1, 1], game[0, 2]))
            {
                return 2;
            }
            else
            {
                return 3;
            }

        }

        static bool lineWin(string player, string first, string second, string third)
        {
            if (first == player && second == player && third == player)
                return true;
            else
                return false;
        }
        static void Cheat()
        {
            if (game[0, 0] == "o" && game[0, 1] == "o" && game[0, 2] == "o" ||
            game[0, 0] == "o" && game[0, 1] == "o" && game[0, 2] == "o")
            {

            }

        }

        static void playx()
        {
            Console.WriteLine("enter the x coordinate player x");
            int a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("enter the y coordinate");
            int b = Convert.ToInt32(Console.ReadLine());
            game[a, b] = "x";
        }
        static void playo()
        {
            Console.WriteLine("enter the x coordinate player o");
            int q = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("enter the y coordinate");
            int c = Convert.ToInt32(Console.ReadLine());
            game[q, c] = "o";
        }
    }
}
第一代码

using System;

namespace tictactoe
{
    class MainClass
    {
        static string[,] game = new string[3, 3];

        public static void Main(string[] args)
        {

            Console.WriteLine("Welcome to tictactoe; please enter players name?");
            string player1 = Console.ReadLine();
            Console.WriteLine(" please enter player 2 name?");
            string player2 = Console.ReadLine();


            Console.Clear();
            int n = 0;

            while (n < 9)
            {
                print();
                playx();
                print();

                if (3 == checkifwon())
                {
                    playo();
                }
                else if (2 == checkifwon())
                {
                    Console.WriteLine("player x won");
                }
                else
                {
                    Console.WriteLine("player y won");
                }

                print();

            }

            Console.ReadKey();

        }

        static void print()
        {
            Console.WriteLine(game[0, 0] + "|" + game[0, 1] + "|" + game[0, 2]);

            Console.WriteLine(game[0, 0] + "|" + game[1, 1] + "|" + game[2, 2]);

            Console.WriteLine(game[2, 0] + "|" + game[2, 1] + "|" + game[2, 2]);

        }

        static int checkifwon()
        {


            if (lineWin("o", game[0, 0], game[0, 1], game[0, 2]) ||
             lineWin("o", game[1, 0], game[1, 1], game[1, 2]) ||
             lineWin("o", game[2, 0], game[2, 1], game[2, 2]) ||
             lineWin("o", game[0, 0], game[0, 1], game[0, 2]) ||
             lineWin("o", game[1, 0], game[1, 1], game[1, 2]) ||
             lineWin("o", game[2, 0], game[2, 1], game[2, 2]) ||
             lineWin("o", game[0, 0], game[1, 1], game[2, 2]) ||
             lineWin("o", game[2, 0], game[1, 1], game[0, 2])
            )
            {
                return 1;
            }
            else if (lineWin("x", game[0, 0], game[0, 1], game[0, 2]) ||
             lineWin("x", game[1, 0], game[1, 1], game[1, 2]) ||
             lineWin("x", game[2, 0], game[2, 1], game[2, 2]) ||
             lineWin("x", game[0, 0], game[0, 1], game[0, 2]) ||
             lineWin("x", game[1, 0], game[1, 1], game[1, 2]) ||
             lineWin("x", game[2, 0], game[2, 1], game[2, 2]) ||
             lineWin("x", game[0, 0], game[1, 1], game[2, 2]) ||
             lineWin("x", game[2, 0], game[1, 1], game[0, 2]))
            {
                return 2;
            }
            else
            {
                return 3;
            }
        }

        static bool lineWin(string player, string first, string second, string third)
        {
            if (first == player && second == player && third == player)
                return true;
            else
                return false;
        }
        static void Cheat()
        {
            if (game[0, 0] == "o" && game[0, 1] == "o" && game[0, 2] == "o" ||
            game[0, 0] == "o" && game[0, 1] == "o" && game[0, 2] == "o")
            {

            }

        }

        static void playx()
        {
            Console.WriteLine("enter the x coordinate player x");
            int a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("enter the y coordinate");
            int b = Convert.ToInt32(Console.ReadLine());
            if (game[a, b] == "x")
            {
                Console.WriteLine("already played that grid");
            }
            else if (game[a, b] == "o")
            {
                Console.WriteLine("sorry,can not  play that grid");
            }
            else
            {
                game[a, b] = "x";

            }

        }
        static void playo()
        {
            Console.WriteLine("enter the x coordinate player x");
            int a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("enter the y coordinate");
            int b = Convert.ToInt32(Console.ReadLine());

            if (game[a, b] == "o")
            {
                Console.WriteLine("already played that grid");
            }
            else if (game[a, b] == "x")
            {
                Console.WriteLine("sorry,can not  play that grid");
            }
            else
            {
                game[a, b] = "o";

            }
        }
    }
}
如果你看你的照片,你会发现你在第二行游戏[1,0]的地方重复游戏[0,0]。你在第二行重复游戏[2,2],也在游戏[1,2]的地方

您的函数应该如下所示:

static void print()
{
    Console.WriteLine(game[0, 0] + "|" + game[0, 1] + "|" + game[0, 2]);

    Console.WriteLine(game[1, 0] + "|" + game[1, 1] + "|" + game[1, 2]);

    Console.WriteLine(game[2, 0] + "|" + game[2, 1] + "|" + game[2, 2]);

}

您认为看到的错误是因为打印值不正确。这就是为什么你说有时它会在两个网格中写入,而当你在游戏[0,0]和游戏[2,2]中放置一个值时,有时会在这两个网格中写入。

使用调试器并逐步完成代码。这告诉你什么?