gpt4 book ai didi

java - 骰子模拟 - 循环中的 onClickListener

转载 作者:行者123 更新时间:2023-12-01 15:20:38 25 4
gpt4 key购买 nike

我正在为移动应用程序类开发 Yahtzee 程序,但遇到了一些麻烦。即使 onClick() 仅被按下一次,我编写的循环也将运行整个循环(13 圈和 3 圈)。我已将它们移动到几个不同的订单中,但我似乎无法做到正确。有人可以指导我正确的方向,让 onClick 准确地记录转弯和滚动吗?

代码

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;



public class Yahtzee4Activity extends Activity {
/** Called when the activity is first created. */


ImageButton dice1, dice2, dice3, dice4, dice5;
Button roll, begin;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
PlayGame();
}

public void PlayGame()
{
final Random rand = new Random();

final int MAX_TURNS = 13;
final int MAX_ROLLS = 3;


dice1 = (ImageButton)findViewById(R.id.btndice1);
dice2 = (ImageButton)findViewById(R.id.btndice2);
dice3 = (ImageButton)findViewById(R.id.btndice3);
dice4 = (ImageButton)findViewById(R.id.btndice4);
dice5 = (ImageButton)findViewById(R.id.btndice5);

final ImageButton[] dice = {dice1, dice2, dice3, dice4, dice5}; //array of buttons (dice)
final int [] diceValue = new int [5];
final boolean [] isHeld = {false, false, false, false, false}; // array of dice to be held (hold)



roll = (Button)findViewById(R.id.btnroll);
begin = (Button)findViewById(R.id.btnbegin);

roll.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v ) {
RollDice(dice, diceValue, isHeld, rand);
}
});

int turnNum = 0;
for (int i = 0; i < MAX_TURNS; i++) {

int rollNum = 0;
for (int j = 0; j < MAX_ROLLS; j++) {
rollNum++;
roll.setText("Roll (" + (MAX_ROLLS - rollNum) + " Remaining)");
}
turnNum++;
ScoreDice();
}
}

private void ScoreDice() {
// TODO Auto-generated method stub

}

public int[] RollDice(ImageButton [] dice, int [] diceValue, boolean [] isHeld, Random rand)
{
for (int i = 0; i < dice.length; i++) {
if (!isHeld[i]) {
int rndInt = rand.nextInt(6) + 1; // Random number between 1 and 6
String imgName = "die" + rndInt;
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
diceValue[i] = rndInt;
dice[i].setImageResource(id); //Loops through the dice array and sets the appropriate dice images based on individual randoms
} else {
//do nothing
}
}
return diceValue;
}
}

最佳答案

它只是循环所有的滚动和转弯,因为你没有告诉它做其他事情。我建议将大部分代码移至 RollDice(...)。为了使下面的示例正常工作,您需要创建 rollNum、turnNum、MAX_TURNS、MAX_ROLLS 实例变量而不是局部变量,例如:

ImageButton dice1,dice2,dice3,dice4,dice5;
Button roll, begin;
private final int MAX_TURNS = 13;
private final int MAX_ROLLS = 3;
private int turnNum = 0;
private int rollNum = 0;

然后从 PlayGame 中删除回合代码并将其放入 RollDice 中,如下所示:

public int[] RollDice(ImageButton [] dice, int [] diceValue, boolean [] isHeld, Random rand)
{
for (int i = 0; i < dice.length; i++) {
if (!isHeld[i]) {
int rndInt = rand.nextInt(6) + 1; // Random number between 1 and 6
String imgName = "die" + rndInt;
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
diceValue[i] = rndInt;
dice[i].setImageResource(id); //Loops through the dice array and sets the appropriate dice images based on individual randoms
} else {
//do nothing
}
}

rollNum ++;
if(rollNum >= MAX_ROLLS){
//Turn is over
turnNum ++;
ScoreDice();
else {
roll.setText("Roll (" + (MAX_ROLLS - rollNum) + " Remaining)");
}

return diceValue;
}

我认为还值得指出的是,您的java代码风格可以进行一些复习,例如,人们普遍认为类名应以大写字母开头,方法名称应以小写字母开头,诸如此类。完全取决于你的粗糙程度,但如果你开始与其他程序员合作,开始时的良好习惯将有助于你进一步走下去

关于java - 骰子模拟 - 循环中的 onClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10972806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com