C# 将数组转换为方法时出现问题

C# 将数组转换为方法时出现问题,c#,list,methods,C#,List,Methods,我正在尝试获取一段显示tic-tac趾板的代码,并将其转换为方法。这被证明是非常困难的,尽管我觉得我在遵守规则,但我什么都做不到。我需要帮助了解如何使代码协同工作(例如,从顶部的用户输入中签入win或tie框) static void() { int computerSquare = 0; int userSquare = 0; bool playerTurn = true; int whoFirst = 0;

我正在尝试获取一段显示tic-tac趾板的代码,并将其转换为方法。这被证明是非常困难的,尽管我觉得我在遵守规则,但我什么都做不到。我需要帮助了解如何使代码协同工作(例如,从顶部的用户输入中签入win或tie框)

static void()
      {
        int computerSquare = 0;
        int userSquare = 0;
        bool playerTurn = true;
        int whoFirst = 0;
        string winCheck = null;
        string[,] theBoard = new string[3, 3]
        {   { " ", " ", " "},
                { " ", " ", " "},
                { " ", " ", " "}   };
        int placesFilled = 0;


        Random myRandomGenerator = new Random();  //Initializing the random generator

        //Explain the game to the player
        Console.WriteLine("Welcome to Tic-Tac-Toe.  You will be X and the computer will be O.\n");
        Console.WriteLine("On your turn please indicate which square you want to select by entering" +
            " the number as shown below.\n");
        Console.WriteLine(" 1 | 2 | 3");
        Console.WriteLine("---|---|---");
        Console.WriteLine(" 4 | 5 | 6");
        Console.WriteLine("---|---|---");
        Console.WriteLine(" 7 | 8 | 9");
        Console.WriteLine("\nPlease enter any key when you are ready to begin playing. ");
        Console.ReadKey();

        //figure who goes first here, set firstTurn appropriately
        whoFirst = myRandomGenerator.Next(0, 2);
        if (whoFirst == 0)
        {
            playerTurn = false;
            Console.WriteLine("The computer will go first");
            Console.ReadKey();
        }

        //Display the blank board
        Console.Clear();

        Console.WriteLine($" {theBoard[0, 0]} | {theBoard[0, 1]} | {theBoard[0, 2]}");
        Console.WriteLine("---|---|---");
        Console.WriteLine($" {theBoard[1, 0]} | {theBoard[1, 1]} | {theBoard[1, 2]}");
        Console.WriteLine("---|---|---");
        Console.WriteLine($" {theBoard[2, 0]} | {theBoard[2, 1]} | {theBoard[2, 2]}");

    }

    static void CheckForSquare()
    {
        int computerSquare = 0;
        int userSquare = 0;
        bool playerTurn = true;
        int whoFirst = 0;
        string winCheck = null;
        string[,] theBoard = new string[3, 3]
        {   { " ", " ", " "},
                { " ", " ", " "},
                { " ", " ", " "}   };
        int placesFilled = 0; 

        if (playerTurn)
        {
            Console.Write($"\nWhere would you like to place your X? ");

            try
            {
                userSquare = Convert.ToInt32(Console.ReadLine());
            }
            catch (FormatException)
            {
                Console.Clear();
                Console.Write("You must enter an integer. Press any key to continue: ");
                Console.ReadKey();
                Console.Clear();
            }
            catch (OverflowException)
            {
                Console.Clear();
                Console.Write("That value is too large. Press any key to continue: ");
                Console.ReadKey();
                Console.Clear();
            }


            if (userSquare < 4)
            {
                if (theBoard[0, userSquare - 1] != " ")
                {
                    Console.WriteLine("That square is occupied.  Try again.");
                    continue;
                }
                theBoard[0, userSquare - 1] = "X";
                placesFilled++;
            }
            else if (userSquare < 7)
            {
                if (theBoard[1, userSquare - 4] != " ")
                {
                    Console.WriteLine("That square is occupied.  Try again.");
                    continue;
                }
                theBoard[1, userSquare - 4] = "X";
                placesFilled++;
            }
            else if (userSquare < 10)
            {
                if (theBoard[2, userSquare - 7] != " ")
                {
                    Console.WriteLine("That square is occupied.  Try again.");
                    continue;
                }
                theBoard[2, userSquare - 7] = "X";
                placesFilled++;
            }
            else
            {
                Console.WriteLine("You must select a value from 1 - 9");
            }
            playerTurn = false;
            winCheck = "X";
        }
        else
        {
            computerSquare = myRandomGenerator.Next(1, 10);
            //Console.WriteLine(computerSquare);

            if (computerSquare < 4)
            {
                if (theBoard[0, computerSquare - 1] != " ")
                {

                    continue;
                }
                theBoard[0, computerSquare - 1] = "O";
                placesFilled++;
                //break;
            }
            else if (computerSquare < 7)
            {
                if (theBoard[1, computerSquare - 4] != " ")
                {

                    continue;
                }
                theBoard[1, computerSquare - 4] = "O";
                placesFilled++;
                //break;
            }
            else
            {
                if (theBoard[2, computerSquare - 7] != " ")
                {

                    continue;
                }
                theBoard[2, computerSquare - 7] = "O";
                placesFilled++;
                //break;
            }
            playerTurn = true;
            winCheck = "O";
        }
    }

}

static void PrintBoard()
{
    string[,] theBoard = new string[3, 3]
       {   { " ", " ", " "},
                { " ", " ", " "},
                { " ", " ", " "}   };

    Console.Clear();
    Console.WriteLine($" {theBoard[0, 0]} | {theBoard[0, 1]} | {theBoard[0, 2]}");
    Console.WriteLine("---|---|---");
    Console.WriteLine($" {theBoard[1, 0]} | {theBoard[1, 1]} | {theBoard[1, 2]}");
    Console.WriteLine("---|---|---");
    Console.WriteLine($" {theBoard[2, 0]} | {theBoard[2, 1]} | {theBoard[2, 2]}");
}

static void CheckForWinTie()
{
    int computerSquare = 0;
    int userSquare = 0;
    bool playerTurn = true;
    int whoFirst = 0;
    string winCheck = null;
    string[,] theBoard = new string[3, 3]
    {   { " ", " ", " "},
                { " ", " ", " "},
                { " ", " ", " "}   };
    int placesFilled = 0;

    if (theBoard[0, 0] == winCheck && theBoard[0, 1] == winCheck && theBoard[0, 2] == winCheck)
    {
        if (winCheck == "X")
        {
            Console.WriteLine("\nYou have won");
            Console.ReadKey();

        }
        else
        {
            Console.WriteLine("\nThe computer has won");
            Console.ReadKey();

        }
    }

    if (theBoard[1, 0] == winCheck && theBoard[1, 1] == winCheck && theBoard[1, 2] == winCheck)
    {
        if (winCheck == "X")
        {
            Console.WriteLine("\nYou have won");
            Console.ReadKey();

        }
        else
        {
            Console.WriteLine("\nThe computer has won");
            Console.ReadKey();

        }

    }
    if (theBoard[2, 0] == winCheck && theBoard[2, 1] == winCheck && theBoard[2, 2] == winCheck)
    {
        if (winCheck == "X")
        {
            Console.WriteLine("\nYou have won");
            Console.ReadKey();

        }
        else
        {
            Console.WriteLine("\nThe computer has won");
            Console.ReadKey();

        }

    }


    if (theBoard[0, 0] == winCheck && theBoard[1, 0] == winCheck && theBoard[2, 0] == winCheck)
    {
        if (winCheck == "X")
        {
            Console.WriteLine("\nYou have won");
            Console.ReadKey();

        }
        else
        {
            Console.WriteLine("\nThe computer has won");
            Console.ReadKey();

        }
    }
    if (theBoard[0, 1] == winCheck && theBoard[1, 1] == winCheck && theBoard[2, 1] == winCheck)
    {
        if (winCheck == "X")
        {
            Console.WriteLine("\nYou have won");
            Console.ReadKey();

        }
        else
        {
            Console.WriteLine("\nThe computer has won");
            Console.ReadKey();

        }


    }
    if (theBoard[0, 2] == winCheck && theBoard[1, 2] == winCheck && theBoard[2, 2] == winCheck)
    {
        if (winCheck == "X")
        {
            Console.WriteLine("\nYou have won");
            Console.ReadKey();

        }
        else
        {
            Console.WriteLine("\nThe computer has won");
            Console.ReadKey();

        }
    }

    if (theBoard[0, 0] == winCheck && theBoard[1, 1] == winCheck && theBoard[2, 2] == winCheck)
    {
        if (winCheck == "X")
        {
            Console.WriteLine("\nYou have won");
            Console.ReadKey();

        }
        else
        {
            Console.WriteLine("\nThe computer has won");
            Console.ReadKey();

        }


    }

    if (theBoard[0, 2] == winCheck && theBoard[1, 1] == winCheck && theBoard[0, 0] == winCheck)
    {
        if (winCheck == "X")
        {
            Console.WriteLine("\nYou have won");
            Console.ReadKey();

        }
        else
        {
            Console.WriteLine("\nThe computer has won");
            Console.ReadKey();
        }


    }

    if (placesFilled == 9)
    {
        Console.WriteLine("\nIt's a tie");
        Console.ReadKey();

    }
}

Console.Write("\n(Y) to play again: ");
    string playAgain = Console.ReadLine();
    if (playAgain != "Y" && playAgain != "y")
    {
        break;
    }
