我正在用 C# 制作 tic tac toe

问题描述 投票:0回答:1
try
{
    Console.Write("\nPlayer {0}: Choose your field:", player);
    input = Convert.ToInt32(Console.ReadLine());
    if (input > 9 || input < 1)
    {
        Console.WriteLine("Number is invalid!");
    }
    else
    {
        charInput = Convert.ToChar(input);
        foreach (char c in PlayField)
        {
            if (c == charInput)
            {
                Console.WriteLine(c);
                inputCorrect = false;
                Console.ReadKey();
            }
        }
    }
}

我已将这部分代码放在 do while 循环中,条件为 (inputCorrect)。

此代码的目标是通过确保输入在边界内并且在 tic tac toe 板内(在 2D 数组中为 1 到 9,例如列中的 1,2,3)来验证输入、4、5、6 等等。

每次我按照应有的方式运行代码时,它都会运行 foreach 循环,同时不会对 if 条件做出反应,即使输入是正确的。

没有显示错误,它只是运行 foreach 循环并返回到 try 块中的第一个 C.W。

使用系统; 使用 System.Numerics;

命名空间 TicTacToe__working_ { 内部课程计划 {

    static void Main()
    {
        int player = 1; // Player 1 starts
        int input = 0;

        char[,] PlayField =
    {
        {'1', '2', '3'},
        {'4', '5', '6'},
        {'7', '8', '9'}
    };




        // Run code as long as the program runs
        do
        {

            SetPlayField(PlayField);

            input = ValidateInput(player, input, PlayField);

            if (player == 1)
            {
                EnterXorO(player, input, PlayField);
                player = 2;
            }
            else
            {
                EnterXorO(player, input, PlayField);
                player = 1;
            }




        } while (true);



    }
    public static int ValidateInput(int player, int input, char[,] PlayField)
    {
        bool inputCorrect = true;
        char charInput = '1';

        do
        {

            try
            {
                Console.Write("\nPlayer {0}: Choose your field:", player);
                input = Convert.ToInt32(Console.ReadLine());
                if (input > 9 || input < 1)
                {
                    Console.WriteLine("Number is invalid!");
                }
                else
                {

                    charInput = Convert.ToChar(input);
                    foreach (char c in PlayField)
                    {
                        Console.WriteLine(charInput);
                        Console.WriteLine(c);
                        if (c == charInput)
                        {
                            Console.WriteLine(c);
                            inputCorrect = false;
                            Console.ReadKey();
                        }
                    }
                }
            }
            catch
            {
                Console.WriteLine("Please enter a valid number!");
            }

        } while (inputCorrect);

        return input;
    }
    
    
    
    public static void SetPlayField(char[,] PlayField)
    {
        Console.Clear();
        Console.WriteLine("     |     |");
        Console.WriteLine("  {0}  |  {1}  |  {2}  ", PlayField[0,0], PlayField[0,1], PlayField[0,2]);
        Console.WriteLine("_____|_____|_____");
        Console.WriteLine("     |     |");

        Console.WriteLine("  {0}  |  {1}  |  {2}  ", PlayField[1,0], PlayField[1, 1], PlayField[1,2]);
        Console.WriteLine("_____|_____|_____");
        Console.WriteLine("     |     |");

        Console.WriteLine("  {0}  |  {1}  |  {2}  ", PlayField[2, 0], PlayField[2, 1], PlayField[2, 2]);
        Console.WriteLine("     |     |");
    }
    public static void EnterXorO(int player, int input, char[,] PlayField)
    {
        char playerSign;

        if (player == 1)
            playerSign = 'X';
        else
            playerSign = 'O';

        switch (input)
        {
            case 1: PlayField[0, 0] = playerSign; break;
            case 2: PlayField[0, 1] = playerSign; break;
            case 3: PlayField[0, 2] = playerSign; break;
            case 4: PlayField[1, 0] = playerSign; break;
            case 5: PlayField[1, 1] = playerSign; break;
            case 6: PlayField[1, 2] = playerSign; break;
            case 7: PlayField[2, 0] = playerSign; break;
            case 8: PlayField[2, 1] = playerSign; break;
            case 9: PlayField[2, 2] = playerSign; break;

        }
    }
}

}

我现在已经发布了完整的代码。我知道可能存在错误,或者只是更快、更有效的方法来修复它,但是我只想了解我对前面的代码块做了什么以及如何修复它。

谢谢!!

c# if-statement try-catch
1个回答
0
投票

这可能听起来很愚蠢,但如果 char input 等于 PlayField 的 1 个条目,那么 inputCorrect 不应该为 true 吗?如果 Play 字段包含从 1 到 9 的数字,我觉得整个 else 部分是多余的。您正在测试两次 t@ 查看输入是否在 1 到 9 之间...

如果我弄错了并且它不是多余的,根据您使用的库,您的字符转换可能会改变 ASCII 数字。开始调试确实可能是个好主意

© www.soinside.com 2019 - 2024. All rights reserved.