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:

Input Validation

There are a few cases where your program should make sure that the user provides valid input:

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:

To submit:

  1. Upload PickUpSticks.java to the Project 1 Dropbox folder on Learn@UW.

Notes

To use the skeleton file:

  1. In Eclipse, create a new Java project (e.g., "Project1").
  2. Download the linked PickUpSticks.java file by right-clicking and choosing the "Save Link As..." option.
  3. 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.
  4. 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.