井字游戏:人与计算机

时间:2019-10-13 20:40:57

标签: c++ arrays loops

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

#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;
}
} 

1 个答案:

答案 0 :(得分: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';

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

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