• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Java project csc110 EyesHaveIt

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here are the requirements for a project I'm having trouble with.  I will attach a file of what it is supposed to look like.

In this assignment you will write a dice game called Eyes Have It.  You must write a class named, EyesHaveIt.java, that will contain the logic to play the game with two players: the computer and a human player.  The computer begins by taking its turn, followed by the human player taking his/her turn.  When the game is done, both players will have had the same number of turns.

A turn consists of rolling a pair of six-sided dice at least once, but no more than five times.  The computer will automatically stop rolling if its turn total reaches 20 points (the sum of the dice rolls in that turn).  The human will be prompted as to whether another roll is desired and may answer Y or y four times in a turn to get a maximum of five turns.  For each player the turn score or turn total is sum of both dice on all rolls taken in that turn.  A player's game score will be the sum of all the turn totals for all turns taken in the game.

If double sixes (box cars) are rolled, then that player will lose the current turn and ALL points are lost (both current turn points and all game points accumulated so far).

If double ones (snake eyes) are rolled once, 2 points are added to the turn points and all turn points are then doubled at the end of the current turn.  If a second snake eyes is rolled in the same turn, 2 more points are added to the turn total and then all turn points are multiplied by 4 at the end of the current turn.  This logic continues, so that if snakes eyes were rolled on each of a players five rolls in one turn, the turn total would be 10, but this would be multiplied by 32 (2 to the 5th power) to yield a turn total of 320!  LogGame 4 attached to this assignment shows this event actually happening!

If any other double is rolled (besides 6's and 1's), that player will lose the turn AND all points accumulated during this turn.  No game points are lost in this case.

The first player to 150 after both players have had the same number of turns is the winner.  If both players are over 150, the one with the higher total wins.  If both players have more than 150 points and the same number of points, another turn must be played for each player.

The Classes You Must Write
You are given the application class, PlayGame.java - available for download herePreview the document.  This application class will be executed by the JVM and will use the three classes you will write: EyesHaveIt class, which will use the PairOfDice class, which will use the Die class.  Each of these is described below.

EyesHaveIt.java - As you can see from the code in PlayGame.java, this class has a method named, init, which accepts a String, and another method named, playGame.  Both methods are void.  The design of these methods and the other methods that these may invoke is your assignment.  You may add other methods as you expand your design to play the game according to the rules in this document.  Recall that you are doing an object-oriented design.  This implies that the only variables that will be defined at the class level will be attributes of the game object.  Methods within this class should communicate by passing parameters and returning values, not by using variables at the class level for this purpose.   Examples of the EyesHaveIt object's attributes are the game scores for each player, the name of the human player, and the PairOfDice object used throughout the game by both players.  Your design should NOT cause any other variables to be defined at the class level in this class.  Note that this class uses one PairOfDice object and does NOT directly use the Die class.
PairOfDice.java - This class will be an abstraction of a physical pair of standard six-sided dice.  Only one object of this class will be created by the EyesHaveIt object and used throughout the game by both players.  You should consider this object to be a pair of dice.  When a player rolls during a turn, that player is rolling the PairOfDice object.  Players never roll a Die object directly.  The client of the PairOfDice class will roll the PairOfDice object, get the values of each die in the pair, etc.  This class should have exactly two attributes: two objects of the class, Die.  This is the only class that uses the Die class.
Die.java - This class is an abstraction of one standard six-sided die.  A class similar to this is given in Chapter 6 of the text and you may base your design on that example.  HOWEVER, it is not a good idea to create the Random object inside the roll method as is done in the book.  An application will experience more randomness by creating the Random object less often.  Besides that, there really is no need to recreate this object every time the dice are rolled.  Therefore, the best design is to create a static Random object at the class level in the Die class.  This way only one Random object is created per game and, by making this object static, both Die objects will use the same Random object to generate its rolls. So, create your Random object at the class level (inside the class but outside of or before the main method) in the Die class as follows:
 public static Random generator = new Random();
Of course, you can name the object whatever you choose.  Remember to import java.util.Random.
When you submit this assignment, you should submit three Java files named exactly as follows:

EyesHaveIt.java

PairOfDice.java

Die.java
 
Markus Herrera
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
User wins this game. Annotations should help reader understand this log and the
others that are not annotated.
----jGRASP exec: java PlayGame
Who is playing against the computer? Joe
Computer's turn:
Rolled: 5 and 5
Rolled double. . . lose all turn points.
CURRENT GAME SCORE: Computer: 0 Joe: 0
Press ENTER to continue ...
Joe 's turn:
Rolled: 2 and 3
Current score for this turn: 5
Roll again? (y/n) y
Rolled: 6 and 4
Current score for this turn: 15
Roll again? (y/n)
Answer Y or N: Roll again? (y/n) y
Rolled: 3 and 1
Current score for this turn: 19
Roll again? (y/n) y
Rolled: 1 and 4
Current score for this turn: 24
Roll again? (y/n) n
CURRENT GAME SCORE: Computer: 0 Joe: 24
Press ENTER to continue ...
Computer's turn:
Rolled: 1 and 1
Rolled snake eyes! All turn points will be doubled.
Rolled: 6 and 5
Current score for this turn: 13
Snake Eyes cause 13 points to become 26 points!
CURRENT GAME SCORE: Computer: 26 Joe: 24
Press ENTER to continue ...
Joe 's turn:
Rolled: 4 and 2
Current score for this turn: 6
Roll again? (y/n) y
Rolled: 6 and 4
Current score for this turn: 16
Roll again? (y/n) y
Rolled: 6 and 4
Current score for this turn: 26
Roll again? (y/n) y
Rolled: 4 and 6
Current score for this turn: 36
Roll again? (y/n) y
Rolled: 1 and 4
Current score for this turn: 41
CURRENT GAME SCORE: Computer: 26 Joe: 65
Press ENTER to continue ...
Computer's turn:
Rolled: 4 and 1
Current score for this turn: 5
Rolled: 3 and 5
Current score for this turn: 13
Rolled: 2 and 5
Current score for this turn: 20
CURRENT GAME SCORE: Computer: 46 Joe: 65
Press ENTER to continue ...
Joe 's turn:
Rolled: 3 and 4
Current score for this turn: 7
Roll again? (y/n) y
Rolled: 1 and 3
Current score for this turn: 11
Roll again? (y/n) y
Rolled: 4 and 6
User presses ENTER to continue here
User answers Y or N (either
case) to tell program whether
the user wants to roll again.
User presses ENTER to continue here
User presses ENTER to continue here
User presses ENTER to continue here
User presses ENTER to continue here
User answers are circled here. User
must always answer y/n after the first
roll. User may answer y four times to
get a maximum of five rolls for one turn.
 
Marshal
Posts: 4796
601
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Markus Herrera wrote:Here are the requirements for a project I'm having trouble with. ...


Welcome to the Ranch

Maybe start by explaining where you are running in to trouble.
 
Markus Herrera
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just don't know how to code this correctly I already have a die Java and a pair of dice Java file for this project but I can't get this assignment done correctly
 
Markus Herrera
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To fully understand what is expected here are the complete requirements:

Using/Writing Classes Assignment
To fully understand all of the requirements for the program you will write for this assignment read this entire page, but also study each of the four execution logs linked below.  Reading all of these documents will enable you to see what will be required in each of the three classes you will write and submit:

EyesHaveIt.java,  PairOfDice.java, and  Die.java.

Before you begin writing your Java code, design your algorithms for the various parts.  This is a way to save time both in implementation and in debugging.  Before doing any coding be sure to review the REMEMBER section at the end of this document, as well as all coding conventions, especially those pertaining to writing classes.

Overview of the Game
In this assignment you will write a dice game called Eyes Have It.  You must write a class named, EyesHaveIt.java, that will contain the logic to play the game with two players: the computer and a human player.  The computer begins by taking its turn, followed by the human player taking his/her turn.  When the game is done, both players will have had the same number of turns.

A turn consists of rolling a pair of six-sided dice at least once, but no more than five times.  The computer will automatically stop rolling if its turn total reaches 20 points (the sum of the dice rolls in that turn).  The human will be prompted as to whether another roll is desired and may answer Y or y four times in a turn to get a maximum of five turns.  For each player the turn score or turn total is sum of both dice on all rolls taken in that turn.  A player's game score will be the sum of all the turn totals for all turns taken in the game.

If double sixes (box cars) are rolled, then that player will lose the current turn and ALL points are lost (both current turn points and all game points accumulated so far).

If double ones (snake eyes) are rolled once, 2 points are added to the turn points and all turn points are then doubled at the end of the current turn.  If a second snake eyes is rolled in the same turn, 2 more points are added to the turn total and then all turn points are multiplied by 4 at the end of the current turn.  This logic continues, so that if snakes eyes were rolled on each of a players five rolls in one turn, the turn total would be 10, but this would be multiplied by 32 (2 to the 5th power) to yield a turn total of 320!  LogGame 4 attached to this assignment shows this event actually happening!

If any other double is rolled (besides 6's and 1's), that player will lose the turn AND all points accumulated during this turn.  No game points are lost in this case.

The first player to 150 after both players have had the same number of turns is the winner.  If both players are over 150, the one with the higher total wins.  If both players have more than 150 points and the same number of points, another turn must be played for each player.

The Classes You Must Write
You are given the application class, PlayGame.java - available for download herePreview the document.  This application class will be executed by the JVM and will use the three classes you will write: EyesHaveIt class, which will use the PairOfDice class, which will use the Die class.  Each of these is described below.

EyesHaveIt.java - As you can see from the code in PlayGame.java, this class has a method named, init, which accepts a String, and another method named, playGame.  Both methods are void.  The design of these methods and the other methods that these may invoke is your assignment.  You may add other methods as you expand your design to play the game according to the rules in this document.  Recall that you are doing an object-oriented design.  This implies that the only variables that will be defined at the class level will be attributes of the game object.  Methods within this class should communicate by passing parameters and returning values, not by using variables at the class level for this purpose.   Examples of the EyesHaveIt object's attributes are the game scores for each player, the name of the human player, and the PairOfDice object used throughout the game by both players.  Your design should NOT cause any other variables to be defined at the class level in this class.  Note that this class uses one PairOfDice object and does NOT directly use the Die class.
PairOfDice.java - This class will be an abstraction of a physical pair of standard six-sided dice.  Only one object of this class will be created by the EyesHaveIt object and used throughout the game by both players.  You should consider this object to be a pair of dice.  When a player rolls during a turn, that player is rolling the PairOfDice object.  Players never roll a Die object directly.  The client of the PairOfDice class will roll the PairOfDice object, get the values of each die in the pair, etc.  This class should have exactly two attributes: two objects of the class, Die.  This is the only class that uses the Die class.
Die.java - This class is an abstraction of one standard six-sided die.  A class similar to this is given in Chapter 6 of the text and you may base your design on that example.  HOWEVER, it is not a good idea to create the Random object inside the roll method as is done in the book.  An application will experience more randomness by creating the Random object less often.  Besides that, there really is no need to recreate this object every time the dice are rolled.  Therefore, the best design is to create a static Random object at the class level in the Die class.  This way only one Random object is created per game and, by making this object static, both Die objects will use the same Random object to generate its rolls. So, create your Random object at the class level (inside the class but outside of or before the main method) in the Die class as follows:
 public static Random generator = new Random();
Of course, you can name the object whatever you choose.  Remember to import java.util.Random.
When you submit this assignment, you should submit three Java files named exactly as follows:

EyesHaveIt.java

PairOfDice.java

Die.java
 
Marshal
Posts: 80634
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't like assessments with so much detail supplied in advance. It detracts from the thought students have to put into the application. But it makes the process easier.

Suggestion. Find the smallest class you can and implement them. If you have done that already, and tested that your code is working correctly, you have your Die and Pair classes ready to submit; they won't need any changes. Remember those classes must be complete, with correct OO design, correct code style and comments. Do you require documentation comments?

public static Random generator = new Random();

That should of course read …static final Random… Please query why you have been told to declare it coderanch, since it will probably only be used inside the Die class. Also, if it is final, that Random object should probably be named as a constant: GENERATOR, or similar. That is another problem with over‑specified assignments; it only take five minutes for some annoying b*gg*r like me to create mistakes in the assignment But if the assignment says coderanch, then public it must be.
As you go, record what you are doing; maybe the best way to record things is to put your code into a Git or SVN repository and keep it updated. Most people use Git nowadays rather than SVN.

Once you have got that all done, write down your design for the game. Draw diagrams or flowcharts about how you are going to play. Work out the smallest method you can, preferably one that you can implement on its own. Maybe one to roll the dice 5×. Don't all that method throw(). One you have got that working, tested, correct, move on to the next small part. Divide and rule. A long time ago, I worked out that whatever people may say, they design from centre to periphery and implement from periphery to centre. Well, that is only a rough guideline.

. . . and . . . welcome to the Ranch
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic