Java 随机空方块?@Dr.Avalanche我只是个14岁的孩子。我已经对逻辑进行了编程,但第一步我想让计算机随机选择一个sqaure。请帮帮我!难道你不想让电脑玩家按照游戏逻辑玩,而不是随机选择一个空方块吗?@Dr.Avalanche我只是个14岁的孩子。我已
Java 随机空方块?@Dr.Avalanche我只是个14岁的孩子。我已经对逻辑进行了编程,但第一步我想让计算机随机选择一个sqaure。请帮帮我!难道你不想让电脑玩家按照游戏逻辑玩,而不是随机选择一个空方块吗?@Dr.Avalanche我只是个14岁的孩子。我已,java,android,tic-tac-toe,Java,Android,Tic Tac Toe,随机空方块?@Dr.Avalanche我只是个14岁的孩子。我已经对逻辑进行了编程,但第一步我想让计算机随机选择一个sqaure。请帮帮我!难道你不想让电脑玩家按照游戏逻辑玩,而不是随机选择一个空方块吗?@Dr.Avalanche我只是个14岁的孩子。我已经对逻辑进行了编程,但第一步我想让计算机随机选择一个sqaure。请帮帮我!您只需从列表中删除该按钮-也要注意随机生成器,以避免IndexOutOfBounds异常!您只需从列表中删除该按钮-也要注意随机生成器,以避免IndexOutOfBou
随机空方块?@Dr.Avalanche我只是个14岁的孩子。我已经对逻辑进行了编程,但第一步我想让计算机随机选择一个sqaure。请帮帮我!难道你不想让电脑玩家按照游戏逻辑玩,而不是随机选择一个空方块吗?@Dr.Avalanche我只是个14岁的孩子。我已经对逻辑进行了编程,但第一步我想让计算机随机选择一个sqaure。请帮帮我!您只需从列表中删除该按钮-也要注意随机生成器,以避免IndexOutOfBounds异常!您只需从列表中删除该按钮-也要注意随机生成器,以避免IndexOutOfBounds异常!您只需从列表中删除该按钮-也要注意随机生成器,以避免IndexOutOfBounds异常!您只需从列表中删除该按钮-也要注意随机生成器,以避免IndexOutOfBounds异常!谢谢我一定会试试的。谢谢!我一定会试试的。谢谢!我一定会试试的。谢谢!我一定会试试的。
btn00 = (Button) findViewById(R.id.button);
btn01 = (Button) findViewById(R.id.button2);
btn01 = (Button) findViewById(R.id.button3);
btn10 = (Button) findViewById(R.id.button4);
btn11 = (Button) findViewById(R.id.button5);
btn12 = (Button) findViewById(R.id.button6);
btn20 = (Button) findViewById(R.id.button7);
btn21 = (Button) findViewById(R.id.button8);
btn22 = (Button) findViewById(R.id.button9);
/**
* RandomArray is a data structure similiar to a set, which removes a random number one at a time and decreases the size of the set by one.
*/
public class RandomArray {
int size;
int[] array;
/**
* The only constructor for this class. assigns numbers from 1 to m in the 0 to m-1 cells respectively.
* @param size holds the value of m - meaning, the method will generate a number for each movie.
*/
public RandomArray(int size) {
this.size = size;
array = new int[size];
for (int i = 0; i<size; i++) {
array[i] = i+1;
}
}
/**
* remove removes a number randomly and returns it.
* with each removal the number in the last cell replaces the removed number and size is decreased by one.
* @return a random number represents a movie that hasn't been played yet.
*/
public int remove() {
int ans = -1;
if (size > 0) {
// generating a random number from 0 to the last cell.
int randomNum = (int)(Math.random()*size);
ans = array[randomNum];
// last number replaces the number to be removed.
array[randomNum] = array[size-1];
size--;
}
return ans;
}
}
int[] state = new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0 }; // int[9]
Button[] buttons = new Button[] {
(Button) findViewById(R.id.button),
(Button) findViewById(R.id.button2),
(Button) findViewById(R.id.button3),
...
}
Random rnd = new Random();
int index = rnd.nextInt(state.length);
while(state[index] != 0) {
index = rnd.nextInt(state.length);
}
state[index] = 1;
Button b = buttons[index];
b.set....();
...
int getIndex(Button b) {
for(int i = 0; i < buttons.length; i++) {
if(buttons[i].equals(b)) {
return i;
}
}
return -1;
}