我正在为移动应用程序类开发Yahtzee程序,遇到了一些麻烦。 我编写的循环将贯穿循环(13 圈和 3 卷),即使只按下一次 onClick() 也是如此。我已经将它们移动到几个不同的顺序中,但我似乎无法正确处理。 有人可以指导我朝着正确的方向前进,让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代码风格可以进行一些刷新,例如,通常认为类名应该以大写字母开头,方法名称应该以小写字母开头。完全取决于你的粗略,但如果你开始与其他程序员合作,当你开始时的好习惯将有助于进一步前进