Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CS 0007 Project 3

Overview

In this project, you’ll be building a very simple dice game in Java from scratch using object-oriented principles and the coding knowledge you’ve learned this semester.

You may use any code from lecture, recitation, or the textbook as a reference, but this is an indi- vidual assignment. You may not share code with or copy code from any other person (whether they are in the class or not).

Requirements

Unlike previous assignments, there is basically no starter code. Instead, I provided step-by-step instructions in the handout but expect you to create your files from scratch.

I have provided PlayGame.java, which is explained line-by-line in the lecture recorded and posted alongside the release of this project. I have also provided AutoGrader .java, which I STRONGLY encourage you to use as you progress on this project.

You will be creating three files: Dice .java, Player .java, and Game .java. Each one corresponds to a Java class necessary to complete the game. You can compile them individually (e.g. javac Dice.java), or you can compile them all at once using javac  Dice .java  Player .java  Game .java .

When you’re done, you should be able to play the game with your family or friends.

Important:  Like the other projects, there is a reference implementation.  You should use it! On the command line, run java  -jar  Reference .jar.

Projects in Mirror May Be Harder Than They Appear

This project is not extremely difficult, code-wise. However, I’ve given you very little starter code. Much of the challenge of this project is building up this (relatively simple) game from scratch. Jeff and the TAs are willing to help, but be warned: even though there’s less code to write than the previous project, you should still start early!

Note: No Late Days!

This project is due on the very last date that I can accept work for the class. If you turn it in late, I might have to assign you an ”incomplete” grade for the semester while I grade your work.

Submission Instructions, Due Date, and Grading

This project is due on April 30, 2023at 11:59:59pm.

To submit, you should submit your three  .java files on blackboard, as a  .zip file.  If you have trouble creating a .zip file, email Jeff or your TAs.

You will get 5 points if the code you submit compiles without errors, and 5 points if your code is

reasonably formatted and commented.

Task

Possible points

Compiles without errors

5

Reasonable formatting and comments

5

Dice.java

20

Player.java

40

Game.java

30

Total

100

Note: If you’re totally stuck and you’re out of time to submit, I’d rather you comment out your broken code and include a note about what you were trying to do, rather than just delete it. This will let me award partial credit!

I’ll be doing three things to grade your projects: running the autograder, reading your code, and playing your game.

AutoGrader

This project includes an AutoGrader! The AutoGrader (AutoGrader.java) will tell you if anything is amiss in your Dice .java or Player .java, and runs some sanity checks on Game .java to make sure you’re at least on the right track.

You can run the autograder by downloading AutoGrader .java to the same folder your code is in, then building it with javac  AutoGrader .java and running it with java  AutoGrader.  Be sure that you recompile your own code before running the autograder, since it will not recompile your code!

Style, Comments, and Formatting

Style and commenting are pretty subjective, but undeniably important. I’ll be grading on the stan- dard of ”does this look mostly reasonable,”not ”is this exactly how I would do it.”

