C++井字游戏AI
C++ Tic Tac Toe AI
我几乎完成了我的井字游戏。目前它被设置为双人对人,但我知道我必须实现一个简单的 AI 才能获得批准。现在我需要你的帮助。我知道我必须分小步骤来考虑它,例如三种"采取行动"的方法,例如
- 如果 AI 在第 1 列中有移动 &&&右侧的两个框打开,则在任一框中移动并返回 true
- 如果AI在中间移动&左右的框打开,则在任一框中移动并返回true
- 如果 AI 在第 3 列中有移动 &&&左侧的两个框打开,则在任一框中移动并返回 true
我无法确切理解如何在下面的代码中实现它:
#include <iostream>
using namespace std;
char matrix[3][3] = { '7', '8', '9', '4', '5', '6', '1', '2', '3' };
char player = 'X';
int n;
void Draw()
{
system("cls");
cout << "Tic Tac Toe !n" << 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 << "nIt's " << player << " turn. " << "Press the number of the field: ";
cin >> a;
if (a == 7)
{
if (matrix[0][0] == '7')
matrix[0][0] = player;
else
{
cout << "Field is already in use try again!" << endl;
Input();
}
}
else if (a == 8)
{
if (matrix[0][1] == '8')
matrix[0][1] = player;
else
{
cout << "Field is already in use try again!" << endl;
Input();
}
}
else if (a == 9)
{
if (matrix[0][2] == '9')
matrix[0][2] = player;
else
{
cout << "Field is already in use try again!" << endl;
Input();
}
}
else if (a == 4)
{
if (matrix[1][0] == '4')
matrix[1][0] = player;
else
{
cout << "Field is already in use try again!" << endl;
Input();
}
}
else if (a == 5)
{
if (matrix[1][1] == '5')
matrix[1][1] = player;
else
{
cout << "Field is already in use try again!" << endl;
Input();
}
}
else if (a == 6)
{
if (matrix[1][2] == '6')
matrix[1][2] = player;
else
{
cout << "Field is already in use try again!" << endl;
Input();
}
}
else if (a == 1)
{
if (matrix[2][0] == '1')
matrix[2][0] = player;
else
{
cout << "Field is already in use try again!" << endl;
Input();
}
}
else if (a == 2)
{
if (matrix[2][1] == '2')
matrix[2][1] = player;
else
{
cout << "Field is already in use try again!" << endl;
Input();
}
}
else if (a == 3)
{
if (matrix[2][2] == '3')
matrix[2][2] = player;
else
{
cout << "Field is already in use 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 '/';
}
int main()
{
n = 0;
Draw();
while (1)
{
n++;
Input();
Draw();
if (Win() == 'X')
{
cout << "X wins!" << endl;
break;
}
else if (Win() == 'O')
{
cout << "O wins!" << endl;
break;
}
else if (Win() == '/' && n == 9)
{
cout << "It's a draw!" << endl;
break;
}
TogglePlayer();
}
system("pause");
return 0;
}
像
井字棋这样的简单棋盘游戏的计算机播放器可以使用Minimax算法实现,该算法可以通过α β修剪进行改进。尽管生成的实现将非常小,但可能需要一些时间来理解。
#include <iostream>
#include <string>
using namespace std;
int main()
{
/*
the chart is :
X 2 X
4 5 6
X 8 X
*/
char Matrix[3][3] = { 'X','2','X','4','5','6','X','8','X' };
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << Matrix[i][j] << " ";
}
cout << endl;
}
/*
after this for :
X O X
4 5 6
X O X
*/
int l = 0, m = 0, p[3] = { 0,0,0 };
for (l; l <= 2; l++)
{
for (m; m <= 2; m++)
{
if ((Matrix[l][m]) == 'X')
{
p[l]++;
if ((p[l]) == 2)
{
for (m; m >= 0; m--)
{
if ((Matrix[l][m]) != 'X')
{
Matrix[l][m] = 'O';
}
}
}
}
}
}
return 0;
}
why this code dosent work?
相关文章:
- 在C++游戏中与库存系统作斗争
- 换位表导致测试失败(但在游戏中运行良好)
- Craps游戏问题,忽略if语句
- 矢量下标超出SFML游戏中的范围
- 我是 c++ 的新手.学习基本知识后,我想做井字游戏.对于印刷板,我在下面写代码,但它显示错误
- 我在贪吃蛇游戏中收到了错误代码 -1073741571
- 我应该在简单的策略游戏中为各个派系使用类吗 - C++
- 游戏体验和升级
- 我的代码运行良好,但在游戏循环中中断
- 如何使用 SFML 在贪吃蛇游戏中定义游戏结束?
- 我可以使用任何好的逻辑来阻止计算机将 O 放在井字游戏中的现有 X 上
- 将鼠标和键盘输入发送到 unity3d 游戏 (Rust)
- 在猜谜游戏 c++ 中不计算尝试次数
- 在C++不适用于猜数字游戏的情况下再次播放选项
- 在为我的基于文本的 RPG 游戏制作库存时遇到困难
- 在定义字符数组(井字游戏)的 for 循环中应用输入限制
- Frank Luna 在他的书"使用 DirectX12 进行 3D 游戏编程"的介绍中盒子示例的问题
- C++井字游戏AI
- 允许测试AI算法的游戏
- c++中井字游戏的AI问题