New Python Pass the Pigs Game

So in honor of coaching a Python introduction class with Devoxx4Kids this weekend, I thought I would update my pass the pigs game and add in the scoring and just rewrite it a little.

If you aren’t familiar with this fun little game, here’s the gist-  It consists of two pigs that you roll like dice.  Depending on how the pigs land, you get a particular score-

passpigs

I’ve simplified my original script even a bit more and added a function to set up the roll.  I’ve added the scoring in, along with information on if you roll doubles and the game will break out of the roll if a Pigout or Oinker, (referred to as a “Makin Bacon” and a “Sider” in the top image) is “rolled”.

>vi pig_gm.py

#!/usr/bin/python3

#Pass the Pigs Game
import random
def setup_roll():
    print(' ')
    print('Your Roll is:')
def pig_pass():
 for x in range(1, 3): #two dice rolls
 dice_roll = random.randint(1, 6)
 if dice_roll == 1:
 print('Razorback, 5 pts! If you get doubles, 20 pts!')
 elif dice_roll == 2:
 print('Trotter, 5 pts! If you get doubles, 20 pts!')
 elif dice_roll == 3:
 print('Snouter, 10 pts! If you get doubles, 40 pts!')
 elif dice_roll == 4:
 print('Leaning Jowl, 15 pts! If you get doubles, 60 pts!')
 elif dice_roll == 5:
 print('Pig Out, Back to ZERO pts and Next Players turn unless you get doubles, then you get 1 pt!')
 break
 elif dice_roll == 6:
 print('Oinker, Back to Zero and Next Players turn!')
 break

setup_roll()
pig_pass()

Once we’re done coding this, you can save by hitting the <Esc> key, then “:wq” to tell the vi editor to “write” and “quit”, (for those of you that are “vi newbies”.

Let’s change the permissions on our file so it’s easy to execute without power privs:

chmod 775 pig_gm.py

To execute the game, just run the following from the directory you saved the program in:

./pig_gm.py

The output from the game looks like the following:

 ./pig_gm.py

Your Roll is:
Leaning Jowl, 15 pts! If you get doubles, 60 pts!
Trotter, 5 pts! If you get doubles, 20 pts!
> ./pig_gm.py

Your Roll is:
Trotter, 5 pts! If you get doubles, 20 pts!
Oinker, Back to Zero and Next Players turn!
> ./pig_gm.py

Your Roll is:
Trotter, 5 pts! If you get doubles, 20 pts!
Pig Out, Back to ZERO pts and Next Players turn unless you get doubles, then you get 1 pt!

You get to add up your points unless you get an Oinker or a Pigout, (unless it’s doubles Pigout) and you can play the game just like you had the lovely plastic pigs!

See everyone at Oracle Open World soon and enjoy!

OOW-01

Print Friendly
October 15th, 2015 by

facebook comments:

  • Facebook
  • Google+
  • LinkedIn
  • Twitter