C ++ Tic Tac Toe游戏 [英] C++ Tic Tac Toe Game
问题描述
我很困惑。我试图创建一个tic tac toe游戏使用windows c ++视觉。到目前为止,我做得很好,直到我不断收到错误。我试图寻求帮助,但没有一个答案似乎是对的。这是我的实践问题。
I am so confused. I am trying to create a tic tac toe game using windows c++ visual. So far I was doing good until I kept getting errors. I tried looking for help but none of the answers seemed right. This is my practice problem.
- 实施displayBoard以显示Tic Tac Toe板。
-
提示用户在板上选择一个框,即1到9之间的数字,其中1是左上角。
- Implement displayBoard to display Tic Tac Toe board.
Prompt User for a box on the board to select, i.e. a number between 1 and 9 with 1 being the upper left corner.
使用cin.get得到框号和isdigit来验证它是一个
号码;
1 | 2 | 3
4 | 5 | 6
7 | 8 | 9
如果盒子可用,在其中放置适当的X或O并切换播放器,即X变为O,反之亦然。
如果框不可用,警告用户并获得另一个框,直到他们选择有效的打开框。
use cin.get(box) to get the box number and isdigit to verify it is a number; 1 | 2 | 3 4 | 5 | 6 7 | 8 | 9 If the box is available put the appropriate X or O in there and switch players, i.e. X becomes O and vice versa. If the box is NOT available warn the user and get another box until they select a valid open box.
Game Over!;
After all spots have been select Display "Game Over!";
。
#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
you need a statement to go after the if
之后的语句,即使它只是一个;
even if its just a ;
但也许你想要
if (c > '9' || c < '0')
cout << "Not a Number!";
这篇关于C ++ Tic Tac Toe游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!