edicer2 Posted February 20, 2009 Share Posted February 20, 2009 (edited) Hi am new to java and i have a program my teacher wants us to practice and write. Here is the program instructions: Using the PairOfDice class from programming project 5.10, design and implement a class to play a game called Pig. In this game, the user competes against the computer. On each turn, the current player rolls a pair of dice and accumulates points. The goal is to reach 100 points before your opponent does. If, on any turn, the player rolls a 1, all points accumulated for that round are forfeited and control of the dice moves to the other player. If the player rolls two 1’s in one turn, the player loses all points accumulated thus far in the game and loses control of the dice. The player may voluntarily turn over the dice after each roll. There for the player must decide to either roll again (be a Pig) and risk losing points or relinquish control of the dice, possibly allowing the other player to win. Implement the computer player such that it always relinquishes the dice after accumulating 20 or more points in any given round. Here are the 5 classes required Die: public class Die { private final int MAX = 6; private int faceValue; public Die() { faceValue = 1; } public int roll() { faceValue = (int) (Math.random() * MAX) + 1; return faceValue; } public void setFaceValue(int value) { if (value > 0 && value <= MAX) faceValue = value; } public int getFaceValue() { return faceValue; } public String toString() { String result = Integer.toString(faceValue); return result; } } PairOfDice public class PairOfDice { private Die die1, die2; public PairOfDice() { die1 = new Die(); die2 = new Die(); } public int roll() { return die1.roll() + die2.roll(); } public int getTotalFaceValue() { return die1.getFaceValue() + die2.getFaceValue(); } public void setDie1FaceValue(int value) { die1.setFaceValue(value); } public void setDie2FaceValue(int value) { die2.setFaceValue(value); } public int getDie1FaceValue() { return die1.getFaceValue(); } public int getDie2FaceValue() { return die2.getFaceValue(); } public String toString() { return "Die 1: " + die1.getFaceValue() + " Die 2: " + die2.getFaceValue(); } } PigPlayer import java.util.*; import java.util.Scanner; public class PigPlayer{ public static void main(String[]args){ public final static int ASK=-1; private static PairOfDice dice = new PairOfDice(); private int total; private int round; private int limit; public PigPlayer(int max){ total=0; round=0; limit=max; } public boolean roll(PairOfDice dice, int goal){ boolean rollAgain=true; dice.roll(); int die1=dice.getDie1FaceValue(); int die2=dice.getDie2FaceValue(); System.out.println(); System.out.println("Dice; " + die1 +" + " + die2 + " = " + (die1+die2)); if (die1==1 || die2==1){ round=0; System.out.println("Busted"); rollAgain=false; if (die1==1 && die2 ==1) total=0; } else round+=die1+die2; System.out.println("Current Round:" + round); System.out.println("Potential Total: " +total+round); if((total+round)>=goal) rollAgain=false; else if (limit==ASK){ System.out.println("Take another turn (Y/N)?"); Scanner sc =new Scanner(System.in); String again=sc.nextLine(); rollAgain=again.equalsIgnoreCase("Y"); } else if(round>=limit) rollAgain=false; if(rollAgain){ total+=round; round=0; } return rollAgain; } public int getPoints(){ return total; } } PlayPig import java.util.*; class PlayPig { public static void main(String[]args) { Pig game = new Pig(100); game.play(); } } Pig (I have no idea what to do for this class. I know that PlayPig class want to run game from this Pig class but I have no idea what im suppose to be doing. I have email my teacher and he said to ask the tutors but they are closed today and he wants us to turn it in by 11:59 pm tonight which is in 3.5 hours for me. i have go this far and have no idea what to do now. The pig class is suppose to manage the players but i have now idea how to set it up and link the game.play method.(teacher gave us the playpig class) Thanks for your time and help! Edited February 20, 2009 by edicer2 Link to comment Share on other sites More sharing options...
Pezzy Posted February 20, 2009 Share Posted February 20, 2009 Sorry, cant help you... I'm taking JavaScript, and am finding that google is my friend. Link to comment Share on other sites More sharing options...
edicer2 Posted February 20, 2009 Author Share Posted February 20, 2009 yeah i have been googling for 3 days now and put about 8 hours into getting no where..... Link to comment Share on other sites More sharing options...
vengeful Posted February 20, 2009 Share Posted February 20, 2009 Java and I are not on speaking terms right now, so I'm of no help to you. In fact, I think my brain went numb reading your scripts....Good luck though! Link to comment Share on other sites More sharing options...
beavis0076 Posted February 20, 2009 Share Posted February 20, 2009 Yea, that hurt me... I think I might have broken something and gotten dumber....lmao! Link to comment Share on other sites More sharing options...
edicer2 Posted February 20, 2009 Author Share Posted February 20, 2009 stuff is retarded.....i really hope i dont have to retake the class for a 3rd time Link to comment Share on other sites More sharing options...
Kingman Posted February 20, 2009 Share Posted February 20, 2009 Thanks for the headache... Link to comment Share on other sites More sharing options...
beavis0076 Posted February 20, 2009 Share Posted February 20, 2009 ahh.. no sweat.. I took spanish 1 three times... never did pass.... lol! Link to comment Share on other sites More sharing options...
adamzan Posted February 20, 2009 Share Posted February 20, 2009 Oh man, it's reading week, can't even think about that kinda stuff. Link to comment Share on other sites More sharing options...
edicer2 Posted February 20, 2009 Author Share Posted February 20, 2009 ha yeah ik what you mean i sat in front of it for 12 hours and didnt get any further. Link to comment Share on other sites More sharing options...
GrimGreg Posted February 20, 2009 Share Posted February 20, 2009 Last programing language I learned was C, no help here. Link to comment Share on other sites More sharing options...
tekazgtr1984 Posted February 21, 2009 Share Posted February 21, 2009 Can't help ya 'cause my brain's not designed for that. Yet I had no problems learning the three alphabets in Japanese... Who put me together!? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now