Console.Clear();
staticvoid()
{
int computerSquare=0;
int userSquare=0;
bool playerTurn=true;
int whoirst=0;
字符串winCheck=null;
string[,]theBoard=新字符串[3,3]
{   { " ", " ", " "},
{ " ", " ", " "},
{ " ", " ", " "}   };
int placesFilled=0;
Random myRandomGenerator=new Random();//初始化随机生成器
//向玩家解释游戏
Console.WriteLine(“欢迎来到Tic-Tac-Toe。您将是X,而计算机将是O”。\n”);
Console.WriteLine(“轮到你时,请通过输入指示要选择的方块”+
“数字如下所示。\n”);
Console.WriteLine(“1 | 2 | 3”);
Console.WriteLine(“----------------------”号);
Console.WriteLine(“4 | 5 | 6”);
Console.WriteLine(“----------------------”号);
Console.WriteLine(“7 | 8 | 9”);
Console.WriteLine(“\n请在准备开始播放时输入任意键。”);
Console.ReadKey();
//计算谁先到这里,适当地设置firstTurn
whoFirst=myRandomGenerator.Next(0,2);
如果(whoFirst==0)
{
playerTurn=false;
Console.WriteLine(“计算机将先走”);
Console.ReadKey();
}
/显示空白板
Console.Clear();
Console.WriteLine($“{theBoard[0,0]}{theBoard[0,1]}}{theBoard[0,2]}”);
Console.WriteLine(“----------------------”号);
Console.WriteLine($“{theBoard[1,0]}{theBoard[1,1]}}{theBoard[1,2]}”);
Console.WriteLine(“----------------------”号);
Console.WriteLine($“{theBoard[2,0]}{theBoard[2,1]}}{theBoard[2,2]}”);
}
静态void CheckForSquare()
{
int computerSquare=0;
int userSquare=0;
bool playerTurn=true;
int whoirst=0;
字符串winCheck=null;
string[,]theBoard=新字符串[3,3]
{   { " ", " ", " "},
{ " ", " ", " "},
{ " ", " ", " "}   };
int placesFilled=0;
如果(播放翻转)
{
控制台。写入($“\n要将X放置在何处?”);
尝试
{
userSquare=Convert.ToInt32(Console.ReadLine());
}
捕获(格式化异常)
{
Console.Clear();
Write(“您必须输入一个整数。按任意键继续:”);
Console.ReadKey();
Console.Clear();
}
捕获(溢出例外)
{
Console.Clear();
写入(“该值太大。按任意键继续:”);
Console.ReadKey();
Console.Clear();
}
if(userSquare<4)
{
如果(主板[0,userSquare-1]!=“”)
{
控制台。WriteLine(“该方块已被占用。请重试。”);
继续;
}
主板[0,userSquare-1]=“X”;
placesFilled++;
}
else if(userSquare<7)
{
如果(主板[1,userSquare-4]!=“”)
{
控制台。WriteLine(“该方块已被占用。请重试。”);
继续;
}
电路板[1,userSquare-4]=“X”;
placesFilled++;
}
否则如果(userSquare<10)
{
如果(theBoard[2,userSquare-7]!=“”)
{
控制台。WriteLine(“该方块已被占用。请重试。”);
继续;
}
theBoard[2,userSquare-7]=“X”;
placesFilled++;
}
其他的
{
WriteLine(“必须从1-9中选择一个值”);
}
playerTurn=false;
winCheck=“X”;
}
其他的
{
computerSquare=myRandomGenerator.Next(1,10);
//控制台。WriteLine(computerSquare);
if(computerSquare<4)
{
如果(电路板[0,computerSquare-1]!=“”)
{
继续;
}
电路板[0,computerSquare-1]=“O”;
placesFilled++;
//中断;
}
否则如果(computerSquare<7)
{
如果(电路板[1,computerSquare-4]!=“”)
{
继续;
}
电路板[1,computerSquare-4]=“O”;
placesFilled++;
//中断;
}
其他的
{
如果(电路板[2,computerSquare-7]!=“”)
{
继续;
}
电路板[2,computerSquare-7]=“O”;
placesFilled++;
//中断;
}
playerTurn=true;
winCheck=“O”;
}
}
}
静态无效打印板()
{
string[,]theBoard=新字符串[3,3]
{   { " ", " ", " "},
{ " ", " ", " "},
{ " ", " ", " "}   };
Console.Clear();
Console.WriteLine($“{theBoard[0,0]}{theBoard[0,1]}}{theBoard[0,2]}”);
Console.WriteLine(“----------------------”号);
Console.WriteLine($“{theBoard[1,0]}{theBoard[1,1]}}{theBoard[1,2]}”);
控制台写入线(“---|--
public int Add(int x, int y)
{
   return x + y;
}
public int GetUserAnswer()
{
   Console.Write("Enter a number");
   if(int.TryParse(Console.ReadLine(), out int number))
     return number;
   return -1;
}
public void Foo()
{
   int userNumber = GetUserAnswer();
   int userNumber2 = GetUserAnswer();
   int sum = Add(userNumber, userNumber2);
   //do more stuff
}
public int HandleUserSqaureLessThan4(string[,] theBoard, int userSquare, int PlacesFilled)
{
   if (theBoard[0, userSquare - 1] != " ")
            {
                Console.WriteLine("That square is occupied.  Try again.");
                continue;
            }
            theBoard[0, userSquare - 1] = "X";
            return placesFilled++;
}
if (userSquare < 4)
{
    HandleUserSqaureLessThan4(theBoard, userSquare, placesFilled);
}
... //the rest of the code