java - 井字棋 vs 电脑

标签 java loops matrix tic-tac-toe

在一项作业中,我被要求创建一个 [7] x [7] 矩阵,以及一个与计算机对战的井字棋游戏。玩家是 X,计算机是 O。 [1][1] 是选择 1,[1][3] 是选择 2,[1][5] 是选择 3,[3][1] 是选择 4,依此类推,直到选择 9。 除了创建两个 boolean 方法来传递播放器和计算机方法来检查空间是否已被占用之外,我已经完成了所有操作。

我似乎无法创建一个 while 循环和 if 语句,逻辑上告诉计算机一旦生成 1 到 9 之间的随机数,如果该数字已被获取,则生成另一个随机数,直到该数字被获取没有被采取。

public static char[][] ComputerPlays(char[][] M)
{
    System.out.println("Computer selects grid position...");
    //  *** computer play code ***

    int x = (int)((Math.random() * 9)+1);
    if (x ==1)
    {
        while (occupied(M[1][1]) == true)
        {
            x = (int)((Math.random() *9)+1);      
        }
        if (occupied(M[1][1])== false)
        {
            M[1][1] = 'O';        
        } 
    }
    if (x ==2)
    {
        if (occupied(M[1][3])== true)
        {
            x = (int)((Math.random() *9) +1);
        }
        if (occupied(M[1][3])== false)
        {
            M[1][3] = 'O';
        }
    }
    while (x ==3)
    {
        if (occupied(M[1][5])== true)
        {
            x = (int)((Math.random() *9) +1);
        }
        if (occupied(M[1][5])== false)
        {
            M[1][5] = 'O';        
        }

    }
    while (x ==4)
    {
        if (occupied(M[3][1])== true)
        {
            x = (int)((Math.random() *9) +1);
        }
        if (occupied(M[3][1])== false)
        {
            M[3][1] = 'O';
        }
    }
    while (x ==5)
    {
        if (occupied(M[3][3])== true)
        {
            x = (int)((Math.random() *9) +1);
        }
        if (occupied(M[3][3])== false)
        {
            M[3][3] = 'O';
        }
    }
    while(x ==6)
    {
        if (occupied(M[3][5])== true)
        {
            x = (int)((Math.random() *9) +1);
        }
        if (occupied(M[3][5])== false)
        {
            M[3][5] = 'O';
        }
    }
    while(x ==7)
    {
        if (occupied(M[5][1])== true)
        {
            x = (int)((Math.random() *9) +1);
        }
        if (occupied(M[5][1])== false)
        {
            M[5][1] = 'O';
        }
    }
    while (x ==8)
    {
        if (occupied(M[5][3])== true)
        {
            x = (int)((Math.random() *9) +1);
        }
        if (occupied(M[5][3])== false)
        {
            M[5][3] = 'O';
        }
    }
    while (x ==9)
    {
        if (occupied(M[5][5])== true)
        {
            x = (int)((Math.random() *9) +1);
        }
        if (occupied(M[5][5])== false)
        {
            M[5][5] = 'O';
        }
    }
    return M;
}//end Computer Play

最佳答案

您的代码将重新滚动一个新位置,但不会重新检查之前测试的位置。

例如,当您掷出 5 时,它已被占用,您将重新掷出,但不会返回并重新检查 1 至 4。如果新掷出的 1到 4 时,将不会播放任何内容。

您需要将滚动和检查(所有位置)放入循环中。在(最终)滚动有效位置后,可以将实际的播放排除在循环之外。

这是我徒手编写的示例(由于时间限制):

public static char[][] ComputerPlays( char[][] M ) {
   System.out.println("Computer selects grid position...");
   int pos, x, y;
   do {
      pos = (int)(Math.random() * 9); // Roll a new position.
      x = ( pos / 3 )*2 + 1;
      y = ( pos % 3 )*2 + 1;
   } while ( occupied( M[x][y] ) ); // Repeat as long as the position is occupied.
   M[x][y] = 'O';
   return M;
} //end ComputerPlays

另一种方法是保留一个未平仓职位列表,当它们消失时将其从列表中删除,并且只在列表上滚动。这将消除重新滚动的需要。

关于java - 井字棋 vs 电脑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58776168/

相关文章:

java - 如何从 groovy 中的日期中提取一年中的某一天

python - 使用循环将值添加到字典键

matlab - 翻转 1's and 0' 的 5% 左右

ruby - Ruby 中的错误反转矩阵

PHP循环遍历数组从n到n-1个元素

algorithm - 来自采访 : Removing rows and columns in an n×n matrix to maximize the sum of remaining values

java - WAGU( TableView 中的数据)库中的修改

java - application.properties 内的变量

java - finally block 中的代码和 finally block 之后的代码有什么区别?

python - 如何只对 python 循环中的第一项执行某些操作?