井字游戏:人类与计算机



我现在正在努力解决的问题是,在人类选择第一个动作后,试图让计算机在随机位置移动。在代码的底部,我试图将计算机设置为随机的x和y值,但我还需要检查以确保计算机不会随机化已经拍摄的点。所以我尝试做的是修复底部的最后一个 else 语句,我应该很好。谁能弄清楚我需要做什么?谢谢!

#include<iomanip>
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */
using namespace std;
int main ()
{
char board [3][3] = {' ',' ',' ',' ',' ',' ',' ',' ',' ',}; // This is going to create the 3X3 board for the X's and O's to be placed
bool playing = true;
int x,y; // the X and Y will correspond to the rows and columns
int randomX, randomY;
while (playing) {
for (int i=0;i<3; i++){
for (int j=0;j<3; j++){
cout<<board[i][j];
if (j<2)
cout << "|";
}
if (i<2){
cout<<endl;
cout << "-----";
cout << endl;
}
}
cout << endl << "Where would you like to move? ";
cin >> x >> y;
board [x-1][y-1] = 'x'; // When the user is asked to place a X in a certain box, the values they enter will be subtracted by 1 so that it corresponds to the location on the board
if (board [0][0]== 'x' && board [0][1]== 'x'){
if (board [0][2] == 'x')
playing = false;
else board [0][2] = 'o';// This is the start of all the rows being checked
}
else if (board [0][0]== 'x' && board [0][2]== 'x'){
if  (board [0][1]== 'x')
playing = false;
else board [0][1] = 'o';
}
else if (board [0][1]== 'x' && board [0][2]== 'x'){
if  (board [0][0]== 'x')
playing = false;
else board [0][0] = 'o';
}
else if (board [1][0]== 'x' && board [1][1]== 'x'){
if  (board [1][2]== 'x')
playing = false;
else board [1][2] = 'o';
}
else if (board [1][0]== 'x' && board [1][2]== 'x'){
if  (board [1][1]== 'x')
playing = false;
else board [1][1] = 'o';
}
else if (board [1][1]== 'x' && board [1][2]== 'x'){
if  (board [1][0]== 'x')
playing = false;
else board [1][0] = 'o';
}
else if (board [2][0]== 'x' && board [2][1]== 'x'){
if  (board [2][2]== 'x')
playing = false;
else board [2][2] = 'o';
}
else if (board [2][0]== 'x' && board [2][2]== 'x'){
if  (board [2][1]== 'x')
playing = false;
else board [2][1] = 'o';
}
else if (board [2][1]== 'x' && board [2][2]== 'x'){
if  (board [2][0]== 'x')
playing = false;
else board [2][0] = 'o';// this is the end of all the rows being checked
}
else if (board [0][0]== 'x' && board [1][0]== 'x'){
if  (board [2][0]== 'x')
playing = false;
else board [2][0] = 'o';// this is the start of all the columns being checked
}
else if (board [0][0]== 'x' && board [2][0]== 'x'){
if  (board [1][0]== 'x')
playing = false;
else board [1][0] = 'o';
}
else if (board [1][0]== 'x' && board [2][0]== 'x'){
if  (board [0][0]== 'x')
playing = false;
else board [0][0] = 'o';
}
else if (board [0][1]== 'x' && board [1][1]== 'x'){
if  (board [2][1]== 'x')
playing = false;
else board [2][1] = 'o';
}
else if (board [0][1]== 'x' && board [2][1]== 'x'){
if  (board [1][1]== 'x')
playing = false;
else board [1][1] = 'o';
}
else if (board [1][1]== 'x' && board [2][1]== 'x'){
if  (board [0][1]== 'x')
playing = false;
else board [0][1] = 'o';
}
else if (board [0][2]== 'x' && board [1][2]== 'x'){
if  (board [2][2]== 'x')
playing = false;
else board [2][2] = 'o';
}
else if (board [0][2]== 'x' && board [2][2]== 'x'){
if  (board [1][2]== 'x')
playing = false;
else board [1][2] = 'o';
}
else if (board [1][2]== 'x' && board [2][2]== 'x'){
if  (board [0][2]== 'x')
playing = false;
else board [0][2] = 'o';// This is the end of all the columns being checked
}
else if (board [0][0]== 'x' && board [1][1]== 'x'){
if  (board [2][2]== 'x')
playing = false;
else board [2][2] = 'o';// This is the start of checking for diagonals
}
else if (board [0][0]== 'x' && board [2][2]== 'x'){
if  (board [1][1]== 'x')
playing = false;
else board [1][1] = 'o';
}
else if (board [1][1]== 'x' && board [2][2]== 'x'){
if  (board [0][0]== 'x')
playing = false;
else board [0][0] = 'o';
}
else if (board [0][2]== 'x' && board [1][1]== 'x'){
if  (board [2][0]== 'x')
playing = false;
else board [2][0] = 'o';
}
else if (board [0][2]== 'x' && board [2][0]== 'x'){
if  (board [1][1]== 'x')
playing = false;
else board [1][1] = 'o';
}
else if (board [1][1]== 'x' && board [2][0]== 'x'){
if  (board [0][2]== 'x')
playing = false;
else board [0][2] = 'o';
}
else {
srand(time(0));
randomX = rand()% 3;
randomY = rand()% 3;
board [randomX][randomY]== 'o';
}
return 0;
}
} 

如果我理解正确,您目前正在尝试"修复"最后一个 else 语句。 修复意味着:您希望计算机播放器更改"board [randomX][randomY]"处的值。

目前您正在做三件事:

  1. 初始化随机数生成器。
  2. 获取 2 个随机索引作为位置。
  3. 将该位置与'o'进行比较。(==对此进行了比较。它不会更改值。

您可能想做什么:

  1. 初始化一次,非常高。实际上没有常量,因为计算机总是会播放相同的数字,否则。
  2. 将您的随机位置设置为值'o'

[randomX][randomY] = 'o';而不是[randomX][randomY] == 'o';

此外,您可能应该检查该职位是否已经具有值。否则,您只需覆盖之前的任何内容。

在此之后,您需要测试计算机是否获胜。

哦。您的return 0;处于 while 循环中。它可能应该在外面。

最后:查看帖子下的评论。那里有一些好/有趣的建议。例如,您可以使用一个介于 0-8 之间的随机数,而不是两次乘以 0-2。

尤其是格式建议:请考虑使用以下内容,而不是格式化:

if  (board[1][0] == 'x')
playing = false;
else 
board[1][0] = 'o';

保持一致,并相应地缩进代码。

我希望这有所帮助。 干杯。

最新更新