File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Craps Game Code

 
Juan Villena
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 287
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is next to impossible to help you, if you don't post what your code currently looks like.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you roll 3 + 6 = 9 then roll 4 + 4 = 8, you will lose because you haven’t reached your “point”
 
Juan Villena
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Juan Villena
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9461
49
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Juan Villena
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks winston and everyone who help me, the game coding is resolved

JUan villena
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic