Difference between revisions of "Roulette"

From CompSciWiki
Jump to: navigation, search
m (int money is 100 changed to int money = 100;)
m
 
(18 intermediate revisions by 6 users not shown)
Line 3: Line 3:
 
|ProblemName=Roulette
 
|ProblemName=Roulette
  
|Problem= Please read the following ''carefully'', and write a complete program Roulette that prints out<br/>
+
|Problem= Please read the following carefully. Write a complete Roulette program that prints out the total gained from player.<br>
total gained from player.<br/>
+
<br>
<br/>
+
Each game will consist of six turns. <br>
Each game will be running for 6 turns. Player will take the first turn <br/>
+
The player will take the first turn, followed by the opponent. <br>
followed by the opponent. You need to generate a double random number <br/>
+
For each turn you will need to generate a double random number, which will represent the chance of getting shot. <br>
each time that will represent the chance of getting shot. <br/>
+
If the chance is smaller than the probability, then the bullet is fired.<br>
If the chance is smaller than the probability, then the bullet is fired.<br/>
+
First turn, the probability starts at 0.2. After the first turn, you have to increase the probability. <br/>
+
It will increase 0.1 for first four times, then, it will increase 0.25 for the 5th and the 6th time.<br/>
+
If the player wins the player gets $ 50 dollars, otherwise the player loses $ 50 dollars.<br/>
+
The starting money of the player will be $ 100 dollars.<br/>
+
<br/>
+
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)<br/>
+
<br/>
+
Run this game for 20 times, and print out the output (money of the player) using JOPtionPane.<br/>
+
  
|SolutionCode=<pre>import java.lang.Math.*;
+
First turn, the probability starts at 0.2. <br>
 +
After the first turn, you have to increase the probability. <br>
 +
The probability is increased by 0.1 for the first four turns, and increased by 0.25 for the 5th and the 6th turn.<br>
 +
If the player wins the player gets $50 dollars, otherwise the player loses $50 dollars.<br>
 +
The starting amount of money for the player is be $100 dollars.<br>
 +
<br>
 +
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)<br>
 +
<br>
 +
Run this game 20 times and print the output (money of the player) using [[JOptionPane_Methods|JOptionPane]] method.<br>
 +
 
 +
|SolutionCode=
 +
import java.lang.Math.*;
 
import javax.swing.*;
 
import javax.swing.*;
  
Line 67: Line 69:
 
     }
 
     }
 
}
 
}
</pre>
 
  
|SideSectionTitle=...by students
+
|SideSectionTitle=Mid-term Review
  
 
|SideSection=
 
|SideSection=
[[Image:Wiki_trays01.jpg|center]]
+
[[Image:Wiki_trays01.jpg|center]]<BR>
<BR>
+
  
|Solution=First, analyize what you need to import. It tells you to generate random numbers.<br/>
+
|Solution=First, analyze the question and determine the structure and the types of variables you will need.<br>
You will need to import Math class which is in java.lang. Also, you will need swing <br/>
+
It tells you to generate random numbers. <br>
for printing out the money using JOptionPane.<br/>
+
You will need to import <b>''Math''</b> class which is in <b>''java.lang''</b>. <br>
<br/>
+
Also, you will need swing for printing out the money using <b>''JOptionPane''</b>.<br>
 +
<br>
 
<pre>
 
<pre>
 
import java.lang.*;
 
import java.lang.*;
 
import javax.swing.*;
 
import javax.swing.*;
 
</pre>
 
</pre>
<br/>
+
<br>
After imporing java.lang.Math.Random and javax.swing.JOptionPane, figure out which variables you will need.<br/>
+
After importing <b>''java.lang.Math.Random''</b> and <b>''javax.swing.JOptionPane''</b>, figure out which variables you will need.<br>
<br/>
+
<br>
You will need the initial money which is 100 as integer.<br/>
+
You will need a variable for the player's initial money. Because this program does increments and decrements in non-decimal values, we will use integer. Initialize the starting amount at 100 for the starting amount.<br>
<br/>
+
<br>
 
<pre>
 
<pre>
 
int money = 100;
 
int money = 100;
 
</pre>
 
</pre>
<br/>
+
<br>
Then, you will need a for loop to run the games 20 times.<br/>
+
Then, you will need a for loop to run the games 20 times. <br>
Inside the for loop, you will need to declare another variable for the probability.<br/>
+
Inside the for loop, you will need to declare another variable for the probability. <br>
The probability will start from 0.2, so set the probability variable equal to 0.2.<br/>
+
The probability will start from 0.2, so set the probability variable equal to 0.2.<br>
<br/>
+
<br>
 
<pre>
 
<pre>
for(int i running 20 times, incremented by 1)
+
for(int i=0; i<20; i++)
 
{
 
{
 
double probability is 0.2
 
double probability is 0.2
 
}
 
}
 
</pre>
 
