| CPSC 124 | Introduction to Programming | Spring 2008 |
In this lab, we will continue working with methods.
Create a lab07 directory in your cs124 directory to hold the files for this lab.
Copy the file from /classes/s08/cs124/labs/lab07/ to your lab07 directory.
Here are the exercises for this week's lab, due in two weeks (3/27) because of spring break. You should continue to follow the good programming style rules from lab #2. Remember to indent statements contained in 'if' statements and loops. Also, remember to comment your code and to use reasonable variable names. Note: points will be taken off for poor indentation, uncommented code, or poor variable naming.
Write a program Number.java that reads in a number n (greater than or equal to 2) from the user and computes various properties of that number. Your program will print the primes between 2 and n, whether n is a square, and how many digits n has. For example, below are two runs of the program, the first where the user enters 100 for n and the second where the user enters 10 for n (user input is bold):
bash$ java Number Enter a number >= 2: 100 Primes between 2 and n: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 100 is a square. 100 has 3 digits. bash$ java Number Enter a number >= 2: 10 Primes between 2 and n: 2 3 5 7 10 is not a square. 10 has 2 digits.
You will write methods to help with each sub-component of this problem as enumerated below:
Feel free to write the methods above in any order that you like.
The game of nim is a two-player game, in which some number of beans are laid out and the players alternate turns in which a player can pick up one, two, or three beans. The winner is the player who picks up the last bean. (There are many variations on this game; perhaps you've heard of another version.)
If you write pseudocode for this version of nim, you may end up with something like the following:
get player 1's name from the user
get player 2's name from the user
get the number of beans to start with from the user
while there are beans left
display the number of beans remaining
get the number of beans player 1 wants to pick up
while player 1 has picked an invalid number of beans
remind player 1 of the rules for picking up beans
get the number of beans player 1 wants to pick up
if player 1 has won the game
print out a congratulatory message
else
display the number of beans remaining
get the number of beans player 2 wants to pick up
while player 2 has picked an invalid number of beans
remind player 2 of the rules for picking up beans
get the number of beans player 2 wants to pick up
if player 2 has won the game
print out a congratulatory message
Now, you may notice that the pseudocode for player 1's turn is very similar to the pseudocode for player 2's turn - the only thing that is different is which player is playing. This should suggest creating a parameterized method which carries out one turn.
Your task is to write a program called Nim.java which allows two players to play nim.
You should first write a method called turn() which handles one turn - it should take the player's name and the number of beans left as parameters, and should return the number of beans the player has picked up. The method should prompt the player to enter the number of beans to pick up, and should make her keep choosing a number of beans until a valid number is picked up. In pseudocode:
display the number of beans remaining
get the number of beans the player wants to pick up
while the player has picked an invalid number of beans
remind the player of the rules for picking up beans
get the number of beans the player wants to pick up
Keep in mind that a player can't pick up more beans than are available - picking up two beans when only one is left shouldn't be allowed.
Next, write a main program which uses your method to play a full game of nim, as specified by the pseudocode given above. Use call(s) to your take-a-turn method where appropriate.
Output from a sample run of the program is shown below. (User input is shown in bold.) Your program's output doesn't have to match this exactly, but your output should be nicely formatted and players should always be referred to by name.
Player 1, enter your name: Arthur Player 2, enter your name: Ford How many beans to start? 21 Arthur, how many beans to pick up? (21 left) 3 Ford, how many beans to pick up? (18 left) 0 number of beans must be 1, 2, or 3 Ford, how many beans to pick up? 2 Arthur, how many beans to pick up? (16 left) 4 number of beans must be 1, 2, or 3 Arthur, how many beans to pick up? 3 Ford, how many beans to pick up? (13 left) 2 Arthur, how many beans to pick up? (11 left) 3 Ford, how many beans to pick up? (8 left) 2 Arthur, how many beans to pick up? (6 left) 2 Ford, how many beans to pick up? (4 left) 2 Arthur, how many beans to pick up? (2 left) 3 can't pick up more beans than are left Arthur, how many beans to pick up? 2 Arthur, you win!
In Nim, depending on the number of starting beans, either the first player can always win or the second player can always win. As an exercise, see if you can figure out why and under what circumstances the first player can always win and under what circumstances the second player can always win.
Create a new version of nim called Nim2.java - leave your solution to problem #2 intact - which allows a human player to choose whether she wants to play against another human player or against the computer. When playing against the computer, the computer should always pick up a random number of beans.
To start, copy the code from Nim.java into Nim2.java. Then write a new method called computerTurn(), which is called whenever it is the computer's turn to play. It should take as a parameter the total number of beans remaining. When randomly picking the beans, it should not violate the rules of Nim, e.g., it can only grab 1 or 2 beans if there are only 2 beans left (although a smarter computer player would always take 2 beans in this case). It should return the number of beans it picked up so that the human player can be informed of how many beans were picked up and so that the total beans can be updated.
You will also need to modify your main method to support both 1 player mode and 2 player mode. At the beginning of the program, main should prompt the user for 1 player mode or 2 player mode. In 2 player mode, the program works as problem #2. In 1 player mode, the program calls computerTurn() rather than turn() for player 2.
Feel free to create additional methods, beyond computerTurn(), should you need them.Verify that your lab07 folder contains all of the files you created or modified for this lab (which should include Number.java, Nim.java, Nim2.java, and CopyFile.java), then copy your entire lab07 folder to the handin directory ~mcorliss/handin/cs124/username (where username is replaced with your username).