英文:
Value of 2D Array not being change by switch statement
问题
我尝试编写一个井字棋游戏,但是某种原因我的switch
函数似乎没有运行。我做错了什么?
#include <iostream>
using namespace std;
int n = 0;
int Player[2] {1, 2};
int moveChosen=0;
int n1 = 0, n2 = 0;
void Display(int grid[][3], int row, int column)
{
cout << "Coordinates: \n";
cout << "1 | 2 | 3\n";
cout << "--|---|--\n";
cout << "4 | 5 | 6\n";
cout << "--|---|--\n";
cout << "7 | 8 | 9\n";
cout << "Current Moves: \n";
cout << grid[0][0] << " | " << grid[0][1] << " | " << grid[0][2] << "\n";
cout << "--|---|--\n";
cout << grid[1][0] << " | " << grid[1][1] << " | " << grid[1][2] << "\n";
cout << "--|---|--\n";
cout << grid[2][0] << " | " << grid[2][1] << " | " << grid[2][2] << "\n";
}
int moveChoice(int Choice)
{
cout << "Player " << Player[n] << " please pick where to move. ";
cin >> Choice;
if (Choice < 1 || Choice > 9)
{
while (Choice < 1 || Choice > 9)
{
cout << "Invalid choice, please enter a new number: ";
cin >> Choice;
}
}
return Choice;
}
void CheckMoveValid(int grid[][3], int Choice)
{
switch (Choice)
{
case 1:
if(grid[0][0] != 0)
{
cout << "Invalid move, space already filled. ";
}
else
{
moveChosen = Player[n];
n1 = 0;
n2 = 0;
break;
}
break;
case 2:
if(grid[0][1] != 0)
{
cout << "Invalid move, space already filled. ";
}
else
{
moveChosen = Player[n];
n1 = 0;
n2 = 1;
break;
}
}
}
int main()
{
int grid[3][3]={{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
int Choice, CheckWin = 0;
while (CheckWin < 1)
{
Display(grid, 3, 3);
moveChoice(Choice);
CheckMoveValid(grid, Choice);
if (n == 0) // 请注意,这里应该使用双等号来比较,而不是赋值。
{
n = 1;
}
else
{
n = 0;
}
grid[n1][n2] = Player[n];
}
return 0;
}
在你的代码中,有一个小错误,即在if (n=0)
中应该使用双等号if (n == 0)
来比较n
的值。此外,你的switch
语句似乎只包括了两种情况(case 1
和case 2
),你需要继续添加其他情况以处理不同的选择。如果在第一个case
中没有触发"Invalid move, space already filled.",那可能是因为grid[0][0]
的值一直为0。
英文:
I am trying to code a game of Tic-Tac-Toe, but for some reason my switch
function doesn't appear to be running. What am I doing wrong?
#include <iostream>
using namespace std;
int n = 0;
int Player[2] {1, 2};
int moveChosen=0;
int n1 = 0, n2 = 0;
void Display(int grid[][3], int row, int column)
{
cout<<"Coordinates: \n";
cout<<"1"<<" | "<<"2"<<" | "<<"3"<<"\n";
cout<<"--|---|--\n";
cout<<"4"<<" | "<<"5"<<" | "<<"6"<<"\n";
cout<<"--|---|--\n";
cout<<"7"<<" | "<<"8"<<" | "<<"9"<<"\n";
cout<<"Current Moves: \n";
cout<<grid[0][0]<<" | "<<grid[0][1]<<" | "<<grid[0][2]<<"\n";
cout<<"--|---|--\n";
cout<<grid[1][0]<<" | "<<grid[1][1]<<" | "<<grid[1][2]<<"\n";
cout<<"--|---|--\n";
cout<<grid[2][0]<<" | "<<grid[2][1]<<" | "<<grid[2][2]<<"\n";
}
int moveChoice(int Choice)
{
cout<<"Player "<<Player[n]<<" please pick where to move. ";
cin>>Choice;
if (Choice < 1 || Choice > 9)
{
while (Choice < 1 || Choice > 9)
{
cout << "Invalid choice, please enter a new number: ";
cin >> Choice;
}
}
return Choice;
}
void CheckMoveValid(int grid[][3], int Choice)
{
switch (Choice)
{
case 1:
if(grid[0][0] != 0)
{
cout<<"Invalid move, space already filled. ";
}
else
{
moveChosen = Player[n];
n1 = 0;
n2 = 0;
break;
}
break;
case 2:
if(grid[0][1] != 0)
{
cout<<"Invalid move, space already filled. ";
}
else
{
moveChosen = Player[n];
n1 = 0;
n2 = 1;
break;
}
}
}
int main()
{
int grid[3][3]={{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
int Choice, CheckWin = 0;
while (CheckWin < 1)
{
Display(grid, 3, 3);
moveChoice(Choice);
CheckMoveValid(grid, Choice);
if (n=0)
{
n = 1;
}
else
{
n = 0;
}
grid[n1][n2] = Player[n];
}
return 0;
}
If I put cout << "0" << endl;
between line 65 and 66, nothing happens even when I enter 1
, and whatever I tried I could not get "Invalid move, space already filled. "
to trigger.
答案1
得分: 1
在main()
函数中,对moveChoice()
的调用提示用户输入,但然后丢弃了该输入。main()
函数中的Choice
变量从未被赋值,因此对CheckMoveValid()
的调用会表现出未定义行为。
删除moveChoice()
中的Choice
参数,它是不必要的。让main()
函数将moveChoice()
的返回值分配给Choice
变量,例如:
int moveChoice()
{
int Choice;
...
cin >> Choice;
...
return Choice;
}
...
int main()
{
...
int Choice;
...
Choice = moveChoice(); // <--- 这里!!!
CheckMoveValid(grid, Choice);
...
}
英文:
In main()
, the call to moveChoice()
prompts the user for input, but then discards that input. The Choice
variable in main()
is never assigned a value, so the call to CheckMoveValid()
exhibits undefined behavior.
Get rid of the Choice
parameter in moveChoice()
, it is not needed. Have main()
assign the return value of moveChoice()
to the Choice
variable, eg:
int moveChoice()
{
int Choice;
...
cin >> Choice;
...
return Choice;
}
...
int main()
{
...
int Choice;
...
Choice = moveChoice(); // <-- HERE!!!
CheckMoveValid(grid, Choice);
...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论