</pre>
<br/>
+
<br>
Once you set up the outer loop, you will need an inner loop for each game.<br/>
+
Once you set up the outer loop, you will need an inner loop for each game. <br>
There will 6 turns for each game which means the loop will run 6 times.<br/>
+
There will be 6 turns for each game which means the loop will run 6 times. <br>
<br/>
+
<br>
 
<pre>
 
<pre>
for(int j running 6 times, incremented by 1)
+
for(int j=0; j<6, j++)
 
{
 
{
 
}
 
}
 
</pre>
 
</pre>
<br/>
+
<br>
What you need to do inside the inner loop, you will need to generate random numbers.<br/>
+
What you need to do is inside the inner loop, you will need to generate random numbers. <br>
Random numbers can be generated by using<br/>
+
Random numbers can be generated by using <br>
<br/>
+
<br>
 
<pre>
 
<pre>
 
double probability = Math.random();
 
double probability = Math.random();
 
</pre>
 
</pre>
<br/>
+
<br>
The numbers are generated by Math.Random are double values.<br/>
+
The numbers are generated by <b>''Math.Random()''</b>, which are double type values. <br>
You need double values for chances, so it's ok to leave it by itself.<br/>
+
This function will generate a number between 0 and 1. Because this function generates decimal numbers, make sure you store it in a double type variable.<br>
However, if you want a random generator that generates integers between 1 - 99, <br/>
+
However, if you want a random generator that generates integers between 1 - 99, you will need to put: <br>
you will need to put: <br/>
+
<br>
<br/>
+
 
<pre>
 
<pre>
 
int random = (int)(Math.random() * 99)
 
int random = (int)(Math.random() * 99)
 
</pre>
 
</pre>
<br/>
+
<br>
The reason for this is, Math.random() will generate numbers between 0.0 < x < 1.0.<br/>
+
The reason for this is, <b>''Math.random()''</b> will generate numbers between 0.0 < x < 1.0. <br>
You will need to multiply by 99 to make the generate numbers 0.0 < x < 99.0.<br/>
+
You will need to multiply by 99 to make the generate numbers 0.0 < x < 99.0. <br>
Remember, you must ''CAST'' the random numbers to int if you will assign the value to int.<br/>
+
Remember, you must <b>CAST</b> the random numbers to int if you will assign the value to int. <br>
<br/>
+
<br>
Now, once you generate the random numbers, you will need to use some conditions to check who won.<br/>
+
Now, once you generate the random numbers, you will need to use some conditions to check who the winner is. <br>
As it says on the question, if the given probability is greater than <br/>
+
As stated in the question, if the given probability is greater than the randomly generated double value, there was gun fire. <br>
the randomly generated double value, there was gun fire. <br/>
+
<br>
<br/>
+
You will also need to consider whose turn it is. <br>
You will also need to consider whose turn it is.<br/>
+
The player takes first turn, for example 1st = Player, 2nd = Opponent, 3rd = player, 4th = Opponent, 5th = Player, 6th = Opponent. <br>
The player takes first turn, for example <br/>
+
We can find out whose turn it is by using <b>'%'</b> which is mod in java. <br>
1st = Player, 2nd = Opponent, 3rd = player, 4th = Opponent, 5th = Player, 6th = Opponent. <br/>
+
If your loop starts from 0, then the player will take turns when the loop counter is even. <br>
We can find out whose turn it is by using mod (%). <br/>
+
Here is the pseudo-code for the condition. <br>
If your loop starts from 0, then the player will take turns when the loop counter is even.<br/>
+
<br>
Here is the pseudo-code for the condition.
+
<br/>
+
 
<pre>
 
<pre>
 
if(probability > chance)
 
if(probability > chance)
Line 152: Line 150:
 
}
 
}
 
</pre>
 
</pre>
This pseudo-code means if there was a gun fire, check whose turn it is.<br/>
+
This pseudo-code means if there was a gun fire, check whose turn it is. <br>
Then if it was the player's turn, he loses money.<br/>
+
If the result was the player's turn then the player loses money, otherwise the player wins money. <br>
Otherwise, he wins money. <br/>
+
Note that money -= 50 is equal to: <br>
Note that money -= 50 is equal to:<br/>
+
<br>
<br/>
+
 
<pre>
 
<pre>
 
money = money - 50;
 
money = money - 50;
 
</pre>
 
</pre>
<br/>
+
<br>
After setting up the condition, we need to increase the probability.<br/>
+
After setting up the condition, we need to increase the probability. <br>
If there was no gun fire, another person needs to take the next turn.<br/>
+
If there was no gun fire, another person needs to take the next turn. <br>
For the first four times, it will increase by 0.1.<br/>
+
For the first four times, it will increase by 0.1. <br>
For the fifth and the sixth time, it will increase by 0.25.<br/>
+
For the fifth and the sixth time, it will increase by 0.25. <br>
<br/>
+
<br>
 
<pre>
 
<pre>
 
if(j < 4)  
 
if(j < 4)  
Line 172: Line 169:
 
probability += 0.25
 
probability += 0.25
 
</pre>
 
