please help me with my computer science homework
The project description can be found on this page (Pig Project Assignment Details) in case this is difficult to read.
Overview
Pig (Links to an external site.) is a folk jeopardy dice game described by John Scarne in 1945, and was an ancestor of the modern game Pass the Pigs® (originally called PigMania®).
Here are the rules: two players race to reach 100 points. Each turn, a player repeatedly rolls a die until either a 1 is rolled or the player holds and scores the sum of the rolls (i.e. the turn total). At any time during a player's turn, the player is faced with a choice: roll or hold.
· Roll - If the player rolls a
· 1: the player scores nothing and it becomes the opponent's turn.
· 2 - 6: the number is added to the player's turn total and the player's turn continues.
· Hold - The turn total is added to the player's score and it becomes the opponent's turn.
In Part 1 - your program will consist of the following classes:
· Die - has a static method to roll a die
· PigPlayer - an abstract class representing the player's name, current score, and win record.
· UserPigPlayer - a class that asks the user if they want to roll or hold
· PigGame - plays Pig
For Part 2 - you will add the following classes:
· SimpleHoldPlayer - a computer player that uses a strategy to roll until a value is reached (the first of four computer strategies)
· PigGame - plays Pig (updated for the computer player)
· Simulations - runs several simulations to find out which strategies work well
· FourTurnsPlayer - the second of four computer strategies
· WatchOpponentPlayer - the third of four computer strategies
· StrategicPlayer - the fourth of four computer strategies
· PigGameGUI - a graphical user interface for the PigGame. I have written this class for you, but you will need to make sure you write your classes exactly to specifications for your code to work with my interface.
When you have finished, you will have two ways of running your program. The text program will be run by running themain() method in PigGame. You will also be able to run a graphical version, by running the main method inPigGameGUI.
You will write three separate classes that will "have" each other as instance variables or local variables: Die, PigGameand Simulations. A PigGame "has" two PigPlayer objects, and a Simulations "has a"PigGame. These three classes will not be related to each other through inheritance. You will also write an abstract PigPlayer class that has five child classes:
This is the first 2 weeks of a 4 week project. I would suggest using the following deadlines to keep yourself on track. (Completing the class includes testing and debugging! Don't wait until the last week to test and debug!)
Week 1: complete the Die, PigPlayer, and UserPigPlayer classes
Week 2: complete the PigGame class to allow two users to play against each other
Die
You should begin by writing the Die class. It should have an instance variable sides, which you can initialize to 6. You should then write four methods (plus any constructors you find useful):
public static int roll() public void setSides(int numberOfSides) public int getSides() public int rollDie()
The static roll() method will assume a six sided die, whereas the non-static rollDie() method will allow you to create special die with as many sides as you like (positive numbers only). Write the static roll()method first!
In the static roll() method, you can call the static Math.random (Links to an external site.) to help you get random numbers. Math.random returns a random number between 0 and 1 (including 0 but not 1). Math.random() returns a random floating point number (a double) between 0 and 1, possibly 0 but never 1, which is sometimes described as the range [0, 1). First think about what math would be required to convert the range [0, 1) to [0, 6), or a floating point number between 0 and 6. Write your static roll() method so it calls Math.random(), converts to the range [0, 6) and then returns an int that is 0,1,2,3,4 or 5.
You should then write a main() method that checks that your die will roll numbers 0 - 5. This main() method should be inside of the Die class. Write a loop that calls the static roll() method several times and make sure all six numbers show up (approximately the same number of times). For example, if you run the roll() method 600 times, you hope to have all six numbers appear between 90-110 times (although occasionally you'll have a number appear only 80 times). Once you're sure everything works, go back and modify the roll method so it returns a number between 1-6 (instead of 0-5).
After your static roll() method return numbers between 1 to 6, generalize your algorithm in the non-static rollDie() method so the Die class works for more sides.
PigGame
First create an empty PigGame class. Then add a static constant to this class: a GOAL that represents the winning score (usually 100, but you could change it to something smaller for debugging). Static constants and class variables are described in Section 6.2 of your textbook (pages 390-391).
PigPlayer
Next write the PigPlayer class. It should have one String instance variable (the player's name) and at least two int instance variables (current score and total number of games won). We need to keep track of the number of games won to run our simulations later.
This PigPlayer class should have the following methods:
· a constructor that takes a name as a String
· public void setName(String) and public String getName()
· public void reset() - should get the player ready for a new game by setting the score to 0
· public void addPoints(int turnTotal) - should add the turn total to the player's score. It should also increment the number of wins if the player's score is now greater than or equal to the goal.
· public boolean won() - returns true if this player has won the game (reached the GOAL)
· public int getScore() - returns the player's current score
· public int getWinRecord() - returns the number of games this player has won
· public String toString() - returns a String with the player's name and score
Your methods should not have 100 hard-coded, but use the GOAL constant from the PigGame. This will make it easier to debug your code later.
You should write a short main() method in the PigPlayer class to check that all your methods work correctly.
Then add an abstract method:
public abstract boolean isRolling(int turnTotal, int opponentScore);
This abstract method will return whether or not the player wants to roll the die. You'll need to comment out your main method once you make the PigPlayer class abstract. But don't delete the method - it will be a good start for testing the UserPigPlayer class.
UserPigPlayer
Next write the UserPigPlayer class. This class should be a child of the PigPlayerclass, which means you'll need to write a constructor and the isRolling() method:
· The constructor should take a String argument (the name of the player).
· The isRolling() method in the UserPigPlayer class should print out the turn total and prompt the user if they want to roll or hold. You'll want to print from the screen and read from the keyboard. Since the user will be rolling more often than holding, use the Enter key (empty input) to indicate the user wants to roll. Any other input (a line of non-zero length) will indicate the user wants to hold. Don't worry about not using the opponentScore parameter - this is information that will be used by futurePigPlayer classes, but not this one. The code below shows you how you can check if the user presses the Enter key, or enters another character: (This assumes a Scanner object has been created in the constructor for the UserPigPlayer class.) System.out.println("Press enter to roll again, anything else to hold."); String ch = keyboard.nextLine(); if (ch.length() == 0) { return true; } else { return false; } If the user presses the Enter key, the nextLine() method returns an empty String which is length 0. If the user presses anything other than Enter, the nextLine() method will return a String that has a length > 0.
You should be able to write a main method that calls isRolling(). Put this main method in the UserPigPlayer class. Test and comment the class before continuing.
At this point, you will be able to test your Die, UserPigPlayer and PigPlayer classes with PigPlayerTest (Links to an external site.) unit tests. Don't forget to set PigGame.GOAL to 100, or your code will fail this unit test.
Your code should pass PigPlayerTest before proceeding.
PigGame
Write the PigGame class. It should have two instance variables: two PigPlayers. There should be at least three constructors:
· a default constructor that creates two UserPigPlayers (Player 1 and Player 2)
· a constructor that takes two Strings, and creates two UserPigPlayers with those two Strings as the names
· a constructor that takes two PigPlayers
You should write the following methods:
· public void reset() - reset the two PigPlayers, when your players are ready to start a new game.
· public static int playTurn(PigPlayer player, PigPlayer opponent) - You should allow "player" to roll the die (or hold) until the turn is over, by calling the isRolling method. The playTurn method returns the turn total rolled (0 if 1 was rolled, or the turn total if the player chose to stop rolling).
· public void playGame() - plays an entire game of Pig, where each PigPlayer gets a turn (calling playTurn) until one player wins. Your code should always have the same PigPlayer instance variable go first.
· public static void userVsUser() - this method should be similar to a main method. Print the rules and prompt the users for their names. Decide randomly (using Math.random) which player will go first. Then create a PigGame and call playGame to run the game. Here is an example of my userVsUser program running (Links to an external site.). In this example, I've underlined the users' input.
· your main method will be one line, calling userVsUser () method.
Test each method as you write it. Then run the game and debug it. Have fun!
These classes will be used as part of the larger project. Submit the following classes here:
- Die.java - PigPlayer.java - PigGame.java
- UserPigPlayer.java
Use doctor java only!!