Matt Gerrans
Posts: 1153
Nickname: matt
Registered: Feb, 2002
|
|
Re: Roulette
|
Posted: Mar 8, 2002 10:50 PM
|
|
Don't forget the square brackets around the java and /java tags.
As for you code, the problem is disguised by the indentation, somewhat, but it appears that the part that prints out the spin result is floating by itself and is not a part of the spin() method. Which means it is being treated as a static initializer, by the way. This is the code I'm referring to:
public static int spin()
{
int ballPosition = (int) (Math.random() * 36);
return ballPosition;
}
{
if (ballPosition%2 == 0)
color = 1;
else
color = 2;
System.out.println ("Your spin is: " + ballPosition + color);
}
I added indentation, which should further clarify the problem. Additionally, even removing the extra curlies to move the block into the method would not be quite enough; you also need to move the return statement to the end, so the result will be something like this:
public static int spin()
{
int ballPosition = (int) (Math.random() * 36);
if (ballPosition%2 == 0)
color = 1;
else
color = 2;
System.out.println ("Your spin is: " + ballPosition + color);
return ballPosition;
}
Sorry, but I have a little more bad news. There are 38, not 36 possible results on a roulette table. Two of them are neither black nor red, however, they are the special, usually green, values of 0 and 00. So, really the odds are stacked in favor of the house, as usual (of course). Additionally, it is not the case that all odds represent one color and evens the other. Here is the layout: Red: 1 3 5 7 9 12 14 16 18 19 21 23 25 27 30 32 34 36 Black: 2 4 6 8 10 11 13 15 17 20 22 24 26 28 29 31 33 35 Green (or "other"): 0 00 One way to deal with this would be to make an array in the Wheel class like this:
private final static int positions = {
GREEN, // 0
RED, // 1
BLACK, // 2
RED, // 3
BLACK, // 4
RED, // 5
BLACK, // 6
RED, // 7
BLACK, // 8
RED, // 9
BLACK, // 10
BLACK, // 11
RED, // 12
BLACK, // 13
RED, // 14
BLACK, // 15
RED, // 16
BLACK, // 17
RED, // 18
RED, // 19
BLACK, // 20
RED, // 21
BLACK, // 22
RED, // 23
BLACK, // 24
RED, // 25
BLACK, // 26
RED, // 27
BLACK, // 28
BLACK, // 29
RED, // 30
BLACK, // 31
RED, // 32
BLACK, // 33
BLACK, // 34
BLACK, // 35
RED, // 36
GREEN // 00
};
and have methods along these lines:
String getColor()
{
switch( positions[ballPosition] )
{
case BLACK: return "black";
case RED: return "red";
case GREEN: return "green";
}
}
This brings up another point. The spin() and other methods shouldn't be static. Instead, the Wheel should be an object, which has some state and methods for finding out about it (getColor()) and changing it (spin()). Also, as discussed in another recent post, your program may be more flexible if you don't print out results all over the place (in many different methods). In other words, main() should be able to glean all the information from the objects and print it out, otherwise all these different classes you are developing are tied directly to stdin and stdout. That may not seem to be a big deal now, but when you have to change this to be a graphical game using Swing, you will appreciate it.
|
|