Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

macman2790

macrumors 6502a
Original poster
Sep 4, 2006
716
1
Texas
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:
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.
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
I'm sorry I don't have time to thoroughly look it over and understand it, but one thing that caught my eye was this:

Code:
while (gameOver = false)

It seems like you would probably want to use == for comparison here? as it is you're assigning false to gameOver each time through the loop.
 

mufflon

macrumors 6502
Sep 15, 2006
264
2
First of all, your while loop will only iterate once (improper use of the return statement)...


furthermore I found a more informative rule section here: clickety

The first roll of the dice in a craps round is called the "come out roll". The basic opening bet in craps, placed just before the come out roll, is called the "pass line bet". Pass line bets immediately win when the shooter's come out roll is 7 or 11, and lose when the come out roll is 2 (snake eyes), 3 (cross eyes), or 12 (box cars). If 4, 5, 6, 8, 9, or 10 is rolled on the come out roll, that number becomes "the point." The puck labeled "ON" shows that the point has been established and is located on the craps table over the box for the number rolled as the point: "4, 5, SIX, 8, NINE, or 10". The shooter now keeps rolling the dice until he rolls the point or 7 to end the round. If the shooter rolls the point first, a pass line bet wins. If, however, the shooter rolls a 7 first (i.e. "sevens out"), a pass line bet loses.

To end a round of craps and resolve pass line bets, the shooter must roll either the point or 7 after the come out roll. There's always the possibility that a craps game will go on all night if the shooter fails to roll the point or 7. Pass line bets can't win until the point is rolled, and can't lose until 7 is rolled. Because unresolved pass line bets may not be removed, players must wait for a roll of the point or 7 to determine the fate of their pass line bets. If you need to leave the game, your bet will be lost.




so make a linked list containing all rolls for that game, until the player wins or looses, when he wins/looses do gameswon++ or gameslost++ and clear the linked list (I suppose linkedlist is kinda optional though but might be useful if you want to print out the full series of rolls, I guess)...

... should really do the trick imo
 

macman2790

macrumors 6502a
Original poster
Sep 4, 2006
716
1
Texas
First of all, your while loop will only iterate once (improper use of the return statement)...


furthermore I found a more informative rule section here: clickety

The first roll of the dice in a craps round is called the "come out roll". The basic opening bet in craps, placed just before the come out roll, is called the "pass line bet". Pass line bets immediately win when the shooter's come out roll is 7 or 11, and lose when the come out roll is 2 (snake eyes), 3 (cross eyes), or 12 (box cars). If 4, 5, 6, 8, 9, or 10 is rolled on the come out roll, that number becomes "the point." The puck labeled "ON" shows that the point has been established and is located on the craps table over the box for the number rolled as the point: "4, 5, SIX, 8, NINE, or 10". The shooter now keeps rolling the dice until he rolls the point or 7 to end the round. If the shooter rolls the point first, a pass line bet wins. If, however, the shooter rolls a 7 first (i.e. "sevens out"), a pass line bet loses.

To end a round of craps and resolve pass line bets, the shooter must roll either the point or 7 after the come out roll. There's always the possibility that a craps game will go on all night if the shooter fails to roll the point or 7. Pass line bets can't win until the point is rolled, and can't lose until 7 is rolled. Because unresolved pass line bets may not be removed, players must wait for a roll of the point or 7 to determine the fate of their pass line bets. If you need to leave the game, your bet will be lost.

thanks, but i have to go by the initial rules given to me, its for a project.
 

macman2790

macrumors 6502a
Original poster
Sep 4, 2006
716
1
Texas
heres what i have now, it compiles and i get an error: Exception in thread "main" java.lang.NoClassDefFoundError: Games

Games has exited with status 1.

what does this mean?

heres the updated code:
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 int nextSum()
	{
	dice1.roll();
	dice2.roll();
	return dice1.getSideUp() + dice2.getSideUp();
	}

    public void 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;
				}
			}
any new ideas??? i'm a little confused if the while loop will keep starting the game over if it is false, am i using the right approach?
thanks.
 

mufflon

macrumors 6502
Sep 15, 2006
264
2
I think your tabulations are really odd btw:

you use the following:

