我感到很困惑。我正在尝试使用 Windows C++ Visual 创建一个 tic tac toe 游戏。到目前为止,我做得很好,直到我不断出错。我尝试寻求帮助,但似乎没有一个答案是正确的。这是我的练习题。
- 实现 displayBoard 以显示 Tic Tac Toe 棋盘。
-
提示用户在板上选择一个框,即 1 到 9 之间的数字,其中 1 是左上角。
使用 cin.get(box) 获取盒子编号并使用 isdigit 验证它是一个
数字;
1 | 2 | 3
4 | 5 | 6
7 | 8 | 9
如果盒子可用,则将适当的 X 或 O 放入其中并切换玩家,即 X 变为 O,反之亦然。
如果该框不可用,则会警告用户并获取另一个框,直到他们选择有效的打开框。
所有点都被选择后显示“Game Over!”;
- 编写一个主函数来使用 TicTacToe 类并测试上述所有功能。
.
#include<iostream>
using namespace std;
class TicTacToe {
public:
void displayBoard();
void getMove();
void playGame();
private:
char board[9];
char player; // Switch after each move.
};
int main ()
{
TicTacToe ttt;
// you need to do the following in a loop 9 times
ttt.playGame();
}
void TicTacToe::playGame()
{
getMove();
// Your implementation here...
}
void TicTacToe::displayBoard()
{
// Your implementation here...
}
void TicTacToe::getMove()
{
cout << "Enter Box: ";
char c;
cin.get(c);
if (c > '9' || c < '0')
// Error message here.
int number = c - '0';
cout << "your number is " << number;
// Your implementation here...
}
你需要一个声明来跟随 if
即使它只是一个 ;
但也许你想要
if (c > '9' || c < '0')
cout << "Not a Number!";
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)