Back to the Program-A-Day homepage
Problem
Please read the following carefully, and Write a complete program Roulett that prints out
total gained from player.
Each game will be running for 6 turns. Player will take the first turn
followed by the opponent. You need to generate a double random number
each time that will represent the chance of getting shot.
If the chance is smaller than the probability, then the bullet is fired.
First turn, the probability starts at 0.2. After the first turn, you have to increase the probability.
It will increase 0.1 for first four times, then, it will increase 0.25 for the 5th and the 6th time.
If the player wins the player gets $ 50 dollars, otherwise the player loses $ 50 dollars.
The starting money of the player will be $ 100 dollars.
Example. 1st(player, 0.2), 2nd(opponent, 0.3), 3rd(player, 0.4), 4th(opponent, 0.5), 5th(player, 0.75), 6th(opponent, 1.0)
Print out the output (money of the player) using JOPtionPane.
|
|
...by students
Taken from http://www.flickr.com/photos/daniello/565304023/
An image or By Students section
|
Solution
The solution...
|
Code
Solution Code
import java.lang.Math.*;
import javax.swing.*;
public class Roulett
{
public static void main(String[] args)
{
boolean win = false;
int money = 100;
for(int i = 0; i < 20; i++)
{
double probability = 0.2;
for(int j = 0; j < 6; j++)
{
double chance = Math.random();
if(probability > chance)
{
if(j % 2 == 0)
{
money -= 50;
win = false;
}
else
{
money += 50;
win = true;
}
}
if(j < 4)
{
probability += 0.1;
}
else
{
probability += 0.25;
}
}
}
JOptionPane.showMessageDialog(null, "Player's money left: " + money);
}
}
|
Back to the Program-A-Day homepage