如何随机选择一个java按钮,我正在尝试创建一个tic-tac-toe游戏,用户可以在其中播放cpu或其他玩家。我让它适合2名玩家工作,但我坚持一个玩家游戏,我不知道它是否可以完成但我的想法是我只是随机选择一个按钮进行cpu检查,看看它是否先前被选中然后分配适用于所选方格的x或o。
public void buttonSelected(ActionEvent click) {
Object source = click.getSource();
// loop through to see which button has been selected
if(onePlayer){
// User Vs CPU
/*if((turn % 2 == 0)){// CPU Turn
int selected;
do {
selected = new Random().nextInt(btnEmpty.length );
if (chosen[selected -1] == false){
chosen[selected -1] = true;
}
}while (chosen[selected -1] == true);
source = Integer.valueOf(selected);
for(int i=1; i<= btnNotSelected.length; i++) {
if(source == btnNotSelected[i] && turn < 10) {
btnClicked = true; // user has selected a button
// Check which user selected button and insert appropriate x or y
btnNotSelected[i].setText("X");
btnNotSelected[i].setEnabled(false); // disable selected button
pnlPlayingField.requestFocus(); // highlight selected panel
}
}
}
else{ //User Turn
for(int i=1; i<=9; i++) {
if(source == btnNotSelected[i] && turn < 10) {
btnClicked = true; // user has selected a button
// Check which user selected button and insert O
btnNotSelected[i].setText("O");
btnNotSelected[i].setEnabled(false);
chosen[i] = true;// disable selected button
pnlPlayingField.requestFocus(); // highlight selected panel
}
}
} */
turn++;
}
else if(twoPlayer){
for(int i=1; i<=9; i++) {
if(source == btnNotSelected[i] && turn < 10) {
btnClicked = true; // user has selected a button
// Check which user selected button and insert appropriate x or y
if(turn % 2 == 0){
btnNotSelected[i].setText("X");
}
else{
btnNotSelected[i].setText("O");
}
btnNotSelected[i].setEnabled(false); // disable selected button
pnlPlayingField.requestFocus(); // highlight selected panel
turn++;
}`
答案 0 :(得分:0)
int selected;
do {
selected = new Random().nextInt(btnEmpty.length );
if (chosen[selected -1] == false){
chosen[selected -1] = true;
}
}while (chosen[selected -1] == true);
上面的代码是无限循环,将其更改为:
int selected;
do {
selected = new Random().nextInt(btnEmpty.length);
}while (chosen[selected] == true);
chosen[selected] == true;
删除-1,因为nextInt(n)会给你一个“介于0(含)和n(独占)之间的数字”
答案 1 :(得分:0)
我个人会从一个List
按钮开始,在每个按钮被移除时将其移除,这将更容易确定选择的内容和没有选择的内容,但是让我们一起工作我们有......
List<Integer> free = new ArrayList<Integer>(chosen.length);
for (int index = 0; index < chosen.length; index++) {
if (!chosen[index]) {
free.add(index);
}
}
if (!free.isEmpty()) {
Collections.shuffle(free);
int selected = free.get(0);
// Continue with game logic
}
基本上,这会在Integer
中放置一个代表“免费”广告位的List
。然后我们使用Collections.shuffle
随机化列表,然后我们抓住第一个元素(因为想要抓住的东西)并继续游戏逻辑......
这消除了无限循环试图找到不存在的空闲插槽的可能性......
答案 2 :(得分:0)
一个玩家的井字游戏当然可以完成,你随机选择的策略很好。注释掉一个播放器代码中的第一个特定错误是无限的do-while循环。循环上的条件总是求值为真,因为chosen[selected - 1]
总是为真(如果它是假,你在条件检查之前将它设置为真),因此再次循环。
你的做法应该是这样的:
do {
selected = new Random().nextInt(btnEmpty.length);
} while (chosen[selected - 1] == true);
chosen[selected - 1] = true;
这样,您将在while循环条件之后设置所选标志。
我在onePlayer块中看到的一些其他问题:
source
(此处为整数)和btnNotSelected[i]
之间的比较(假设基于双层块中的工作代码的java按钮)将无法正常工作到如果不对您的整体编码风格和策略进行任何重大更改,我会尝试将onePlayer部分转换为更具功能性的部分:
public void buttonSelected(ActionEvent click) {
Object source = click.getSource();
if (onePlayer) {
// User's turn first
source.setText("O");
source.setEnabled(false);
pnlPlayingField.requestFocus();
// Mark that button as chosen
for (int i = 0; i < btnNotSelected.length; i++) {
if (source == btnNotSelected[i]) {
chosen[i] = true;
break;
}
}
// Increment turn counter
turn++;
// Check if game is over
if (turn > 9) return;
// CPU turn
int selected;
do {
selected = new Random().nextInt(btnNotSelected.length);
} while (chosen[selected]);
chosen[selected] = true;
btnNotSelected[selected].setText("X");
btnNotSelected[selected].setEnabled(false);
pnlPlayingField.requestFocus();
turn++;
} else if (twoPlayer) {
/* your preexisting twoPlayer code */
}
}