井字游戏:人类与计算机

Posted

技术标签:

【中文标题】井字游戏:人类与计算机【英文标题】:Tic Tac Toe: Human Vs Computer 【发布时间】:2019-10-13 20:40:57 【问题描述】:

我现在正在努力解决的问题是试图让计算机在人类选择第一个动作后随机移动。在代码的底部,我试图将计算机设置为随机 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;

 

【问题讨论】:

欢迎来到 Stack Overflow。请阅读the help pages、the SO tour、阅读how to ask good questions,以及this question checklist。最后请学习如何创建minimal reproducible example 向我们展示,重点是minimal 无法正常工作、什么都不做或行为与您预期不同的事情:board [randomX][randomY];do...while...if (...) /* something with no ; */ else。如果您(并非不合理地)留下了要实现的东西,那么请使用 cmets 使它们更加明显。 提示:当已经放置X个棋子时,有3*3 - X空闲棋子,你只需要滚动一个随机数来选择它(从0到3*3 - X -1)而不是一个循环 我建议使用一致的缩进对你有利。 【参考方案1】:

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

目前你正在做三件事:

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

你可能想做的事:

    初始化一次,非常高。实际上不是使用常量,否则计算机总是会播放相同的数字。 将您的随机位置设置为值'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';

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

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

【讨论】:

以上是关于井字游戏:人类与计算机的主要内容,如果未能解决你的问题,请参考以下文章

井字游戏最小最大算法 Python。计算机算法并不稳健

带有井字游戏的 Minimax 算法(但每个玩家只能有 3 个 tacs)

为啥我的神经网络不能正确分类这些井字游戏模式?

JavaScript写的一个带AI的井字棋

Android井字游戏游戏界面

python井字棋算法及代码