Reputation: 21
I'm trying to make a simple java simulator horse race game using only arrays and loops. My program was nearly finished. My only problem is that when you enter the number of horses who will join in the contest, that particular number you enter will win even if other horses are finished. Example, when you type 5 horses that will join in the contest, that "5" number will win even if the other number finished first. i can't really determine the winner. My program seems to work well. Any advice would be appreciated. Thank You! Here's my code:
public static void main(String[] args) throws InterruptedException {
Scanner input = new Scanner(System.in);
int[] tracks = new int[70];
int bet;
System.out.println("==============");
System.out.println("||HORSE RACE||");
System.out.println("==============");
System.out.println("WHO'S GONNA WIN IN THIS EPIC RACE?");
System.out.println("ENTER HOW MANY HORSES WOULD YOU LIKE TO JOIN:"
+ "\n 2-10 HORSES are allowed to join!");
int horses;
do {
horses = input.nextInt();
} while (horses < 2 || horses > 10);
int[] move = new int[horses];
double[] betHorse = new double[horses];
System.out.println("Enter how many person will bet?");
int number = input.nextInt();
for (int i = 1; i <= number; i++) {
do {
for (int j = 1; j <= horses; j++) {
System.out.println("[" + j + "]" + " for HORSE " + j);
}
System.out.println("Person no." + i + ": Enter the number of horse:");
bet = input.nextInt();
} while (bet < 1 || bet > horses);
for (int p = 1; p <= horses; p++) {
if (bet == p) {
System.out.println("Enter the amount of your bet?");
betHorse[bet - 1] += input.nextDouble();
}
}
for (int j = 1; j <= horses; j++) {
System.out.println("Bet for HORSE " + j + ":P" + betHorse[j - 1]);
}
}
System.out.println("OKAY THAT'S SETTLED");
System.out.println("Race begins in:");
int num3 = 3;
for (int i = 1; i <= num3; num3--) {
System.out.println(num3);
Thread.sleep(1000);
}
do {
Thread.sleep(100);
int[] numbers = new int[horses];
for (int i = 0; i < horses; i++) {
numbers[i] = 1 + (int) (Math.random() * 6);
}
for (int i = 0; i < horses; i++) {
if (numbers[i] >= 1 && numbers[i] <= 3) {
move[i]++;
} else if (numbers[i] == 4 && numbers[i] == 5) {
move[i] = move[i] + 3;
} else if (numbers[i] == 6) {
move[i] = move[i] + 5;
}
}
System.out.println("\n\n\n");
for (int i = 1; i <= horses; i++){
System.out.println("Horse " + i +" position:" + move[i-1]);
}
for (int i = 1; i <= horses; i++) {
for (int j = 0; j < move[i - 1]; j++) {
System.out.print("--");
}
System.out.println(i + "H" + move[i - 1]);
}
} while (move[horses-1] < tracks.length );
for (int i = 1; i <= horses; i++) {
if (move[i - 1] > tracks.length) {
System.out.println("HORSE " + i + " finished the track! One who bets for HORSE " + i + " won P" + betHorse[i - 1] * 2);
}
}
}
}
Upvotes: 2
Views: 5126
Reputation: 34628
There are several problems with your simulation.
As @Eran said, you most serious problem is the termination condition. The loop stops when the horse in the horses - 1
position finishes, even if other horses were actually winning.
You may wonder why it was always winning, though. After all, your printing loop at the end should have seen another horse if the horses - 1
one finished but the other one finished first. This is probably because you actually gave all the horses very little chance to advance.
The horses have a 50% chance of advancing one step (numbers[i]
is between 1 and 3). Then they have a 33% of not advancing at all ( numbers[i]
is 4 or 5, but you asked for numbers[i] == 4 && numbers[i] == 5
, and it can't be both at the same time so this if is never entered). They have a 16% chance of advancing 5 steps. So most of the time, the other horses are advancing as much or less than the horses - 1
horse. If you change that condition to ||
instead of &&
, there are higher chances that you'll see another winning horse even if you don't correct your while condition (but you should correct it, of course).
That loop at the end is really not necessary. When you keep your ended
boolean, you can also keep an int winner
and set it to the i
of the horse that finished. Then you don't have to loop, just give the results for that particular winner
.
Your tracks
variable is set to be an array, but you are not actually using it. You are only using the moves
array for the horses. So you should really just keep a simple int
that says what the length of the track is, because that's the only thing that's interesting you - have the horses made enough moves to cover the whole length of the track?
You also don't need the numbers
array. You can loop on the horses, and roll a single number for the current horse, and make your decision based on that number.
for (int i = 0; i < horses; i++) {
int die = 1 + (int)(Math.random()*6);
if (die >= 1 && die <= 3) {
move[i]++;
} else if (die == 4 || die == 5) { // Note the || condition
move[i] = move[i] + 3;
} else { // Note that 6 is the only possibility remaining
move[i] = move[i] + 5;
}
}
The most important things are the loop condition and the ||
, of course.
Upvotes: 0
Reputation: 393811
The condition in your while loop :
while (move[horses-1] < tracks.length)
means that the loop will end once the last horse (whose index is horses-1
) finishes. You should change the condition to end the loop when any horse finishes.
Whenever you update move[i]
, you should test if move[i]>=tracks.length
, and if it is, set some boolean variable to true - ended = true;
.
Then change the loop's condition to while (!ended)
.
Upvotes: 4