井字游戏程序帮助
Tic Tac Toe Program help
我不知道如何从我已经:我有麻烦让我的数组工作,我也有麻烦与我的图表重置每次我重新运行程序。它不会保存我的X和O,而是重新进入空白图表。任何提示都会有帮助。由于
/**
Lab 4 - Due 7/22/2010.
Convert Lab 3 to a class
1. Implement displayBoard to display Tic Tac Toe board.
2. 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.
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.
3. After all spots have been select Display "Game Over!";
4. Write a main function to use the TicTacToe class and test all of the above functionality.
**/
#include <iostream>
#include <limits>
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;
for (int i = 0; i < 9; i++) {
ttt.playGame();
}
}
void TicTacToe::playGame()
{
displayBoard();
getMove();
// Your implementation here...
}
void TicTacToe::displayBoard()
{
// Your implementation here...
bool firstMove = true;
if (firstMove == true)
{
for (int i = 0; i < 9; i++) {
board[i] = i + 1;
}
}
firstMove == false;
for (int i = 0; i < 9; i++)
{
if ( (i+1) % 3 == 0 )
{
cout << board[i] << endl;
}
else
{
cout << board[i] << " | ";
}
}
}
void TicTacToe::getMove()
{
if (player == 'X') {
player = 'O';
}
else {
player = 'X';
}
cout << player << " ";
cout << "Enter Box: ";
char c;
bool move;
move = true;
do {
cin.get(c);
cin.ignore(numeric_limits<int>::max(), 'n');
if (c > '9' || c < '0')
// error message
cout << "please enter a number 1-9" << endl;
int number = c - '0';
cout << "your number is " << number << endl;
// Your implementation here...
if (c == '1' && board[0] == '1') {
board[0] = player;
move = false;
}
else if(c == '2' && board[1] == '2')
{
board[1] = player;
move = false;
}
else if(c == '3' && board[2] == '3')
{
board[2] = player;
move = false;
}
else if(c == '4' && board[3] == '4')
{
board[3] = player;
move = false;
}
else if(c == '5' && board[4] == '5')
{
board[4] = player;
move = false;
}
else if(c == '6' && board[5] == '6')
{
board[5] = player;
move = false;
}
else if(c == '7' && board[6] == '7')
{
board[6] = player;
move = false;
}
else if(c == '8' && board[7] == '8')
{
board[7] = player;
move = false;
}
else if(c == '9' && board[8] == '9')
{
board[8] = player;
move = false;
}
} while (!move);
}
@Bunnit根据你的建议,我能够改变它。现在我唯一的问题是用X或O替换每个盒子,它似乎不像我的if和else if正在工作。
您是否使用DisplayBoard函数来显示您的板?
bool firstMove = true;
if (firstMove == true)
{
for (int i = 0; i < 9; i++) {
board[i] = i + 1;
}
}
我不确定你到底想要在这里实现什么,但firstMove将始终为真,所以每次调用这个时,你的板将被重置。
firstMove == false;
这一行没有作用,使用firstMove = false;
设置firstMove为false,尽管上面的if语句仍然会被调用,因为firstMove每次被调用时都被设置为true,如果你只想在第一次调用DisplayBoard时设置firstMove为true,你可以使用一个静态变量:static firstMove == true;
.
编辑
它看起来也像你把字符和整数混淆了,在DisplayBoard中你设置每个板瓷砖等于它相应的数字(上面的for语句),但是当你在getMove函数中检查这个时,你使用字符等量物:if (c == '1' && board[0] == '1')
。1 ! = ' 1 '。因为这是家庭作业,所以我把它留给你们去查找一个ascii图表来找出它等于什么
当一个程序结束时,它的所有内存将被操作系统回收,以便它可以被其他程序使用。当程序结束时,变量和数组中的所有值都将丢失。
如果你想在下次程序运行之前保持一些持久状态,你需要将变量/数组保存到一些存储介质中,比如文件或数据库。当程序稍后启动时,您将从存储介质中加载这些变量/数组。
如果他们还没有教你如何读/写文件,那么我怀疑他们希望你的游戏板在每次程序启动时都恢复到以前的状态。
与其在黑板上填满数字,然后用X和0覆盖,不如直接用整数格式输入。这可能有助于解决覆盖问题。另外,我不知道你是否学过2D数组,但如果你使用它们,它会对你的编程和逻辑思维过程有更多的帮助。
- C++调用具有 *this 属性的单个帮助程序函数
- 用于检查值是否为其任何参数的帮助程序函数
- 类型限定宏帮助程序
- CoreCLR 中的检测探查器 - 将帮助程序程序集加载到 dotnet 进程的方法
- 无法将 2D 数组传递到 c++ 中的帮助程序函数中
- C2783: 无法推断帮助程序函数的模板参数
- 友元函数需要一个帮助程序函数
- 在何处放置与类方法相关的帮助程序函数C++
- 给定概念,SFINAE 帮助程序是否仍在规范中作为未弃用?
- 使用 IP 帮助程序的 Windows 中活动 UDP 连接的远程地址
- 如何安装性能数据帮助程序
- 用于向公众公开数据成员的帮助程序结构
- 类参数化与类和帮助程序函数混淆
- 仅具有公共静态方法的帮助程序类
- Ebpf:帮助程序的未定义符号
- 函数和指针帮助:程序生成,但崩溃,没有任何特定错误
- 使用带有 MEX 包装器的帮助程序 C 文件从 MATLAB 2016 调用C++代码时出现问题
- 输入到字符串流并在单个语句中转换为字符串.可以没有帮助程序类
- 使用帮助程序模板结构时无法推断模板参数
- IE 探索 11 < c++ ATL COM 浏览器帮助程序对象(加载项)来替换 DOM 中的文本