Program help?

Hey, fellow java programmers.

I've been working on a "Pig" program, which requires a class called "PairofDice" and a "Pig" class.

I've gotten the "PairofDice" down without too much trouble. The driver program shouldn't be too hard to write...but I have NO clue on how to write a "Pig" class!

Basically, I've got two dice. I need a player to be able to roll the dice, and add his/her points. Whoever reaches 100 points first wins. If a player rolls a 1, he/she loses all points. If a player rolls two 1s in one turn, the player loses all points earned so far in the game and loses control of the dice. The player needs to decide to either roll again and risk losing points, or give up the dice, possible letting the other player (the computer) win. I also need to set it up so the player always gives up the dice after getting 20+ points in a round.

publicclass PairOfDice

{

privateint die1;// Number showing on the first die.

privateint die2;// Number showing on the second die.

public PairOfDice()

{

// Constructor. Rolls the dice, so that they initially

// show some random values.

roll();// Call the roll() method to roll the dice.

}

publicvoid roll()

{

// Roll the dice by setting each of the dice to be

// a random number between 1 and 6.

die1 = (int)(Math.random()*6) + 1;

die2 = (int)(Math.random()*6) + 1;

}

publicint getDie1()

{

// Return the number showing on the first die.

return die1;

}

publicint getDie2()

{

// Return the number showing on the second die.

return die2;

}

publicint getRoll()

{

// Return the total showing on the two dice.

return (die1 + die2);

}

}// end class PairOfDice

That's my "PairofDice" class. So, my question is this: how should I proceed to write a "Pig" class?

I would really appreciate any help I could get, as this program could either make me fail or pass this semester.

Thank you.

- Max

[3441 byte] By [bronze-starDukes] at [2007-11-26 12:13:12]
# 1

public class Pig

{

public static void main(String[] args)

{

//insert code here

}

}

Theres a start.

goldstar at 2007-7-7 14:14:06 > top of Java-index,Archived Forums,Socket Programming...
# 2
The thing is, the dice are easy to make because they're both objects (all I have to do is construct them and assign methods). But in the Pig class, do I make each player an object, or what?
bronzestar at 2007-7-7 14:14:06 > top of Java-index,Archived Forums,Socket Programming...
# 3
That is a decision you will have to make. You can create a Player class and create two objects. Or just have two ints to hold the scores of the two players. There is no right or wrong answer. You have to decide which way to go.
goldstar at 2007-7-7 14:14:06 > top of Java-index,Archived Forums,Socket Programming...
# 4
You could just make a loop in your main method that loops until one player reaches 100. You only need one int variable for each player. You could also make a Player class if you wanted to.
goldstar at 2007-7-7 14:14:06 > top of Java-index,Archived Forums,Socket Programming...
# 5
Dammit. I'm still lost. Call me stupid, but I can't figure this thing out.
bronzestar at 2007-7-7 14:14:06 > top of Java-index,Archived Forums,Socket Programming...
# 6
What dont you get?
goldstar at 2007-7-7 14:14:06 > top of Java-index,Archived Forums,Socket Programming...
# 7

Thanks for your patience. I'm new at this stuff. I actually liked writing Java, and was pretty good at it until it came to writing classes.

I don't understand what to do with the "Pig" class. My teacher doesn't want any other classes than the three I mentioned above, so the suggestion of a "Player" class won't do. I don't know what objects to put in here, and consequently, I have no clue what to make the non-existing objects do.

bronzestar at 2007-7-7 14:14:06 > top of Java-index,Archived Forums,Socket Programming...
# 8

> Thanks for your patience. I'm new at this stuff. I

> actually liked writing Java, and was pretty good at

> it until it came to writing classes.

You need to get used to it. Java is an Object-Oriented Programming Language.

Basically, you need two ints. One for the user's score and one for the computer's score. You also need one PairOfDice object. You then need a loop that should look something like this:

while(userScore < 100 && compScore < 100)

{

//roll dice for the user

//check if any ones are rolled

//roll for the computer

//check if any ones are rolled

}

Start with that.

goldstar at 2007-7-7 14:14:06 > top of Java-index,Archived Forums,Socket Programming...
# 9

public class Pig

{

public static void main(String[] args)

{

int userScore = 0;

int compScore = 0;

while(userScore <= 100 && compScore <= 100)

{

PairofDice user = new user();

user.roll;

if (die1 == 1 || die2 == 1)

{

}

else

{

}

PairofDice comp = new comp();

comp.roll;

if (die1 == 1 || die2 == 1)

{

}

else

{

}

}

}

}

bronzestar at 2007-7-7 14:14:06 > top of Java-index,Archived Forums,Socket Programming...
# 10
Any good?
bronzestar at 2007-7-7 14:14:06 > top of Java-index,Archived Forums,Socket Programming...
# 11

> Any good?

...Not really.

PairofDice user = new user();

Can you see whats wrong with this?

user.roll;

Methods need to be followed by parentheses.

if (die1 == 1 || die2 == 1)

What is die1 and die2?

PairofDice comp = new comp();

Why do you need a second PairOfDice object?

goldstar at 2007-7-7 14:14:06 > top of Java-index,Archived Forums,Socket Programming...