Scott Grissom
School of Computing & Info Systems
Grand Valley State University
picture

Project 2: Pig

Preparation

Do the following before starting this project:

  • Read chapter 3
  • Review all of the chapter exercises for practice
  • Due at the start of lab

Objectives

After completing this project, you should be able to:

  • write a Java class that interacts with other objects
  • use conditional statements to solve a problem
  • use while loops to solve a problem
  • invoke internal methods

Project Specification

Pig is a dice game played with two or more players. Your assignment is to write an application that allows one person to play against the computer.

Getting Started

We are providing a BlueJ project to get you started.

  1. Connect to the CSIS R: drive
  2. Start Blue J and open the Pig project located on the R: drive in the CS 162 folder
  3. Save this project to your N: drive
  4. You will modify the existing Pig class and disregard the provided GUI class for now. You will use the existing GVdice class but will not have to make any changes to it.
Game Rules
  • The game uses a single die
  • Player and computer take turns until someone wins by earning 100 points
  • Players begin a turn by rolling the die and tallying the points. The current player continues until rolling a '1' or choosing to 'hold'. The player accumulates all points from the round until a 'hold' but loses all points for this round if rolling a '1'.
  • Play rotates to the computer
Class Definition
  • Modify the "Pig" class.
  • Provide appropriate names and data types for instance variables to keep track of the player's score, computer's score and the current round score.
  • public Pig ( ) - a constructor that initializes all of the instance variables to appropriate values.
  • private void rollDie ( ) - roll the die. If the value is '1' then set the round score to 0. Otherwise, increment the round score by the new die value. Print the round score. This method is used by the player and the computer.
  • public void playerRolls ( ) - simulates part of the player turn: 1) invoke the rollDie( ) method, 2) print the player's score if the round is over, or 3) print an approriate message if the player wins.
  • public void playerHolds ( ) - simulates the second half of the player's turn: 1) update the player's score, 2) reset the round score and 3) print the player's score. Refer to the sample output below.
  • public void computerTurn ( ) - simulates the computer's entire turn: continue rolling the die and updating the round score until a '1' is rolled or the round score surpasses 19 or if the computer wins. Update the computer's score and the round score. Print the computer's score. Refer to the sample output below.
  • public boolean playerWon ( ) - return true if the player score is currently 100 or higher. Otherwise, return false.
  • public boolean computerWon ( ) - return true if the computer score is currently 100 or higher. Otherwise, return false.
Sample Interactive Session
  1. player will continue to invoke the "playerRolls" method until a 1 is rolled or the "playerHolds" method is invoked to end the player's turn
  2. player will invoke the "computerTurn" method to simulate the computer's turn
  3. this sequence repeats until someone wins
Automating the Game

Now that you can play manually, add the following two methods to simulate both players.

  • public void autoPlayerTurn ( ) - simulates the player's entire turn: continue rolling the die and updating the round score until a '1' is rolled or the round score surpasses 19 or if the player wins. Update the player's score and the round score. Print the player's score.
  • public void autoGame ( ) - automates and entire game by alternating between a player's turn and computer's turn until one of them wins. Keep track and report the total number of turns taken. Report who wins. Reset all values before starting the new game.
Integrating with GUI

After everything is complete from above, add the following methods to the "Pig" class to have the GUI version work properly.

  • public GVdice getDie ( ) - this one line method simply returns the instance variable for the GVdice.
  • public boolean isPlayerTurn ( ) - this one line method simply returns the instance variable for whether it is the player's turn. returns true if it is the player's turn or false if it is the computer's turn.
  • public int getComputerScore ( ) - return the computer's current score.
  • public int getPlayerScore ( ) - return the player's current score.
  • public int getCurrentPoints ( ) - return the current points of the round.

 

Bonus Challenges

If you finish the project early, you may want to attempt some of the following additional challenges.

  • Modify the GUI class so that the final winning message pops up using JOptionPane (3 pts).

Sample Results

Your program should generate similar results in the terminal window. Pay attention to blank lines, prompts and spelling for an attractive appearance.

Welcome to Scott's Awesome Game of Pig!

Round score: 2
Round score: 4
Round score: 10
--- Player score: 10

Round score: 0
--- Computer score: 0

Round score: 4
--- Player score: 14

Round score: 4
Round score: 0
--- Computer score: 0

Round score: 3
Round score: 6
--- Player score: 20

Round score: 5
Round score: 7
Round score: 12
Round score: 16
--- Computer score: 16

And towards the end of the game

Round score: 6
Round score: 8
Round score: 11
Round score: 0
--- Computer score: 63

Round score: 5
Round score: 8
--- Player score: 93

Round score: 0
--- Computer score: 63

Round score: 4
Round score: 8
--- Player score: 101

You won!

Sample results at the end of a auto play session

Round: 4
Round: 9
Round: 12
Round: 16
---- Computer score: 83

Round: 6
Round: 8
Round: 9
Round: 13
Round: 19
---- Computer score: 102

The computer won!

Total turns: 6

 

Grading Criteria

This project is worth a possible 100 points.

  • Stapled cover page with your name and signed pledge (- 10 pts if missing)
  • The code does not compile (- 10 pts)
  • Each method does what it is supposed to do (60 pts)
  • Source code follows our Java Style Guide (15 pts)
  • Appropriate documentation including javadoc tags (15 pts)
  • Sample output showing the start and end of a game (10 pts)
Turn In
  • Cover page - Your project must have a cover page that includes your name, a title, an appropriate picture and the following signed pledge. "I pledge that this work is entirely mine, and mine alone (except for any code provided by my instructor). " You are responsible for understanding and adhering to the School of CIS Guidelines for Academic Honesty.
  • Source code - a printout of your source code (with your name).
  • Sample output - a printout of sample output. You will need to cut and paste from the terminal window to print the results. As an alternative, you can save the contents of the Terminal to a text file and then print.
  • Be prepared to demo your project during lab.
Late Policy

Projects are expected to be turned in on time. However, you are encouraged to complete a project even if you must turn it in late.

  • 1 weekday late (-20 pts)
  • every subsequent weekday is an additional -10 pts
  • Saturday and Sunday are free days