File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Pig Game Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Pig Game" Watch "Pig Game" New topic

Pig Game

Andy Hammersmith

Joined: Sep 30, 2012
Posts: 13
For my beginning java class we are suppose to make a pig game following these guidelines:
- You are going to make a computer player vs human player game using two dice
- On each turn, the player rolls a pair of dice and adds up his or her points.
- Whoever reaches 100 points first, wins. You are allowed to go over.
- If a player rolls a 1, he or she loses all points for that round and the dice go to the other player
- If a player rolls two 1's in one turn, the player loses all points earned so far in the game and loses control of the dice
- The player may voluntarily turn over the dice after each roll. So the player must decide to either roll again (be a pig) and risk losing points, or give up the dice, possibly letting the other player win.
- Setup the computer player so that it always gives up the dice after getting 20 or more point in a round.

I have the code all written but for some reason I am not able to get it to continue on past the first two turns. I would very much appreciate help. Any suggestions on how to condense it would also be appreciated.
My code is as follows:
Kemal Sokolovic

Joined: Jun 19, 2010
Posts: 825

Are you sure you can even compile that code you posted?

The quieter you are, the more you are able to hear.
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

i got 11 compiler errors.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
arulk pillai
Ranch Hand

Joined: May 31, 2007
Posts: 3371
Break down your code into smaller sub functions. It will be easier to read, debug, and some functions can be reused as well.

[] | [Java job hunting know how] | [Java resumes]
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46349
Welcome to the Ranch
I always worry when I see a main method that long. I worry when I see a main method longer than 1 statement, in fact.
Not only should you create lots of small methods, but I think you should also create Player and Die classes, because you are supposed to be learning object‑oriented programming and you can&#s2019;t be object‑oriented without objects to create.
I agree. Here's the link:
subject: Pig Game
It's not a secret anymore!