Code:
if anything {
   moo moo
}
if anything 
{
   moo moo
}
if anything 
   {
   moo moo
}

try to be as strict about this as possible - makes your code easier to follow (I'm a believer in the first kind of formating but it's uyp to you)
 

mufflon

macrumors 6502
Sep 15, 2006
264
2
you should do it like this (psuedo code!):

private void nextGame() {
increase games played.
if first roll leads to loss: increase loss, return.
else:
while roll is neither a win nor loss roll again
if win->increase win, return, lastgamewon == true
if lose->increase lose, return, lastgamewon == false
end of method

private int roll() {
roll 2 dice, return the sum they are showing.
end of method


then to check on your class-wide globals:
private Dice dice1;
private Dice dice2;
private int gamesPlayed;
private int gamesWon;
private boolean lastGameWon;
private boolean gameOver;
private int firstRoll;
private int secondRoll;

why do you have a firstRoll and a secondRoll? Keeep it localy in your method. GameOver feels kinda unwanted as a global too - why keep local varaibles globaly?

Try to restructurize your problem - what do you need and what don't you? Easier to work with a structurized problem than one you haven't really planned beforehand - do some fiddling on paper - you'll surely get a grip of what to do after that.
 

macman2790

macrumors 6502a
Original poster
Sep 4, 2006
716
1
Texas
thanks guys, that was a lot of help. i still have one more question. I'm using a for loop to play 100,000 games of craps.
However its not working, the number of games won always is 1 or 0, which means that the game is stopping somehow. How do i modify it to make it keep going in the first class i showed you(second one in this example)?
Code:
   for (int i=0; i<100000; i++)     
   craps3.play();
        int gamesWon = craps3.getGamesWon();
        System.out.println("Games won " + gamesWon);

this is what i figured out to make it work:
Code:
 public void 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();
        }

    }
}
 

mufflon

macrumors 6502
Sep 15, 2006
264
2
make the gameover a local variable...

... and define it within play()...


... furthermore, why are you doing a callback to the function?

I've rewritten your code and this is how I would have implemented your latest code:

Code:
	private gamesWon = 0;
	private gamesLost = 0;

	/*
	/ public play class - insert number of games to play
	*/
	public void play(int x) {
		for(int i = 0; i < x; i++) {
			this.play();
		}
	}
	
	/*
	/ private play class - plays a games, increases gamesWon or gamesLost after game.
	*/
    private void play() {
		boolean play == true;
		while(play) {
			int nextRoll = this.roll();
			if(nextRoll == 7 || nextRoll == 11) {
				play == false;
				this.gamesWon++;
			} else if(nextRoll == 2 || nextRoll == 3 || nextRoll == 12) {
				play == false;
				this.gamesLost++;
			}
		}
	}
	
	/*
	/ generates 2 random 1-6 rolls which are added and returned.
	*/
	private int roll() {
		// value from 2 rolled dices
		return 0;
	}
 

aLoC

macrumors 6502a
Nov 10, 2006
726
0
Here is one way to do it...

Code:
class Game
{
    int roll()
    {
        double rand1 = Math.random();
        int d1 = 1 + (int)(11 * rand1);

        double rand2 = Math.random();
        int d2 = 1 + (int)(11 * rand2);
        
        return d1 + d2;
    }

    // Returns true for win, false for lose
    boolean playOne()
    {
        int firstRoll = roll();
        
        if ( firstRoll == 7 || firstRoll == 11 ) {
            return true; // won
        }
            
        if ( firstRoll == 2 || firstRoll == 3 || firstRoll == 12 ) {
            return false; // lost
        }   
        
        int subsRoll = 0;
        
        do {
            subsRoll = roll();      
        } while ( subsRoll != 7 && subsRoll != firstRoll );
        
        return (subsRoll == firstRoll);
    }   
    
    // Returns number of wins
    int playMany(int numGames)
    {
        int numWon = 0;
        for ( int i = 0; i < numGames; i++ ) {
            numWon += ( playOne() ? 1 : 0 );
        }
        return numWon;
    }
    
    public static void main(String argv[])
    {
        Game g = new Game();
        int numWins = g.playMany(100000);
        System.out.println(numWins + " wins");
        
        return; 
    }
    
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.