我正在尝试获取显示井字棋脚板的代码块并将其转换为方法。事实证明,这非常困难,尽管我觉得自己要遵守规则,但是我什么也做不了。我需要帮助弄清楚如何使代码协同工作(例如,从顶部的用户输入中检入胜出或平局。
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();
}
答案 0 :(得分:0)
我认为您要问的是关于方法参数和返回类型。
public int Add(int x, int y)
{
return x + y;
}
在该示例中,Add是方法名称,int是方法的返回类型,int x和int 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