Jump to content

JAVA HELP PLEASE


edicer2
 Share

Recommended Posts

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 by edicer2
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...