trouble with while loop, not sure what to put in it in my project

I'm making a class to a simple command line craps game. I must use a while loop to implement craps rules. The rules that i was given are:

if your first roll is a 4, 5, 6, 8, 9, or 10, you roll again until either you get a 7 (you lose) or you get the number from your first roll. (i thought 7 or 11 wins, like in the next example)

yeah, thats right, not very good directions. The first directions i had for a different way of doing it using if and else statements were:

The player rolls the two dice.

If the sum of the resulting die values is 7 or 11, the player wins.

If the sum is 2, 3, or 12, the player loses.

If something else is rolled, the player has to roll again to determine the outcome.

If the sum of the second roll is the same as what the player rolled the first time, the player wins. Otherwise the player loses.

(You might get a pair of dice and play a few rounds to try it.)

here's my code that i have so far for my craps class, in the middle section, i have previous code that i used for if else statements, that is what im trying to replace:package games;

[code]

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()

{

firstRoll = nextSum();

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

{

gameOver =true;

gamesWon++;

}

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

{

gameOver =true;

}

else{

gameOver =false;

}

while (gameOver ==false)

{

secondRoll++;

}

}

public String toString()

{

return"games - " + gamesPlayed +", won - " + gamesWon +", last game won - " + lastGameWon +" First Roll - " + firstRoll +

", Second Roll - " + secondRoll;

}

}

i'm really confused on how to get the while loop will keep starting the game over if it is false, am i using the right approach? Also i have to use a while loop, its for a school project.

thanks.

[5555 byte] By [kevin123a] at [2007-11-26 16:47:57]
# 1

Then i guess you should design a recursive algorithm. I will not write the exact code since i do not have all of the ideas of your project but it should be like that:

while (!gameOver)

{

firstRoll = nextSum();

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

{

gameOver = true;

gamesWon++;

}

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

{

gameOver = true;

}

else{

play();

}

}

You see the play() function is called in itself ,until the statement is reached it is called again and again you can use this recursive algorithm of course you should modify it since i wrote it by not thinking all the concepts of your project.

Good luck.

canseverayberka at 2007-7-8 23:15:28 > top of Java-index,Java Essentials,Java Programming...
# 2

It should be something like this:

public void play() {

while (gameOver == false) {

//All of your code here...

}

}

Kaj

kajbja at 2007-7-8 23:15:28 > top of Java-index,Java Essentials,Java Programming...
# 3

The code you asked for could be:

package games;

public class Craps {

private Dice dice1;

private Dice dice2;

private int gamesPlayed;

private int gamesWon;

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 boolean lastGameWon(){

return lastGameWon;

}

public int nextSum()

{

dice1.roll();

dice2.roll();

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

}

public void play() {

gamesPlayed++;

firstRoll = nextSum();

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

{

gamesWon++;

lastGameWon = true;

}

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

{

lastGameWon = false;

}

else

{

secondRoll = nextSum();

if (firstRoll == secondRoll)

{

gamesWon++;

lastGameWon = true;

}

else

{

lastGameWon = false;

}

}

public String toString()

{

return "games - " + gamesPlayed + ", won - " + gamesWon + ", last game won - " + lastGameWon + " First Roll - " + firstRoll +

", Second Roll - " + secondRoll;

}

}

I'm not sure of craps rules, the code above makes a first roll and if game isn't won or lose at once (7 and 11 wins, 2,3 or 12 lose) a second roll is done. If the second roll is equals to the first made then the game is won else the game is lose. Is it right?

topfoxya at 2007-7-8 23:15:28 > top of Java-index,Java Essentials,Java Programming...