0

私は、モバイル アプリ クラスの Yahtzee プログラムに取り組んでおり、問題が発生しています。私が書いたループは、onClick() が 1 回だけ押された場合でも、ループ (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;
    }
}
4

1 に答える 1

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コードスタイルがいくつかのブラッシュアップでできることも指摘する価値があると思います。たとえば、クラス名は大文字で始まり、メソッド名は小文字で始まることが一般的に受け入れられています。粗いものは完全にあなた次第ですが、始めたばかりの良い習慣は、他のプログラマーとの共同作業を開始した場合に、さらに先に進むのに役立ちます。

于 2012-06-10T22:59:30.597 に答える