井字游戏vs电脑

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

我似乎无法创建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
kangzhai77 回答:井字游戏vs电脑

您的代码将重新分配新职位,但不会重新检查以前测试过的职位。

例如,当您滚动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

另一种方法是保留一个未平仓清单,将其从列表中删除,然后在清单上滚动。这样就无需重新滚动。

本文链接:https://www.f2er.com/3134133.html

大家都在问