programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Ron McLeod
• Paul Clapham
• Devaka Cooray
• Liutauras Vilda
Sheriffs:
• Jeanne Boyarsky
• paul wheaton
• Henry Wong
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Tim Moores
• Carey Brown
• Mikalai Zaikin
Bartenders:
• Lou Hamers
• Piet Souris
• Frits Walraven

# Creating the game Pig using only while and if statement

Greenhorn
Posts: 5
• Number of slices to send:
Optional 'thank-you' note:
For my high school AP Computer Science class we have been instructed to create a game called PIG using only if and while loops.

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 a two 1s 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. Therefore, 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.

Whenever I roll a one or two ones it does not change to the computer's turn...

Sheriff
Posts: 17665
300
• Number of slices to send:
Optional 'thank-you' note:
I don't get how you can roll a one if you're rolling a pair of dice. The minimum roll with a pair of dice is two, for snake eyes. Does the rule actually mean if one of the die is a one? Like if you roll a one and a six?

Spencer Brunnert
Greenhorn
Posts: 5
• Number of slices to send:
Optional 'thank-you' note:
"Does the rule actually mean if one of the die is a one? Like if you roll a one and a six?"

Yes.

Junilu Lacar
Sheriff
Posts: 17665
300
• Number of slices to send:
Optional 'thank-you' note:
One thing that will help you get a handle on what your program is doing is to break up that humungous mass of code that's in your main() method into smaller pieces that each dealt with one small piece of the problem. This is called functional decomposition.  Putting everything into main is a pain.

Have you been taught how to use methods to make it easier to tackle complexity in your program?

If you did that, you could write something like this:

The above code also assumes you define a Player class to represent a player in the game, whether its the computer or the human player.

Does that make any sense to you, given what you've learned so far or is it a little too advanced?

Spencer Brunnert
Greenhorn
Posts: 5
• Number of slices to send:
Optional 'thank-you' note:
That made little to no sense to me, sorry!

Junilu Lacar
Sheriff
Posts: 17665
300
• Number of slices to send:
Optional 'thank-you' note:
That's OK. It's a somewhat more sophisticated technique than what you've been taught, which is unfortunate because you're only learning how to write very bad code right now. Very, very bad code.

If you defined your variables and a couple of methods like this:

This is an example of how you would use methods to break down your problem into smaller pieces. The code in your main method would just call the methods. You might notice that you already have the logic for rolledOne() and rolledSnakeEyes() somewhere in the Big Ball of Mud that's in your main() method right now.

Notice, too, that I have given the class a name of "Pig" instead of the non-descriptive "fiveelevendriver" name that also violates Java naming conventions. Class names in Java should start with a capital letter.  The other class name you use that doesn't follow that convention either is the "fiveeleven" class that represents a dice.  I have used the class name "Dice" instead in my example code above.

Spencer Brunnert
Greenhorn
Posts: 5
• Number of slices to send:
Optional 'thank-you' note:
I better stick to the way my teacher would like me to do it, but I think your method does make things a lot simpler and I will try to implement your method in future programming projects.

Thank you

Marshal
Posts: 79654
381
• Number of slices to send:
Optional 'thank-you' note:
What is unusual about only using if and while statements? That is plain simple structured programming. Back in 1966, Böhm and Jacopini showed that all programs con be constructed with sequence selection and iteration only. Since if and while are the most basic forms of selection and iteration, they are what one would choose as the most general forms of those constructs. Every program can be constructed with if and while only.

Corrado Böhm and Giuseppe Jacopini. Flow Diagrams, Turing Machines and Languages With Only Two Formation Rules. Communications of the Association of Computing Machinery, 9:366–371, 1966. doi:10.1145/355592.365646

That is a classic paper, though it can be a little difficult to read.

 I AM MIGHTY! Especially when I hold this tiny ad: We need your help - Coderanch server fundraiser https://coderanch.com/wiki/782867/Coderanch-server-fundraiser