help with a while loop
hi i need to use a while loop in a simple command line craps game that i'm making for school. i cant get the program to execute this more than once, i input 100000 times to play, and it only comes out with one win. the play method is where i'm stuck.
package games;
publicclass Craps{
private Dice dice1;
private Dice dice2;
privateint gamesPlayed;
privateint gamesWon;
privateboolean lastGameWon;
privateboolean gameOver;
privateint firstRoll;
privateint secondRoll;
public Craps(){
dice1 =new Dice(6);
dice2 =new Dice(6);
gamesPlayed = 0;
gamesWon = 0;
lastGameWon =false;
firstRoll = 1;
secondRoll = 2;
}
//returns firstroll
publicint getFirstRoll(){
return firstRoll;
}
//returns secondroll
publicint getSecondRoll(){
return secondRoll;
}
publicint getGamesPlayed(){
return gamesPlayed;
}
publicint getGamesWon(){
return gamesWon;
}
publicboolean lastGameWon(){
return lastGameWon;
}
publicint nextSum()
{
dice1.roll();
dice2.roll();
return dice1.getSideUp() + dice2.getSideUp();
}
publicvoid play(){
while (gameOver ==false)
{
firstRoll = nextSum();
if (firstRoll == 7 || firstRoll == 11)
{
gameOver =true;
gamesWon++;
lastGameWon =true;
}
if (firstRoll == 2 || firstRoll == 3 || firstRoll == 12)
{
gameOver =true;
lastGameWon =false;
}
else{
play();
}
}
}
public String toString()
{
return"games - " + gamesPlayed +", won - " + gamesWon +", last game won - " + lastGameWon +" First Roll - " + firstRoll +
", Second Roll - " + secondRoll;
}
}
here is what i'm using to test it, i also have a dice class.
package games;
publicclass CrapsTester{
publicstaticvoid main(String[] args){
System.out.println("This lab was written by Kevin Brown");
Craps crapsgame =new Craps();
Craps craps2 =new Craps();
Craps craps3 =new Craps();
/*for(int i=0; i<10; i++){
crapsgame.play();
System.out.println(crapsgame);
*/
System.out.println("");
for (int i=0; i<4; i++)
{
crapsgame.play();
System.out.println(crapsgame);
}
System.out.println("");
int counter = 0;
int counter2 = 0;
craps2.play();
if (craps2.lastGameWon() ==true)
counter++;
craps2.play();
if (craps2.lastGameWon() ==true)
counter++;
craps2.play();
if (craps2.lastGameWon() ==true)
counter++;
craps2.play();
if (craps2.lastGameWon() ==true)
counter++;
craps2.play();
if (craps2.lastGameWon() ==true)
counter++;
// checks to see if your counter is working right
if (counter == craps2.getGamesWon());
System.out.println("Your counter is correct.");
for (int i=0; i<100000; i++)
craps3.play();
int gamesWon = craps3.getGamesWon();
System.out.println("Games won " + gamesWon);
}
}
here is the dice class.
package games;
import java.util.*;
publicclass Dice{
privateint numberSides;
privateint sideUp;
privatestatic Random rand =new Random();
public Dice(int sides){
numberSides = sides;
sideUp = 1;
}
publicint getNumberSides(){
return numberSides;
}
publicint getSideUp(){
return sideUp;
}
publicvoid roll(){
sideUp = rand.nextInt(numberSides) + 1;
}
public String toString(){
return"number of sides=" + numberSides +" side up is " + sideUp;
}
}