With that said, there are tools that will help you! There are plenty of online code formatting tools (for instance, https://www.tutorialspoint.com/online java formatter.htm).  Tools like this are fine to use as long as they only change the formatting of your code, not the actual logic.  If you’re not sure if something is allowed, send me an email. You’ll never get in any trouble for asking.

As for comments: try to comment big blocks of your code with a sentence or so that explain what’s going on. You don’t have to label every constructor with ”this is a constructor,”but in general one comment for every five or so lines of code is a good rule of thumb.  Again, I won’t be counting exactly, just scanning your code to make sure that you’ve included some number of meaningful comments.

Pig: A Simple Dice Game

For this project, we’ll be implementing a variant of the simple game Pig1. Take a few minutes to play the game (you can play online2) and make sure you understand the rules as laid out in this handout:

• The game has one or more players. To be any fun, you really should have at least two, but you can play by yourself if you want.

• The first player to get to the victory score wins. Typically, this is 100 points.

• On each player’s turn:

1. You start off with zero points for the round.

2.  Roll the die. If you roll a 1, you get zero points for the round and your turn is over.

3.  If you rolled anything other than a 1, add the value on the die to your score for the round, then decide if you want to roll again. If so, go back to step 2.

4.  If you decide not to roll again (but you didn’t end on a 1), add your score for the round to your total score.

5.  Pass the die to the next player.

In our version of the game, there are two adjustable parameters:

• The victory number can be any positive number of points

• The die can have any number of sides (as long as it has at least two)

Task 1: Die .java

The first task is to implement a class to represent a die3.

You will do this in Die .java, which should have the following members:

• A constructor which takes the number of sides that the die should have (as an int).  If the number of sides is less than 2, it should be set to 6.

• A no-argument constructor, which sets the number of sides to six.

• A roll method, which takes no arguments and returns (as an int) a random number be- tween 1 and the number of sides (inclusive).

• Whatever private fields you need.

1. Create a file called Die .java, with an empty class in it.

2. Add a new (private) field to the class to store the number of sides. You probably want this to be an int.

3. Create a constructor for Die that takes one argument, an int representing the number of sides. If the number is less than 2, store 6 as the number of sides in the class field. Other- wise, store the number given.

4. Create a no-argument constructor. It should assume the number of sides is six. You can do this in one line with the this() constructor, but that isn’t required to get full credit.

5. Add a roll() method to your Die, which takes no arguments and returns an int between 1 and the number of sides (inclusive).

Hint : you’ll probably need to import  java .util .Random; at the top of your file.

• Hint :  the Random class’s nextInt() method takes in a number and returns a value between 0 (inclusive) and the number given (exclusive). You can use this with a little bit of math to implement roll().

Hint : you should probably only create a new Random object once, in your constructor. Then you can save it into a field and re-use it every time roll() is called.

6.  If you haven’t already, compile your code and run the autograder.

Task 2: Player .java

Next, we’ll implement an object to represent a player. Players have a name (stored as a String), a current score (an int), and a number of wins (also an int).

The Player class has the following methods:

• A constructor which takes in a String, the player’s name. If the String given is null or the empty string ( ""), set the player’s name to a unique non-empty value (in my reference code, I use Player 1”, ”Player 2”, etc). Otherwise, set the name to the given String.  Score and number of wins should both start at zero.

• A constructor which takes no arguments. It sets the name to a unique non-empty string. (In my reference code, I use ”Player 1”, ”Player 2”, etc).

• Getters for the name (String), number of points (int), and number of wins (int).  These should be called getName(), getScore(), and getWins(), respectively.

• A method addPoints(), which takes in an int and adds that number to the total number of points the user has.

• A method resetScore(), which takes no arguments and resets the player’s score to zero.

• A method addWin(), which takes no arguments and increments the number of times the player has won.

• A toString() method, which takes no arguments and returns (as a String) the player’s name, followed by the number of points in parentheses.  For example, if the player’s name is Jeff and the player has 25 points, you’ll return "Jeff  (25 points)". note that the auto-

grader is vary particular about the formatting of your return String here.

• Whatever private fields you need.

1. Create a blank file, Player .java, with an empty class in it.

2.  Implement the methods above.  I strongly recommend running the autograder after each step.

Hint:  In order to create unique names whenever you need to, you’ll likely want to use a static field.  The easiest way is to create a private  static  int inside the Player class to hold the number of times the class has been instantiated, and increment that number by 1 each time any constructor is called. The lecture from March 27 has some examples that might help.

Task 3: Game .java

Finally, you’ll write Game.java, which contains a class that represents a single game. A Game object takes into its constructor an array of Players, a Die, and an int (the victory score).  It has one method, play(), which plays a round of the game.

Note that the autograder only does basic sanity checks for Game .java.  You should play your game (using the provided PlayGame .java to test it thoroughly!)

1. Create a new file, Game .java, and create an empty class in it.

2. You’ll probably need to import Scanner (like we’ve done a million times in this class), since Game reads input from the user.

3. Create a constructor. It should take in three arguments (in this order): an array of Players, a Die, and an int representing the victory score. The constructor should store all of these in private fields.

4. Create a method called play(). It takes no arguments.

(a)  Do the following forever (yes, really. We’ll use a return statement to end the method when the game is over).

i.  Loop over the array of players.  For each player, do the following:  (you probably want to do a for loop over the array of players)

A.  Print out the player’s name to let them know it’s their turn, and print out their current score. If your toString() method for Player works, this should just be as easy as System .out .println(players[i]);, or something similar.

B. Create a variable to store the player’s score for this round so far. Set it equal to zero.

C.  Roll the die (using the roll() method of your Die) and tell the user what they rolled.

D.  If they rolled a 1, set their score for the round to zero and don’t let them roll again.

E.  If they rolled anything other than a 1, add that number to their round score and ask if they want to roll again. If so, repeate the last two steps.

F.  If they choose not to roll again (but their most recent roll was not a zero), add their round score to their total score using addPoints().

G.  Use getPoints() to see if the player’s total is greater than or equal to the stored victory number.   If so, print out that the player won and call addWin(), then return; to end the round. If not, go on to the next player.