Naughts and Crosses(Tic Tac Toe),子程序查询

时间:2016-12-14 18:15:14

标签: c# console-application subroutine

所以我在我的Noughts and Crosses项目的最后阶段并且我非常困难,我已经完成了移动验证子程序以及一个完全基于改变空白空间的子程序。将盒子变成" X"或者" O", 但我的代码似乎告诉我,我的代码的某些部分在当前上下文中不存在而且我完全感到困惑

代码是:

namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {
        string[,] grid = new string[3, 3] {{" "," "," "},
                                           {" "," "," "},
                                           {" "," "," "}};
        string board = System.IO.File.ReadAllText("E:\\BoardGame.txt");
        Console.WriteLine(board);
        int player = 0;
        var XCoordinate = 0;
        var YCoordinate = 0;
        int x, y;
        GetMoveCoordinates(ref XCoordinate, ref YCoordinate);
        if (player == 0)
        {                
            grid[XCoordinate, YCoordinate] = " X ";
            player++;
        }
        else
        {
            grid[XCoordinate, YCoordinate] = " O ";
            player--;
        }            
        UpdateGrid(grid, box);
        if (player == 1)
        {
        }

    }


    public static void GetMoveCoordinates(ref int XCoordinate, ref int YCoordinate)
    {
        int CommaLocation;
        bool GameHasBeenWon = false;
        string CoordinatesInput;
        string XChar, YChar;
        while (GameHasBeenWon == false)
        {
            try
            {
                Console.Write("Enter your coordinates: (x,y) ");
                CoordinatesInput = Console.ReadLine();
                CommaLocation = CoordinatesInput.IndexOf(",".ToString());
                XChar = CoordinatesInput.Substring(CommaLocation - 1, CommaLocation);
                YChar = CoordinatesInput.Substring(CommaLocation + 1);
                XCoordinate = int.Parse(XChar);
                YCoordinate = int.Parse(YChar);
            }
            catch
            {
                Console.WriteLine("Invalid Input- Please Try Again");
            }
        }
    }


    public static bool CheckValidMove(int XCoordinate, int YCoordinate, string[,] Board)
    {
        if ((XCoordinate >= 1) || (XCoordinate <= 3))
        {
            if ((YCoordinate >= 1) || (YCoordinate <= 3))
            {
                if ((Board[XCoordinate, YCoordinate]) == " ")
                {
                    return true;
                }
                else return false;
            }
            else return false;
        }
        else return false;
    }


    public static void UpdateGrid(string[,] grid, string box)
    {
        Console.Clear();
        for (int x = 0; x < grid.GetLength(0); x++)
        {
            for (int y = 0; y < grid.GetLength(1); y++)
            {
                box = box.Replace((x + 1) + "," + (y + 1), grid[y, x]);
            }
        }
        // In the case not required as clearning the console default the cursor back to 0,0, but left in 
        // as an example
        Console.SetCursorPosition(0, 0);
        Console.WriteLine(box);
    }
}
}

然而我似乎遇到的问题是在Main下,在if语句下,代码似乎告诉我更新(网格,框)中的框在当前上下文中不存在,但它应该在最后一个子程序?我应该这样做作为参考声明还是我错过了什么?此外,如果您有任何关于如何整理代码的提示,我很乐意欣赏它(是的,我会添加win参数,但我想首先绘制我的符号)。

这就是网格的样子:

 +---+---+---+
 |   |   |   |
 +---+---+---+
 |   |   |   |
 +---+---+---+
 |   |   |   |
 +---+---+---+

1 个答案:

答案 0 :(得分:0)

这里有几个错误。首先,以下不会编译:

UpdateGrid(grid, box);
正如Andrew Whitaker在评论中指出的那样,没有&#34;框&#34; main方法中的变量(您从未声明或初始化它)。定义该变量是什么并正确初始化它并再次编译。

接下来,有关以下内容的快速文体说明:

while (GameHasBeenWon == false)

不要明确地比较真与假 - 正确的方法是

while (!GameHasBeenWon)

评论的下一行有几个错误:

(XCoordinate >= 1) || (XCoordinate <= 3)

这意味着XCoordinate&gt; = 1或者它小于或等于3,这根本不是你的意思。实际上,通过这个逻辑,任何整数都是有效的,因为它大于1,等于1,小于3或等于3.(想想看 - 对于整数可能是什么这样的陈述可能是假的?)另外,3具体是有效索引,但是0。请记住,数组是零索引的。因此,这应该是

(XCoordinate >= 0) && (XCoordinate < 3)

就你的if语句而言:

if ((Board[XCoordinate, YCoordinate]) == " ")
            {
                return true;
            }
            else return false;

true为真时返回(Board[XCoordinate, YCoordinate]) == " ",在该语句为假时完全返回false。你可以做到

return (Board[XCoordinate, YCoordinate]) == " ";

事实上,你可以为整个&#34;如果&#34;声明。 (我现在没有坐在IDE的前面,所以如果我的语法在这里不完美,我会道歉。)

return ((XCoordinate >= 0) && (XCoordinate < 3) &&
        ((YCoordinate >= 0) && (YCoordinate < 3)) &&
        ((Board[XCoordinate, YCoordinate]) == " "));