井字游戏 c++ Agrid 不会重置
Tic Tac Toe c++ Agrid doesn't reset
我是一个初级程序员。我用 c++ 构建了一个井字游戏。游戏运行正常,直到提示用户重复。这就是问题所在。程序未正确循环。任何帮助将不胜感激。谢谢。
#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
char matrix[10] = {'0','1','2','3','4','5','6','7','8','9' };
void display();
int checkwin();
int restarter();
int main(){
char repeat;
do {
string playername, player1, player2;
int winner = 0;
char mark = 0;
int number = 0;
int player = 1;
char choice = 0;
cout << "Player 1 please enter your name: ";
getline(cin, player1);
cout << "Player 2 please enter your name: ";
getline(cin, player2);
while (winner == 0)
{
display();
if (player % 2)
{
playername = player1;
}
else
playername = player2;
cout << playername << " " << "Please choose a number you want" << endl;
cin >> number;
if (player % 2)
{
mark = 'X';
}
else
mark = 'O';
if (number == 1 && matrix[1] == '1')
{
matrix[1] = mark;
}
else if (number == 2 && matrix[2] == '2')
{
matrix[2] = mark;
}
else if (number == 3 && matrix[3] == '3')
{
matrix[3] = mark;
}
else if (number == 4 && matrix[4] == '4')
{
matrix[4] = mark;
}
else if (number == 5 && matrix[5] == '5')
{
matrix[5] = mark;
}
else if (number == 6 && matrix[6] == '6')
{
matrix[6] = mark;
}
else if (number == 7 && matrix[7] == '7')
{
matrix[7] = mark;
}
else if (number == 8 && matrix[8] == '8')
{
matrix[8] = mark;
}
else if (number == 9 && matrix[9] == '9')
{
matrix[9] = mark;
}
else
{
cout << "WRONG MOVE!";
player--;
cin.ignore();
cin.get();
}
winner = checkwin();
player++;
display();
if (winner == 1)
{
cout << playername << " " << "WON!" << endl;
}
else
cout << "Its a draw!" << endl;
}
cout << "do u wana repeat?" << endl;
cin >> repeat;
} while (repeat == 'Y');
system("pause");
return 0;
}
void display()
{
system("CLS");
cout << "======= Welcome to You Tic and I Tac Your Toe =======" << endl;
cout << "======= Ivan =======" << endl;
cout << "======= & =======" << endl;
cout << "======= Mostafa =======" << endl;
cout << "=====================================================" << endl;
cout << "n" << endl;
cout << "PLAYER 1 [X] PLAYER 2 [O]" << endl;
cout << " | | " << endl;
cout << " " << matrix[1] <<" | "<<matrix[2]<<" | "<< matrix[3] << endl;
cout << "___|___|____" << endl;
cout << " | |" << endl;
cout << " " << matrix[4] <<" | "<<matrix[5]<<" | "<< matrix[6] << endl;
cout << "___|___|____" << endl;
cout << " | |" << endl;
cout << " " << matrix[7] <<" | "<<matrix[8]<<" | " << matrix[9] << endl;
cout << " | |" << endl;
}
int checkwin()
{
if (matrix[1] == matrix[2] && matrix[2] == matrix[3])
{
return 1;
}
else if (matrix[4] == matrix[5] && matrix[5] == matrix[6])
{
return 1;
}
else if (matrix[7] == matrix[8] && matrix[8] == matrix[9])
{
return 1;
}
else if (matrix[1] == matrix[4] && matrix[4] == matrix[7])
{
return 1;
}
else if (matrix[2] == matrix[5] && matrix[5] == matrix[8])
{
return 1;
}
else if (matrix[3] == matrix[6] && matrix[6] == matrix[9])
{
return 1;
}
else if (matrix[2] == matrix[5] && matrix[5] == matrix[8])
{
return 1;
}
else if (matrix[3] == matrix[5] && matrix[5] == matrix[7])
{
return 1;
}
else if (matrix[1] == matrix[5] && matrix[5] == matrix[9])
{
return 1;
}
else if (matrix[1] != '1' && matrix[2] != '2' && matrix[3] != '3' && matrix[4] != '4' && matrix[5] != '5' && matrix[6] != '6' && matrix[7] != '7' && matrix[8] != '8' && matrix[9] != '9')
return 2;
return 0;
}
问题是你在赢得游戏后没有清除你的矩阵。矩阵是一个全局变量,因此它是在程序开始时创建的,并且在程序停止之前不会被销毁。循环时,将销毁在循环中创建的所有局部变量,但不会销毁全局变量。您需要手动清除此阵列。像这样的函数:
void clearMatrix()
{
for(int i = 0; i<10; i++)
matrix[i] = '0'+i;
}
如果在适当的位置执行矩阵,将清除矩阵。
int main(){
char repeat;
do {
clearMatrix();
string playername, player1, player2;
int winner = 0;
char mark = 0;
...
}
如果你在 do-while 循环开始时调用这个 clearMatrix(( 函数,你的矩阵每次都会被重置。
cout << "Player 1 please enter your name: ";
cin >> player1;
cout << "Player 2 please enter your name: ";
cin >> player2;
请注意,您可以使用"cin"而不是"getline"来避免再次键入名称时出错。
还要这样做:
else
cout << "Its a draw!" << endl;
}
clearMatrix();
cout << "do u wanna repeat?" << endl;
cin >> repeat;
}
由于您必须清除矩阵,因此按照上述定义在底部调用函数clearMatrix();
,同时保留在 do-while 循环中是一种很好的编程习惯。
相关文章:
- 在C++游戏中与库存系统作斗争
- 换位表导致测试失败(但在游戏中运行良好)
- Craps游戏问题,忽略if语句
- 矢量下标超出SFML游戏中的范围
- 我是 c++ 的新手.学习基本知识后,我想做井字游戏.对于印刷板,我在下面写代码,但它显示错误
- 我在贪吃蛇游戏中收到了错误代码 -1073741571
- 我应该在简单的策略游戏中为各个派系使用类吗 - C++
- 游戏体验和升级
- 我的代码运行良好,但在游戏循环中中断
- 如何使用 SFML 在贪吃蛇游戏中定义游戏结束?
- 我可以使用任何好的逻辑来阻止计算机将 O 放在井字游戏中的现有 X 上
- 将鼠标和键盘输入发送到 unity3d 游戏 (Rust)
- 在猜谜游戏 c++ 中不计算尝试次数
- 在C++不适用于猜数字游戏的情况下再次播放选项
- 在为我的基于文本的 RPG 游戏制作库存时遇到困难
- 在定义字符数组(井字游戏)的 for 循环中应用输入限制
- Frank Luna 在他的书"使用 DirectX12 进行 3D 游戏编程"的介绍中盒子示例的问题
- 如何在Linux上正确发布C++软件(游戏)
- 提高基于组件的游戏引擎的效率
- 井字游戏 c++ Agrid 不会重置