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:
if you need my other classes posted for this program which are the Dice class and the CrapsTester class, let me know. They are correct and they worked with the previous code i used in the Craps class.
I hope i sounded clear.
Thanks in advance.
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:
Code:
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 void play() //trying to replace below code, my attempt starts at the //end of the next comments.
/*{
firstRoll = nextSum();
if (firstRoll == 7 || firstRoll == 11)
{
gamesWon++;
lastGameWon = true;
}else
if (firstRoll == 2 || firstRoll == 3 || firstRoll == 12 )
{
lastGameWon = false;
}else{
secondRoll = nextSum();
if (secondRoll == firstRoll)
{
gamesWon++;
lastGameWon = true;
}else{
lastGameWon = false;
}
}
}
public int nextSum(){
dice1.roll();
dice2.roll();
return dice1.getSideUp() + dice2.getSideUp();
}*/
{
// look over this part carefully please, not sure if what i'm doing is wrong or not.
// if i can use less if and else statements, but i must use a while loop.
firstRoll = nextSum();
if (firstRoll == 7 || firstRoll == 11)
{
gameOver = true;
gamesWon++;
}
else if (firstRoll == 2 || firstroll == 3 || firstRoll == 12)
{
gameOver = true;
}
else{
gameOver = false;
}
while (gameOver = false)
{
dice1.roll();
dice2.roll();
return dice1.getSideUp() + dice2.getSideUp();
}
public String toString()// xcode keeps saying illegal start of expression
{
return "games - " + gamesPlayed + ", won - " + gamesWon + ", last game won - " + lastGameWon + " First Roll - " + firstRoll +
", Second Roll - " + secondRoll;
}
} // ; expected error here any reason why???
}
if you need my other classes posted for this program which are the Dice class and the CrapsTester class, let me know. They are correct and they worked with the previous code i used in the Craps class.
I hope i sounded clear.
Thanks in advance.