我正在为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;
}
}
答案 0 :(得分:1)
它只是循环遍历所有的滚动和转弯,因为你没有告诉它不这样做。我建议将大部分代码移到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代码风格可以用一些刷新,例如,通常认为类名应该以大写字母开头,方法名称应该用小写字母这样的东西。如果你开始与其他程序员合作,完全取决于你开始时粗略但良好的习惯将有助于进一步走下轨道