Difference between revisions of "Roulette"
m |
m |
||
(5 intermediate revisions by the same user not shown) | |||
Line 18: | Line 18: | ||
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> | 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> | <br> | ||
− | Run this game 20 times and print the output (money of the player) using [[JOptionPane_Methods|JOptionPane | + | Run this game 20 times and print the output (money of the player) using [[JOptionPane_Methods|JOptionPane]] method.<br> |
|SolutionCode= | |SolutionCode= | ||
Line 77: | Line 77: | ||
|Solution=First, analyze the question and determine the structure and the types of variables you will need.<br> | |Solution=First, analyze the question and determine the structure and the types of variables you will need.<br> | ||
It tells you to generate random numbers. <br> | It tells you to generate random numbers. <br> | ||
− | You will need to import < | + | You will need to import <b>''Math''</b> class which is in <b>''java.lang''</b>. <br> |
− | Also, you will need swing for printing out the money using < | + | Also, you will need swing for printing out the money using <b>''JOptionPane''</b>.<br> |
<br> | <br> | ||
<pre> | <pre> | ||
Line 85: | Line 85: | ||
</pre> | </pre> | ||
<br> | <br> | ||
− | After importing < | + | 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 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> | 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> | ||
Line 120: | Line 120: | ||
</pre> | </pre> | ||
<br> | <br> | ||
− | The numbers are generated by < | + | The numbers are generated by <b>''Math.Random()''</b>, which are double type values. <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> | 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, you will need to put: <br> | However, if you want a random generator that generates integers between 1 - 99, you will need to put: <br> | ||
Line 128: | Line 128: | ||
</pre> | </pre> | ||
<br> | <br> | ||
− | The reason for this is, < | + | 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 | + | 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 the winner is. <br> | Now, once you generate the random numbers, you will need to use some conditions to check who the winner is. <br> | ||
Line 137: | Line 137: | ||
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 1st = Player, 2nd = Opponent, 3rd = player, 4th = Opponent, 5th = Player, 6th = Opponent. <br> | ||
− | We can find out whose turn it is by using | + | We can find out whose turn it is by using <b>'%'</b> which is mod in java. <br> |
If your loop starts from 0, then the player will take turns when the loop counter is even. <br> | If your loop starts from 0, then the player will take turns when the loop counter is even. <br> | ||
Here is the pseudo-code for the condition. <br> | Here is the pseudo-code for the condition. <br> | ||
Line 152: | Line 152: | ||
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> | ||
If the result was the player's turn then the player loses money, otherwise the player wins money. <br> | If the result was the player's turn then the player loses money, otherwise the player wins money. <br> | ||
− | + | Note that money -= 50 is equal to: <br> | |
<br> | <br> | ||
<pre> | <pre> | ||
Line 175: | Line 175: | ||
<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 to print out the amount of money the player has, use the < | + | 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> | ||
Line 181: | Line 181: | ||
</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
ProblemPlease read the following carefully. Write a complete Roulette program that prints out the total gained from player. First turn, the probability starts at 0.2. |
Mid-term Review | |
---|---|---|
SolutionFirst, analyze the question and determine the structure and the types of variables you will need. import java.lang.*; import javax.swing.*;
int money = 100;
for(int i=0; i<20; i++) { double probability is 0.2 }
for(int j=0; j<6, j++) { }
double probability = Math.random();
int random = (int)(Math.random() * 99)
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. money = money - 50;
if(j < 4) probability += 0.1 else probability += 0.25
JOptionPane.showMessageDialog(null, money);
| ||
Code |