java - Dice Emulation - onClickListener in loop -


i'm working on yahtzee program mobile app class, , running trouble. loop have written run through loop (13 turns , 3 rolls) when onclick() pressed once. have moved them several different orders can't seem right. guide me in right direction have onclick accurately keep tally of turns , rolls?

code

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 activity 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 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;         (int = 0; < max_turns; i++) {              int rollnum = 0;             (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)     {         (int = 0; < dice.length; i++) {             if (!isheld[i]) {                 int rndint = rand.nextint(6) + 1; // random number between 1 , 6                           string imgname = "die" + rndint;                                     int id = getresources().getidentifier(imgname, "drawable", getpackagename());                    dicevalue[i] = rndint;                 dice[i].setimageresource(id);  //loops through dice array , sets appropriate dice images based on individual randoms             } else {                 //do nothing                                     }         }         return dicevalue;     } } 

it looping through rolls , turns because don't tell otherwise. suggest moving of code rolldice(...). in order below example work, need make rollnum, turnnum, max_turns, max_rolls instance variables instead of local variable eg:

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; 

and remove turn code playgame , put in rolldice so:

public int[] rolldice(imagebutton [] dice, int [] dicevalue, boolean [] isheld, random rand) {     (int = 0; < dice.length; i++) {         if (!isheld[i]) {             int rndint = rand.nextint(6) + 1; // random number between 1 , 6                       string imgname = "die" + rndint;                                 int id = getresources().getidentifier(imgname, "drawable", getpackagename());                dicevalue[i] = rndint;             dice[i].setimageresource(id);  //loops through dice array , sets appropriate dice images based on individual randoms         } else {             //do nothing                                 }     }      rollnum ++;     if(rollnum >= max_rolls){         //turn on         turnnum ++;         scoredice();     else {         roll.settext("roll (" + (max_rolls - rollnum) + " remaining)");     }      return dicevalue; } 

i think worth pointing out java code style brushing up, example accepted class names should start upper case letter , method names lower case letter things that. of coarse, habits when beginning further down track if start collaborating other programmers


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -