更改 int 变量值时遇到问题(Tic Tac Toe)
Having trouble changing int variable value (Tic Tac Toe)
这是 playerTurn 方法。
public static void playerTurn(int player)
{
if (player == 1)
{
player = 2;
}
else if(player == 2) { player = 1; }
}
为什么调用playerTurn方法后播放器没有变化。我尝试更改玩家 1 的初始值并弄乱 if else 语句,但它没有用。我仍然是 C# 的新手,所以我可能在其他地方做错了,但我不确定还能尝试什么。
static void Main(string[] args) //MAIN METHOD
{
int player = 1;
int inputNum;
int turns = 0;
Console.WriteLine("Player1: X");
Console.WriteLine("Player2: O");
Console.WriteLine("\n");
SetField();
openMessage(player);
string input = Console.ReadLine();
if (int.TryParse(input, out inputNum))
{
inputNum = int.Parse(input);
turns++;
}
else
{
Console.WriteLine("Please enter a valid input!");
}
switch (player)
{
case 1:switch (inputNum)
{
case 1: playField[0, 0] = playerSign(player); break;
case 2: playField[0, 1] = playerSign(player); break;
case 3: playField[0, 2] = playerSign(player); break;
case 4: playField[1, 0] = playerSign(player); break;
case 5: playField[1, 1] = playerSign(player); break;
case 6: playField[1, 2] = playerSign(player); break;
case 7: playField[2, 0] = playerSign(player); break;
case 8: playField[2, 1] = playerSign(player); break;
case 9: playField[2, 2] = playerSign(player); break;
}
break;
case 2:
switch (inputNum)
{
case 1: playField[0, 0] = playerSign(player); break;
case 2: playField[0, 1] = playerSign(player); break;
case 3: playField[0, 2] = playerSign(player); break;
case 4: playField[1, 0] = playerSign(player); break;
case 5: playField[1, 1] = playerSign(player); break;
case 6: playField[1, 2] = playerSign(player); break;
case 7: playField[2, 0] = playerSign(player); break;
case 8: playField[2, 1] = playerSign(player); break;
case 9: playField[2, 2] = playerSign(player); break;
}
break;
}
SetField();
Console.WriteLine(player);
playerTurn(player);
//player 1 turn end
Console.WriteLine(player);
openMessage(player);
Console.ReadKey();
}
参数按值传递给此方法。因此它只获得值的副本。 Return 新值。此外,您可以简化 playerTurn 方法:
public static int playerTurn(int player)
{
return 3 - player; // Switches player from 1 to 2 or vice-versa.
}
3 - 2 = 1
。 3 - 1 = 2
。
用
调用它
player = playerTurn(player);
您可以通过一些数学运算以更简单的方式获得索引:
playField[(inputNum - 1) / 3, (inputNum - 1) % 3] = playerSign(player);
/
执行整数除法,将结果截断为下一个较小的整数。 %
产生该除法的余数。
并且没有必要打开 player
,因为这两种情况是相同的。这允许您用一行代码替换 31 行代码。
另请注意,您的游戏没有循环。因此,它会在第一个玩家移动后停止。
这个方法:
public static void playerTurn(int player)
{
if (player == 1)
{
player = 2;
}
else if(player == 2) { player = 1; }
}
复制传入的任何内容并修改副本。您需要做的是通过引用传递值:
public static void playerTurn(ref int player)
...
这样传递给 playerTurn 方法的任何变量都会被直接修改。
这是 playerTurn 方法。
public static void playerTurn(int player)
{
if (player == 1)
{
player = 2;
}
else if(player == 2) { player = 1; }
}
为什么调用playerTurn方法后播放器没有变化。我尝试更改玩家 1 的初始值并弄乱 if else 语句,但它没有用。我仍然是 C# 的新手,所以我可能在其他地方做错了,但我不确定还能尝试什么。
static void Main(string[] args) //MAIN METHOD
{
int player = 1;
int inputNum;
int turns = 0;
Console.WriteLine("Player1: X");
Console.WriteLine("Player2: O");
Console.WriteLine("\n");
SetField();
openMessage(player);
string input = Console.ReadLine();
if (int.TryParse(input, out inputNum))
{
inputNum = int.Parse(input);
turns++;
}
else
{
Console.WriteLine("Please enter a valid input!");
}
switch (player)
{
case 1:switch (inputNum)
{
case 1: playField[0, 0] = playerSign(player); break;
case 2: playField[0, 1] = playerSign(player); break;
case 3: playField[0, 2] = playerSign(player); break;
case 4: playField[1, 0] = playerSign(player); break;
case 5: playField[1, 1] = playerSign(player); break;
case 6: playField[1, 2] = playerSign(player); break;
case 7: playField[2, 0] = playerSign(player); break;
case 8: playField[2, 1] = playerSign(player); break;
case 9: playField[2, 2] = playerSign(player); break;
}
break;
case 2:
switch (inputNum)
{
case 1: playField[0, 0] = playerSign(player); break;
case 2: playField[0, 1] = playerSign(player); break;
case 3: playField[0, 2] = playerSign(player); break;
case 4: playField[1, 0] = playerSign(player); break;
case 5: playField[1, 1] = playerSign(player); break;
case 6: playField[1, 2] = playerSign(player); break;
case 7: playField[2, 0] = playerSign(player); break;
case 8: playField[2, 1] = playerSign(player); break;
case 9: playField[2, 2] = playerSign(player); break;
}
break;
}
SetField();
Console.WriteLine(player);
playerTurn(player);
//player 1 turn end
Console.WriteLine(player);
openMessage(player);
Console.ReadKey();
}
参数按值传递给此方法。因此它只获得值的副本。 Return 新值。此外,您可以简化 playerTurn 方法:
public static int playerTurn(int player)
{
return 3 - player; // Switches player from 1 to 2 or vice-versa.
}
3 - 2 = 1
。 3 - 1 = 2
。
用
player = playerTurn(player);
您可以通过一些数学运算以更简单的方式获得索引:
playField[(inputNum - 1) / 3, (inputNum - 1) % 3] = playerSign(player);
/
执行整数除法,将结果截断为下一个较小的整数。 %
产生该除法的余数。
并且没有必要打开 player
,因为这两种情况是相同的。这允许您用一行代码替换 31 行代码。
另请注意,您的游戏没有循环。因此,它会在第一个玩家移动后停止。
这个方法:
public static void playerTurn(int player)
{
if (player == 1)
{
player = 2;
}
else if(player == 2) { player = 1; }
}
复制传入的任何内容并修改副本。您需要做的是通过引用传递值:
public static void playerTurn(ref int player)
...
这样传递给 playerTurn 方法的任何变量都会被直接修改。