井字游戏与计算机
Tic Tac Toe vs Computer
对于一项作业,我被要求创建一个 [7] x [7] 矩阵,以及一个与计算机的井字游戏。玩家是 X,计算机是 O。
[1][1] 是选择 1,[1][3] 是选择 2,[1][5] 是选择 3,[3][1] 是选择 4,依此类推,直到选择 9。
除了创建两个布尔方法来传递播放器和计算机方法以检查 space 是否已被占用之外,我已经完成了所有工作。
我似乎无法创建一个 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
另一种方法是保留一个未平仓头寸列表,当它们消失时将它们从列表中删除,并且只在列表中滚动。这将消除重新滚动的需要。
对于一项作业,我被要求创建一个 [7] x [7] 矩阵,以及一个与计算机的井字游戏。玩家是 X,计算机是 O。 [1][1] 是选择 1,[1][3] 是选择 2,[1][5] 是选择 3,[3][1] 是选择 4,依此类推,直到选择 9。 除了创建两个布尔方法来传递播放器和计算机方法以检查 space 是否已被占用之外,我已经完成了所有工作。
我似乎无法创建一个 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
另一种方法是保留一个未平仓头寸列表,当它们消失时将它们从列表中删除,并且只在列表中滚动。这将消除重新滚动的需要。