</pre>
<br/>
+
<br>
The code above is assuming that it starts from 0.<br/>
+
The code above does the proper increments when j starts at 0. <br>
j < 4 will be 0, 1, 2 and 3 which is four times and increase the probability by 0.1.<br/>
+
j < 4 will include turns 0, 1, 2 and 3, which is four times, and each turn will increase the probability by 0.1. <br>  
Otherwise, by 0.25 by setting else.<br/>
+
When j is greater than 4, the probability will be increased by 0.25 for the remaining turns(in the else statement). <br>
<br/>
+
<br>
After increasing the probability, we are done with loops.<br/>
+
After increasing the probability, we are done with loops. <br>
Now, the last step is printing out the money using simple JOptionPane method.<br/>
+
Now, the last step is to print out the amount of money the player has, use the <b>''JOptionPane''</b> method. <br>
<br/>
+
<br>
 
<pre>
 
<pre>
 
JOptionPane.showMessageDialog(null, money);
 
JOptionPane.showMessageDialog(null, money);
 
</pre>
 
</pre>
<br/>
+
<br>
 +
This problem was the review of 'Problem Solving with Nested Loops'. <br>
 
See below for the solution code.
 
See below for the solution code.
  
 
}}
 
}}

Latest revision as of 23:11, 10 April 2010

Back to the Program-A-Day homepage

Problem

Please read the following carefully. Write a complete Roulette program that prints out the total gained from player.

Each game will consist of six turns.
The player will take the first turn, followed by the opponent.
For each turn you will need to generate a double random number, which 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.
The probability is increased by 0.1 for the first four turns, and increased by 0.25 for the 5th and the 6th turn.
If the player wins the player gets $50 dollars, otherwise the player loses $50 dollars.
The starting amount of money for the player is 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)

Run this game 20 times and print the output (money of the player) using JOptionPane method.

 

Mid-term Review

Wiki trays01.jpg

Solution

First, analyze the question and determine the structure and the types of variables you will need.
It tells you to generate random numbers.
You will need to import Math class which is in java.lang.
Also, you will need swing for printing out the money using JOptionPane.

import java.lang.*;
import javax.swing.*;


After importing java.lang.Math.Random and javax.swing.JOptionPane, figure out which variables you will need.

You will need a variable for the player's initial money. Because this program does increments and decrements in non-decimal values, we will use integer. Initialize the starting amount at 100 for the starting amount.

int money = 100;


Then, you will need a for loop to run the games 20 times.
Inside the for loop, you will need to declare another variable for the probability.
The probability will start from 0.2, so set the probability variable equal to 0.2.

for(int i=0; i<20; i++)
{
	double probability is 0.2
}


Once you set up the outer loop, you will need an inner loop for each game.
There will be 6 turns for each game which means the loop will run 6 times.

for(int j=0; j<6, j++)
{
}


What you need to do is inside the inner loop, you will need to generate random numbers.
Random numbers can be generated by using

double probability = Math.random();


The numbers are generated by Math.Random(), which are double type values.
This function will generate a number between 0 and 1. Because this function generates decimal numbers, make sure you store it in a double type variable.
However, if you want a random generator that generates integers between 1 - 99, you will need to put:

int random = (int)(Math.random() * 99)


The reason for this is, Math.random() will generate numbers between 0.0 < x < 1.0.
You will need to multiply by 99 to make the generate numbers 0.0 < x < 99.0.
Remember, you must CAST the random numbers to int if you will assign the value to int.

Now, once you generate the random numbers, you will need to use some conditions to check who the winner is.
As stated in the question, if the given probability is greater than the randomly generated double value, there was gun fire.

You will also need to consider whose turn it is.
The player takes first turn, for example 1st = Player, 2nd = Opponent, 3rd = player, 4th = Opponent, 5th = Player, 6th = Opponent.
We can find out whose turn it is by using '%' which is mod in java.
If your loop starts from 0, then the player will take turns when the loop counter is even.
Here is the pseudo-code for the condition.

if(probability > chance)
{
	if(j % 2 == 0)
		money -= 50
	else
		money += 50
}

This pseudo-code means if there was a gun fire, check whose turn it is.
If the result was the player's turn then the player loses money, otherwise the player wins money.
Note that money -= 50 is equal to:

money = money - 50;


After setting up the condition, we need to increase the probability.
If there was no gun fire, another person needs to take the next turn.
For the first four times, it will increase by 0.1.
For the fifth and the sixth time, it will increase by 0.25.

if(j < 4) 
	probability += 0.1
else
	probability += 0.25


The code above does the proper increments when j starts at 0.
j < 4 will include turns 0, 1, 2 and 3, which is four times, and each turn will increase the probability by 0.1.
When j is greater than 4, the probability will be increased by 0.25 for the remaining turns(in the else statement).

After increasing the probability, we are done with loops.
Now, the last step is to print out the amount of money the player has, use the JOptionPane method.

JOptionPane.showMessageDialog(null, money);


This problem was the review of 'Problem Solving with Nested Loops'.
See below for the solution code.

Code

Solution Code

Back to the Program-A-Day homepage