# Java programing help



## edicer2 (Feb 20, 2009)

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!


----------



## jonathan731 (Aug 30, 2003)

it sounds like you want someone to do the homework for you. asking for help and getting the assignment done are very different. I dont know if your in college or not but if plagarism even in a program can have grave consequences. Just thought i'd say that.


----------



## edicer2 (Feb 20, 2009)

i just needed help with the game.play method i didnt know how it worked but its to late now


----------



## JimmySeal (Sep 25, 2007)

Two bits of advice for the next time you need help:


Ask more than 3.5 hours ahead of the time you need your work done. People here live in various time zones and have their own personal priorities.
Put [ code ] tags around your code to preserve the indentation. Code with no indentation is very difficult to read.

I did honestly consider helping you, but wasn't sure how worthwhile it would have been, given your time constraints. I hope to assist you at a more leisurely pace in the future.


----------

