Difference between revisions of "Rock Paper Scissors"

From CompSciWiki
Jump to: navigation, search
(EDIT: in third para, changed "opponet" to "opponent's")
(EDIT: in fourth para, changed "mulitply" to "multiply")
Line 20: Line 20:
 
</pre>
 
</pre>
  
Math.random() will generate a number between 0 and less than 1, if we mulitply it by 3 we will get either 0, 1, or 2 values before the decimal. We can use 0, 1, and 2 to represent rock, paper, and scissors. We don't need the decimal value so we will cast it as an int and store it in an int variable to cut the decimal values off. This tells us that the variable computer should be an int type variable (as well as the variable player).  
+
Math.random() will generate a number between 0 and less than 1, if we multiply it by 3 we will get either 0, 1, or 2 values before the decimal. We can use 0, 1, and 2 to represent rock, paper, and scissors. We don't need the decimal value so we will cast it as an int and store it in an int variable to cut the decimal values off. This tells us that the variable computer should be an int type variable (as well as the variable player).  
  
 
To make things easier to understand, use int variables to represent each of the three weapons by naming the variables with an assigned unique value.  
 
To make things easier to understand, use int variables to represent each of the three weapons by naming the variables with an assigned unique value.  

Revision as of 13:31, 8 April 2010

Back to the Program-A-Day homepage

Problem

Rock-Paper-Scissors is a game played by two players with a one in three chance of winning. The outcome of each round will result in the player to either win, lose, or draw. In this example, we're going to make a simulation of the game, Rock-Paper-Scissors, to play against the computer. During the execution of the program, the player may request a summary that prints the current score record. The score record includes the number of wins, losses, and draws, as well as the winning average (in percentage) and the player's highest winning streak.

 

...by the Students

Whenever you are having trouble planning or solving a problem, writing a program, debugging a program, or just analyzing code, try drawing it out on paper. Step through each line of code so you know what you want the program to do, and you know what the code is doing. When you refer back to the code in your program, use print statements and test your variables to make sure your getting the same results or the results that you should be getting. When you write or draw each step on paper (ex: arrays), this gives you a visual of what your program is doing. In the case you are fixing errors, you can compare and match your program with your diagrams to pin point where the errors occur.

Solution

Before we start programming, the first thing to do when solving a problem is understand the problem, break it down, and plan out how to solve the smaller problems (divide and conquer).

Lets start off by going over the rules in Rock Paper Scissors. In Rock Paper Scissors, a player plays against an opponent where both the players selects either rock, paper, or scissors as their weapon. At the same time, both players draw their selected weapon. The result of each round will result in win, lose, or draw depending on the player and their opponent's selected weapon. Rock beats scissors, scissors beats paper, and paper beats rock.

Now that we understand how the game is played, we can start breaking the problem down and figure out how to solve the problems. In rock paper scissors, there are two players, so in our program we need two variables (player and computer) to hold their chosen weapon. The player's choice will be obtained through the user's input and the opponent's will be determined by the computer. Because we want the computer to generate a unique choice for each round, we will use a random number generator to generate a random number. Each number represents either rock, paper, or scissors. As you may have noticed, there are only three different choices so our number generator must generate one of three numbers.

(int)(Math.random() * 3)

Math.random() will generate a number between 0 and less than 1, if we multiply it by 3 we will get either 0, 1, or 2 values before the decimal. We can use 0, 1, and 2 to represent rock, paper, and scissors. We don't need the decimal value so we will cast it as an int and store it in an int variable to cut the decimal values off. This tells us that the variable computer should be an int type variable (as well as the variable player).

To make things easier to understand, use int variables to represent each of the three weapons by naming the variables with an assigned unique value.

final int ROCK = 0;
final int PAPER = 1;
final int SCISSORS = 2;

ROCK will be represented by 0, PAPER will be represented by 1, and SCISSORS will be represented by 2. Note that these constants should never change at any time during our program so set them to be final.

Because we want to be able to keep track of the number of wins, loses, draws, average, and win streak, the easiest way to do this is to use an array to store the result of each round. The easiest way to do this is using the same number representation method we used earlier to represent the results of each round, 1 for win, 2 for lose, and 3 for draw. So we'll use an int array rounds[] to store the result of each round. Keep in mind that when we use an array, we need to initalize a maximum size before we can use it so pick a big random number as the array size (ex: 1000).

final int MAX = 1000;

int [] rounds = new int [MAX];

Since this game is played with one or more rounds, it is a good idea to group the algorithm needed to process each round into a method so we can just simply call that function whenever a round is to be executed. Create the method in such a way that it will return the result of each round so we can store it in the rounds[] array. Win will represented by the value 1, lose will represent by the vaue 2, and draw will be represented by the value 3. So in other words, each time a player agrees to play another round, we will simply call the method playRound() that returns an int value to be stored in rounds[] array.

In general, you should have a visual idea that looks like,

main method {

	loop (to get input from user) {
		if the player agrees to play another round {

			result[current round] = call playRound(); 

		}
	}
}

playRound() method {
	calculate win, lose, or tie;

	return an int value that represents win, lose, or draw (1,2, or 3 respectively);
}

Now that we have the basic layout of our program, we can start filling in the details.

To get input from the user, we'll use JOptionPane's input dialog,

input = JOptionPane.showInputDialog(null,"Message for the user goes here.");

Since we want the user to play multiple rounds, we'll use a loop to continue getting input from the user. There are two cases that we have to consider for the loop to be true.

Case 1: Because we have an array storing the result of each round, we have a limit of the number of rounds there can be during each execution of the program (the size of the array). we can't store results exceeding the size of the array. The easiest way to do this is use a counter to keep track of each round and make sure the count does not exceed the max size of the array.

Case 2: If the number of rounds is represented by max size of the array, that means the user MUST play that many times. If the number is too small, the game will end too early when the user wants to keep playing. If the number is too big, the user will get fustrated, bord, and want the game to end. To solve this, we'll give the user the choice to quit the game at any time.

Since we are taking input from the user, we can have the user to quit the game by simply typing in the command "quit".


On a side note, although we have numbers representing rock, paper, and scissors to be used in our algorithm, we should keep the program's communication level with the user at the english language level.

Code

Solution Code

Back to the Program-A-Day homepage