【发布时间】:2018-12-19 19:42:13
【问题描述】:
我正在尝试创建一个井字游戏,其中用户输入一个数字(对应于他想要放置他的 X 或 O 的位置),但接收数字的变量 (Move) 保持在 0 no不管输入是什么。你能帮我弄清楚要修复什么,以便变量实际接收用户输入的内容吗?这是接收移动的函数的代码:
int FDeclarations::GetMove(){
int Move;
std::cin >> Move;
return Move;}
这是通过 switch 语句的函数的代码(因为变量“Move”总是为 0,所以没有任何作用)
int FDeclarations::PlaceMove(){
switch (Move)
{
case(1):
if (turn == false) { TopLeft = 'x'; }
else { TopLeft = 'o'; }
break;
case(2):
if (turn = false) { TopMid = 'x'; }
else { TopMid = 'o'; }
break;
case(3):
if (turn = false) { TopRight = 'x'; }
else { TopRight = 'o'; }
break;
case(4):
if (turn = false) { MidLeft = 'x'; }
else { MidLeft = 'o'; }
break;
case(5):
if (turn = false) { MidMid = 'x'; }
else { MidMid = 'o'; }
break;
case(6):
if (turn = false) { MidRight = 'x'; }
else { MidRight = 'o'; }
break;
case(7):
if (turn = false) { BotLeft = 'x'; }
else { BotLeft = 'o'; }
break;
case(8):
if (turn = false) { BotMid = 'x'; }
else { BotMid = 'o'; }
break;
case(9):
if (turn = false) { BotRight = 'x'; }
else { BotRight = 'o'; }
break;
}
table();
return 0;
}
这是我的变量声明:
class FDeclarations
{
public:
int PlaceMove();
int GetMove();
int CheckWin();
void table();
private:
bool turn = false;
int Move;
char TopLeft = '1';
char TopMid = '2';
char TopRight = '3';
char MidLeft = '4';
char MidMid = '5';
char MidRight = '6';
char BotLeft = '7';
char BotMid = '8';
char BotRight ='9';
bool XWin;
bool OWin;
};
【问题讨论】:
-
您正在重新定义移动。只需删除您获得输入的
int Move -
'1'与1不同。一个是char,另一个是int。 -
您的
GetMove函数本身看起来不错,您只是将FDeclarations::Move成员与Move内部的Move局部变量 混淆了GetMove,正在遮蔽该成员。您应该在返回新动作和修改成员Move之间进行选择。 -
还有
if (turn = false)- 这是什么? C++ 中的相等比较由==运算符表示。 -
我在你的代码中看不到任何地方你会打电话给你的
GetMove。电话在哪里?告诉我们你是怎么称呼它的。