C++ Tic Tac Toe程序帮助
我不知道如何从我已有的着手:我在让数组正常工作方面遇到了困难,每次重新运行程序时,我的图表重置也遇到了困难。它不保存我的X和O,而是重新启动到空白图表。任何提示都会有帮助。谢谢C++ Tic Tac Toe程序帮助,c++,tic-tac-toe,C++,Tic Tac Toe,我不知道如何从我已有的着手:我在让数组正常工作方面遇到了困难,每次重新运行程序时,我的图表重置也遇到了困难。它不保存我的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.
/**
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);
}
/**
实验4-截止日期为2010年7月22日。
将实验3转换为一个类
1.实现显示板以显示Tic Tac趾板。
2.提示用户在板上选择一个框,即1到9之间的数字,1为左上角。
使用cin.get(box)获取框号,并使用isdigit验证它是否为
数量;
1 | 2 | 3
4 | 5 | 6
7 | 8 | 9
如果盒子可用,将适当的X或O放入其中并切换播放器,即X变为O,反之亦然。
如果该框不可用,则警告用户并获取另一个框,直到用户选择有效的打开框。
3.在所有点都被选中后,显示“游戏结束!”;
4.编写一个main函数来使用TicTacToe类并测试上述所有功能。
**/
#包括
#包括
使用名称空间std;
提克塔克类{
公众:
无效显示板();
void getMove();
无效游戏();
私人:
炭板[9];
char player;//每次移动后切换。
};
int main()
{
蒂克塔克托ttt;
对于(int i=0;i<9;i++){
ttt.playGame();
}
}
void TicTacToe::playGame()
{
显示板();
getMove();
//您在这里的实现。。。
}
void TicTacToe::displayBoard()
{
//您在这里的实现。。。
bool firstMove=true;
if(firstMove==true)
{
对于(int i=0;i<9;i++){
板[i]=i+1;
}
}
firstMove==false;
对于(int i=0;i<9;i++)
{
如果((i+1)%3==0)
{
您是否正在使用DisplayBoard功能来显示您的电路板
bool firstMove = true;
if (firstMove == true)
{
for (int i = 0; i < 9; i++) {
board[i] = i + 1;
}
}
bool firstMove=true;
if(firstMove==true)
{
对于(int i=0;i<9;i++){
板[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和O,s覆盖,不如用整数格式输入。这可能有助于解决覆盖问题嗯。另外,我不知道你是否学习过2D数组,但如果你使用它们,会对你的编程和逻辑思维过程有更多帮助。这个问题每2-3天出现一次,似乎有些大学已经为此做了家庭作业。你的问题是什么?你被困在哪里了?你似乎晚了一周左右……每次我跑步该程序似乎没有保存我的数组。它只打印空格和|行。我尝试使用char board={'1','2','3',}以此类推,但这不起作用。不会晚一周。这只是我参加的一个暑期编程课程,目的是提高我的工作机会。@Zach:晚了一周和一年。啊,非常有帮助!!!但我仍然无法在黑板上显示数字。尽管在黑板上分配了每个数字,但还是无法显示出来[I]使用for.I-see!!但由于某些原因,我无法让我的显示板显示数字o.o我只获得空格和|行。除非我使用字符板[9]={'1','2'}这是唯一一次数字出现在黑板上。对不起,我在编程方面有点笨。你帮了我很多。这是真的,但我很难在每个框中维护X和O,而不让它们返回到1 2 3框。让我们尝试实现计算机与玩家模式来测试你的编程技能。