Project 1: Pick Up Sticks
Due: Friday, June 26 by 5:00pm
Sunday, June 28 by 5:00pm
Overview
This project has several goals:
- Develop and implement a main program from high-level specifications
- Practice using if, while and other Java control statements
- Practice using the Scanner class for input
- Practice using System.out.print and System.out.println for output
- Learn how to validate user input
- Learn to use proper style and comments
In this project, you will build a simple game for two players.
In this game, there is a heap of sticks on the board. On their turn, each
player picks up 1 to 3 sticks. The player who picks up the final stick loses.
You will work on this project individually.
Details
Start by creating a new project in Eclipse and copying this code skeleton to your
project folder. See Notes at the bottom for more details on how to do this. The TODO items in the code skeleton will help guide you
through this project.
When launched, this game provides a brief overview of the rules, and then
provides the user with three options: PLAY, HELP, EXIT.
Welcome to Pick Up Sticks.
In this game, there is a pile of sticks on the board.
On each turn, the current player may pick up 1-3 sticks.
The goal is to not pick up the last stick.
Options: PLAY, HELP, EXIT. Enter your choice:
Handling each option:
-
EXIT:
The game displays "Good game!" and terminates.
Options: PLAY, HELP, EXIT. Enter your choice: EXIT
Good game!
-
HELP:
The game displays the rules and then prompts the user for another option.
Options: PLAY, HELP, EXIT. Enter your choice: HELP
On each turn, the current player may pick up 1-3 sticks.
The goal is to not pick up the last stick.
Options: PLAY, HELP, EXIT. Enter your choice:
-
PLAY:
This is where the game actually gets played! First, each user enters their name:
Enter player 1's name: Emily
Enter player 2's name: Mason
Next, they enter the number of sticks on the board:
How many sticks on the board? 10
Finally, the game begins. The game alternates prompting player 1 and player 2 for
the number of sticks they want to pick up. The game ends when a user picks up, or is
forced to pick up, the last stick. The game then displays the winner and prompts
the user for another option. Note that if only one stick remains, the game ends; the
player that would have picked up this stick is not prompted to pick up the last stick.
There are 10 sticks on the board.
Emily, how many do you pick up? 3
There are 7 sticks on the board.
Mason, how many do you pick up? 1
...
There are 2 sticks on the board.
Emily, how many do you pick up? 1
Mason picks up the last stick.
Emily wins!
Options: PLAY, HELP, EXIT. Enter your choice:
If the game is played multiple times in a row (i.e., during one execution of
PickUpSticks), users have the option of keeping the same players as last time.
Options: PLAY, HELP, EXIT. Enter your choice: PLAY
...
Options: PLAY, HELP, EXIT. Enter your choice: PLAY
Same players (y/n)?
Input Validation
There are a few cases where your program should make sure that the user
provides valid input:
- The user must pick from the options PLAY, HELP, EXIT.
- The starting number of sticks on the board must be a positive integer.
- The number of sticks picked up must be an integer in the range [1,3].
- A player cannot pick up more sticks than remain on the board.
If the user chooses an invalid option, the game should print an error
message and re-prompt the user:
Options: PLAY, HELP, EXIT. Enter your choice: asdf
Option not recognized.
Options: PLAY, HELP, EXIT. Enter your choice:
If either of the numeric cases is not met, the game should print an error message
and re-prompt the user:
How many sticks on the board? -1
-1 is not a valid # of sticks. Try again.
How many sticks on the board?
...
There are 2 sticks on the board.
Emily, how many do you pick up? 3
3 is not a valid # of sticks. Try again.
Emily, how many do you pick up?
Sample Output
Sample Output 1
Welcome to Pick Up Sticks.
In this game, there is a pile of sticks on the board.
On each turn, the current player may pick up 1-3 sticks.
The goal is to not pick up the last stick.
Options: PLAY, HELP, EXIT. Enter your choice: HELP
On each turn, the current player may pick up 1-3 sticks.
The goal is to not pick up the last stick.
Options: PLAY, HELP, EXIT. Enter your choice: PLAY
Enter player 1's name: Emily
Enter player 2's name: Mason
How many sticks on the board? 4
There are 4 sticks on the board.
Emily, how many do you pick up? 2
There are 2 sticks on the board.
Mason, how many do you pick up? 1
Emily picks up the last stick.
Mason wins!
Options: PLAY, HELP, EXIT. Enter your choice: EXIT
Good game!
Sample Output 2
Welcome to Pick Up Sticks.
In this game, there is a pile of sticks on the board.
On each turn, the current player may pick up 1-3 sticks.
The goal is to not pick up the last stick.
Options: PLAY, HELP, EXIT. Enter your choice: PLAY
Enter player 1's name: Emily
Enter player 2's name: Mason
How many sticks on the board? -1
-1 is not a valid # of sticks. Try again.
How many sticks on the board? 4
There are 4 sticks on the board.
Emily, how many do you pick up? -1
-1 is not a valid # of sticks. Try again.
Emily, how many do you pick up? eggs
eggs is not a valid # of sticks. Try again.
Emily, how many do you pick up? 5
5 is not a valid # of sticks. Try again.
Emily, how many do you pick up? 2
There are 2 sticks on the board.
Mason, how many do you pick up? 3
3 is not a valid # of sticks. Try again.
Mason, how many do you pick up? 2
Mason picks up the last stick.
Emily wins!
Options: PLAY, HELP, EXIT. Enter your choice: EXIT
Good game!
Sample Output 3
Welcome to Pick Up Sticks.
In this game, there is a pile of sticks on the board.
On each turn, the current player may pick up 1-3 sticks.
The goal is to not pick up the last stick.
Options: PLAY, HELP, EXIT. Enter your choice: PLAY
Enter player 1's name: Emily
Enter player 2's name: Mason
How many sticks on the board? 1
Emily picks up the last stick.
Mason wins!
Options: PLAY, HELP, EXIT. Enter your choice: PLAY
Same players (y/n)? y
How many sticks on the board? 2
There are 2 sticks on the board.
Emily, how many do you pick up? 1
Mason picks up the last stick.
Emily wins!
Options: PLAY, HELP, EXIT. Enter your choice: PLAY
Same players (y/n)? n
Enter player 1's name: Harry
Enter player 2's name: Sally
How many sticks on the board? 1
Harry picks up the last stick.
Sally wins!
Options: PLAY, HELP, EXIT. Enter your choice: EXIT
Good game!
What to Turn In
Before you submit your work, check the following:
- Does your program match the sample output?
- Does your program validate user input before using it?
- Is your source file named PickUpSticks.java?
- Is the entirety of your program in a single class, PickUpSticks,
and in a single main method inside this class?
- If you worked on your own computer, did you verify that your program works correctly
on the lab computer?
- Did you follow the style and commenting guidelines?
To submit:
- Upload PickUpSticks.java
to the Project 1 Dropbox folder on Learn@UW.
Notes
To use the skeleton file:
- In Eclipse, create a new Java project (e.g., "Project1").
- Download the linked PickUpSticks.java file by right-clicking and choosing the "Save
Link As..." option.
- Move the downloaded file into the Project1 folder on your hard drive.
This folder is located inside the workspace you specified when you
launched Eclipse, e.g., C:\Users\<username>\cs302\workspace from the
Eclipse tutorial.
- In Eclipse, in the Package Explorer window, right click on Project1 and
select "Refresh." You should now see PickUpSticks.java inside the
project in Eclipse.
Adapated from http://nifty.stanford.edu/2014/laaksonen-vihavainen-game-of-sticks by Antti Laaksonen and Arto Vihavainen.