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;

}

}

[8897 byte] By [kevin123a] at [2007-11-26 16:51:33]
# 1
Your syntax for the For loop threw me a bit, as I prefer to have braces around the body of fors, ifs, and so on even if they're only one line.Is it because you're not resetting "gameOver" to "false" once it's been set to true once?
DavidKNa at 2007-7-8 23:19:16 > top of Java-index,Java Essentials,New To Java...
# 2
how would i reset the gameOver variable to false and where?thank you.
kevin123a at 2007-7-8 23:19:16 > top of Java-index,Java Essentials,New To Java...
# 3
Just before entering the while loop seems a decent place to me. Just stick a gameOver = false; line in there, the same as you'd normally do any variable assignment.
DavidKNa at 2007-7-8 23:19:16 > top of Java-index,Java Essentials,New To Java...
# 4

He's correct - but there's more ...

import java.util.*;

class Craps {

private Dice dice1;

private Dice dice2;

private int gamesPlayed;

private int gamesWon;

private int gamesLost;

private boolean lastGameWon;

private boolean gameOver;

private int firstRoll;

private int secondRoll;

public Craps() {

dice1 = new Dice(6);

dice2 = new Dice(6);

gamesPlayed = 0;

gamesWon = 0;

lastGameWon = false;

firstRoll = 1;

secondRoll = 2;

}

//returns firstroll

public int getFirstRoll(){

return firstRoll;

}

//returns secondroll

public int getSecondRoll(){

return secondRoll;

}

public int getGamesPlayed(){

return gamesPlayed;

}

public int getGamesWon(){

return gamesWon;

}

public int getGamesLost(){

return gamesLost;

}

public boolean lastGameWon(){

return lastGameWon;

}

public int nextSum()

{

dice1.roll();

dice2.roll();

return dice1.getSideUp() + dice2.getSideUp();

}

public void play(){

while (gameOver == false) {

gamesPlayed++;

firstRoll = nextSum();

if (firstRoll == 7 || firstRoll == 11)

{

gameOver = true;

gamesWon++;

lastGameWon = true;

}

if (firstRoll == 2 || firstRoll == 3 || firstRoll == 12)

{

gameOver = true;

gamesLost++;

lastGameWon = false;

}

//else{

// play();

//}

}

gameOver = false;

}

public String toString()

{

return "games - "+ getGamesPlayed()

+ ", won - "+ getGamesWon()

+ ", lost - " + getGamesLost()

+ ", last game won - " + lastGameWon()

+ " First Roll - "+ getFirstRoll();

// + ", Second Roll - "+ getSecondRoll();

}

}

class Dice {

private int numberSides;

private int sideUp;

private static Random rand = new Random();

public Dice(int sides) {

numberSides = sides;

sideUp = 1;

}

public int getNumberSides() {

return numberSides;

}

public int getSideUp() {

return sideUp;

}

public void roll() {

sideUp = rand.nextInt(numberSides) + 1;

}

public String toString() {

return "number of sides=" + numberSides + " side up is " + sideUp;

}

}

public class CrapsTester {

public static void 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<100; i++) {

craps3.play();

System.out.println(craps3);

}

int gamesWon = craps3.getGamesWon();

System.out.println("Games won " + gamesWon);

}

}

I took the liberty of placing all of the classes in one file - you'll need to back that out and add the package statements back in.

I got rid of the use of secondRoll, because you are not using it.

~Bill

abillconsla at 2007-7-8 23:19:16 > top of Java-index,Java Essentials,New To Java...
# 5
anyone know why i'm only getting two games lost though? with the above code. thanks
kevin123a at 2007-7-8 23:19:16 > top of Java-index,Java Essentials,New To Java...
# 6

disregard the reply i made about only returning 2 lost games.

but im getting a weird output for games played here it is:

i used

for (int i=0; i<100000; i++)

Your counter is correct.

games - 300714, won - 66489, lost - 33511, last game won - true First Roll - 7

Games won: 66489

shouldnt i get 100000 games played?

kevin123a at 2007-7-8 23:19:16 > top of Java-index,Java Essentials,New To Java...
# 7
never mind i figured out to put the gamesPlayed++ at the end of the loop instead of right after the while loop. thanks
kevin123a at 2007-7-8 23:19:16 > top of Java-index,Java Essentials,New To Java...
# 8

> never mind i figured out to put the gamesPlayed++ at

> the end of the loop instead of right after the while

> loop. thanks

Welcome. And yes, I stuck that in so that you'd see that you were not counting games neither won nor lost while the while loop was running. You've also probably figured out why I removed the recursive call to play(), right? If not, uncomment it and see what you get ;o)

~Bill

abillconsla at 2007-7-8 23:19:16 > top of Java-index,Java Essentials,New To Java...