重置第一个循环后的值井字c++
Reset the values after first loop tic tac toe c++?
游戏不会重置上次游戏的输入。它有第一个游戏的输入,不会在询问用户是否想再次玩的循环中重置。我尝试制作一个void函数,通过使用两个相同的数组来重置代码的值,但它仍然不起作用。请帮忙,因为我对c++还很陌生。
#include <iostream>
using namespace std;
char matrix[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
const char matrix2[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
char player = 'X';
int n;
int xscore = 0, oscore = 0, gamenum = 1;
void Draw()
{
system("cls");
cout << "T i c T a c T o e" << endl;
cout << "Game #" << gamenum << " Score: X - [" << xscore << "] O - [" << oscore << "]" << endl;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
void Input()
{
int a;
cout << "It's " << player << " turn. " <<"Press the number of the field: ";
cin >> a;
if (a == 1)
{
if (matrix[0][0] == '1')
matrix[0][0] = player;
else
{
cout << "Number is already filled try again." << endl;
Input();
}
}
else if (a == 2)
{
if (matrix[0][1] == '2')
matrix[0][1] = player;
else
{
cout << "Number is already filled try again." << endl;
Input();
}
}
else if (a == 3)
{
if (matrix[0][2] == '3')
matrix[0][2] = player;
else
{
cout << "Number is already filled try again." << endl;
Input();
}
}
else if (a == 4)
{
if (matrix[1][0] == '4')
matrix[1][0] = player;
else
{
cout << "Number is already filled try again." << endl;
Input();
}
}
else if (a == 5)
{
if (matrix[1][1] == '5')
matrix[1][1] = player;
else
{
cout << "Number is already filled try again." << endl;
Input();
}
}
else if (a == 6)
{
if (matrix[1][2] == '6')
matrix[1][2] = player;
else
{
cout << "Number is already filled try again." << endl;
Input();
}
}
else if (a == 7)
{
if (matrix[2][0] == '7')
matrix[2][0] = player;
else
{
cout << "Number is already filled try again." << endl;
Input();
}
}
else if (a == 8)
{
if (matrix[2][1] == '8')
matrix[2][1] = player;
else
{
cout << "Number is already filled try again." << endl;
Input();
}
}
else if (a == 9)
{
if (matrix[2][2] == '9')
matrix[2][2] = player;
else
{
cout << "Number is already filled try again." << endl;
Input();
}
}
}
void TogglePlayer()
{
if (player == 'X')
player = 'O';
else
player = 'X';
}
char Win()
{
//first player
if (matrix[0][0] == 'X' && matrix[0][1] == 'X' && matrix[0][2] == 'X')
return 'X';
if (matrix[1][0] == 'X' && matrix[1][1] == 'X' && matrix[1][2] == 'X')
return 'X';
if (matrix[2][0] == 'X' && matrix[2][1] == 'X' && matrix[2][2] == 'X')
return 'X';
if (matrix[0][0] == 'X' && matrix[1][0] == 'X' && matrix[2][0] == 'X')
return 'X';
if (matrix[0][1] == 'X' && matrix[1][1] == 'X' && matrix[2][1] == 'X')
return 'X';
if (matrix[0][2] == 'X' && matrix[1][2] == 'X' && matrix[2][2] == 'X')
return 'X';
if (matrix[0][0] == 'X' && matrix[1][1] == 'X' && matrix[2][2] == 'X')
return 'X';
if (matrix[2][0] == 'X' && matrix[1][1] == 'X' && matrix[0][2] == 'X')
return 'X';
//second player
if (matrix[0][0] == 'O' && matrix[0][1] == 'O' && matrix[0][2] == 'O')
return 'O';
if (matrix[1][0] == 'O' && matrix[1][1] == 'O' && matrix[1][2] == 'O')
return 'O';
if (matrix[2][0] == 'O' && matrix[2][1] == 'O' && matrix[2][2] == 'O')
return 'O';
if (matrix[0][0] == 'O' && matrix[1][0] == 'O' && matrix[2][0] == 'O')
return 'O';
if (matrix[0][1] == 'O' && matrix[1][1] == 'O' && matrix[2][1] == 'O')
return 'O';
if (matrix[0][2] == 'O' && matrix[1][2] == 'O' && matrix[2][2] == 'O')
return 'O';
if (matrix[0][0] == 'O' && matrix[1][1] == 'O' && matrix[2][2] == 'O')
return 'O';
if (matrix[2][0] == 'O' && matrix[1][1] == 'O' && matrix[0][2] == 'O')
return 'O';
return '/';
}
void gamereset()
{
for (int g = 0; g < 3; g++)
{
for (int h = 0; h < 3; h++)
{
matrix2[g][h] == matrix[g][h];
}
}
}
int main()
{
char user;
do{
n = 0;
Draw();
while (1)
{
n++;
Input();
Draw();
if (Win() == 'X')
{
cout << "X wins!" << endl;
xscore++;
break;
}
else if (Win() == 'O')
{
cout << "O wins!" << endl;
oscore++;
break;
}
else if (Win() == '/' && n == 9)
{
cout << "It's a draw!" << endl;
break;
}
TogglePlayer();
}
cout << "Do you want to play again[Y/N]:";
cin >> user;
gamenum++;
gamereset();
}
while(user == 'Y' or user == 'y');
}
正如您所看到的,gamreset((函数不起作用。我尝试了很多方法来找出如何重置它,但仍然没有成功。
将matrix2[g][h] == matrix[g][h];
替换为matrix[g][h] = matrix2[g][h];
不起作用的原因是==
是if
语句中使用的运算符,并根据左侧和右侧是否相等返回true
或false
。而=
使左分量等于右分量的值,正如我所看到的,你的matrix2
是const
,这意味着它的值不能改变,所以你可能想把matrix2
复制到matrix
,而不是把matrix
复制到matrix2
,你也可以用memcpy
来做。
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为"adjacent"变量赋值时出现问题
- HEX值到wchar_t字符(UTF-8)的转换
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 数组索引的值没有增加
- std::向量与传递值的动态数组
- 使用指针从C++中的数组中获取最大值
- 将值指定给向量(2D)的向量中的某个位置
- (C++)分析树以计算返回错误值的简单算术表达式
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 使用一个考虑到std::map中键值的滚动或换行的键
- C++中的赋值发生,尽管右侧出现异常
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- 井字游戏具有奇怪行为的最小最大值算法(C++)
- 井字游戏:评估节点的启发式值
- 我如何重置我的井字游戏的值
- 井字游戏的这个最小最大值实现有什么问题?
- 创建一个井字游戏.我想创建一个 3x3 向量来存储行和列的值,然后在函数中打印出电路板
- 在C++中实现井字游戏的最小最大值算法