File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Craps Game Code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Craps Game Code" Watch "Craps Game Code" New topic
Author

Craps Game Code

Juan Villena
Greenhorn

Joined: Aug 27, 2011
Posts: 20
Hello everyone, i really need some help in this code "craps game"
thi is the error message i got when i compile, i think im doing somenthing wrong in my class Die
it remarks that public int getRoll() , illegal start of expression


any help
Thank in advance...

2 errors found:
File: C:\Users\sinner7uk\JavaProjects\Die.java [line: 6]
Error: C:\Users\sinner7uk\JavaProjects\Die.java:6: illegal start of expression
File: C:\Users\sinner7uk\JavaProjects\Die.java [line: 6]
Error: C:\Users\sinner7uk\JavaProjects\Die.java:6: ';' expected




*******************************

Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
constructor not closed(missing curly brace '}')
Math.random() takes no arguments

Line 29 variable roll2 not declared yet( Craps class)
you may use answer.equalsIgnoreCase("y) (line 48)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40060
    
  28
That is a confusing error message, but it has the same cause in most cases. I have added code tags; you should by now know how to use them. You also ought to indent your code correctly. I have written about that before. If you had correct indentation, you would see you are short of a }, so the compiler thinks the keyword public is inside a method or constructor. You are not allowed to use modifiers inside a method (a few are permitted, eg final). In fact, you appear to be trying to write a method inside a constructor, which is not permitted either.

By the way, you have some bad design in that Die class. The tiro variable ought to change every time you roll the Die. You can have it as a field, changed by the random number generator every roll.
Juan Villena
Greenhorn

Joined: Aug 27, 2011
Posts: 20
thanks man, it runs now, but i think it doesn not check the condition properly
because i run it and , it says: " you roled 8 you loose"


Harsha Smith wrote:constructor not closed(missing curly brace '}')
Math.random() takes no arguments

Line 29 variable roll2 not declared yet( Craps class)
you may use answer.equalsIgnoreCase("y) (line 48)
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

it is next to impossible to help you, if you don't post what your code currently looks like.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40060
    
  28
If you roll 3 + 6 = 9 then roll 4 + 4 = 8, you will lose because you haven’t reached your “point”
Juan Villena
Greenhorn

Joined: Aug 27, 2011
Posts: 20

Hello everyone, sorry i could not answer back right away, and thank you for your answers
I just changed the conditionals, using switch and char variables in order to campare , win = w, lose = l, continue = c.

* BUT It returns WRONG answers , when it is NOT a win or a lose MEANS, when it is continue.
* i would like you take a look at the second time when generate the numbers, i use different variable "ddado1" and "ddado2"
because if i continue with the originals "dado1" and "dado2" i got a error, saying that dado1 and dado2 are already define in main(java.lang.String[])

ANY HELP PLEASE.


import java.util.Scanner;
import java.lang.String;
import java.util.Random;
public class Craps
{
public static void main (String[] args)
{
Scanner keyboard = new Scanner(System.in);
String answer;
char stat = ' ';
char gamestat = ' ';
int roll2 = 0;
do
{
Die dado1 = new Die();//store roll1 in a new var1
Die dado2 = new Die();//store roll2 in a new var2

// my point V iqual the sum of the two rolls
int thePoint = (dado1.getRoll() + dado2.getRoll());
System.out.println();
System.out.print("You rolled "+thePoint+" . ");

switch (thePoint)
{
case 7:
case 11:
System.out.println("You Win !");// winner
stat = 'w';
gamestat = stat;
break;

case 2:
case 3:
case 12:
System.out.println("You Lose !");// loser
stat = 'l';
gamestat = stat;
break;

default:
stat = 'c';
gamestat = stat;
break;
}

while ( gamestat == 'c' )
{
Die ddado1 = new Die();//store roll1 in a new var1
Die ddado2 = new Die();//store roll2 in a new var2

roll2 = (ddado1.getRoll() + ddado2.getRoll());

if (roll2 == thePoint){
gamestat = 'w';
System.out.println("You Winnnnn !");
}else if (roll2 == 7){
gamestat = 'l';
System.out.println("You Looooose !");
}
}
*******************************************************
// random numbers class

mport java.util.Random;

public class Die
{
private int tiro;
private final static Random random = new Random();
public Die()
{
tiro =random.nextInt(6) + 1;
}
public int getRoll()
{
return tiro;
}
}



Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
It would help if you posted code that compiled and also that you posted it in code tags.
I'm not sure if the lineis the end of the do construction or a while loop within the do/while construction. Your newline positioning suggests the latter but the lack of a second which suggest the former.

Joanne
Juan Villena
Greenhorn

Joined: Aug 27, 2011
Posts: 20
Joanne Neal, thanks for respond,
sorry, the end of the do is:

}while(answer.equals("y") || answer.equals("y"));
}
}

And i think it is working now.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Juan Villena wrote:ANY HELP PLEASE...

Not related directly to your query, but a few observations:
1. An enum for your 'game state' might make it a lot more readable, eg:
2. I'm not quite sure why you think your Die needs to hold the result of its throw; the method could just return it:that way you use the same dice for the whole game (rather like the real thing). Just declare them outside the loop.

3. I'd suggest not calling the result of the throw 'thePoint'. In the actual game of craps, the "point" is only established in the COME_OUT phase, if the result of the roll was 4-6 or 8-10. Indeed, in a real game, there can be many "point"s.

4. The way you're determining win/loss is only true if your game assumes a 'pass' (or 'come', in a multi-player game) bet. The normal rule for a 'don't pass' bet is that 2 and 3 win, and 12 is a "push" (no money changes hands).

Too many mis-spent days in Vegas.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Juan Villena
Greenhorn

Joined: Aug 27, 2011
Posts: 20
thanks winston and everyone who help me, the game coding is resolved

JUan villena
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Craps Game Code