The objective of this lab is to learn how to use functions to implement a solitaire version of a game called Pig. In Pig Solitaire, our objective is to reach a given goal score g within n turns. For example, you win the game if you score 100 points within 5 turns. Or, 50 points within 2 turns. You get to set the game parameters. In Pig Solitaire, during each turn, a player repeatedly rolls a die until either a 1 is rolled or the player holds and scores the sum of the rolls (i.e., the turn total). At any time during a player’s turn, the player is faced with two choices: roll or hold. If the player rolls a 1, the player scores nothing and a new turn begins. If the player rolls a number other than 1, the number is added to the player’s turn total and the player’s turn continues. If the player instead chooses to hold, the turn total is added to the player’s score and a new turn begins
Problem:
Pig Solitaire 1. Design and implement a Python program to implement Pig Solitaire. Your program must use the following three functions: (a) roll_die()—returns the result of a single roll of the die. (b)play_single_turn(total)—plays a single turn of Pig Solitaire and returns the number of points the player achieved on this turn. This function expects a value that will be stored in the parameter total, which denotes the total number of points the player has achieved so far. (c) main()—the foundation of the program and contains all of the code that does not appear in the roll_die or play_single_turn functions.
import random
g = input('How many points to win?: ')
n = input('What is the maximum number of turns?: ')
def roll_die():
# Roll the die
return random.randint(1,6)
def play_single_turn(total):
# Play a single turn
play = 'y'
turn_total = 0
while play != 'h':
y = roll_die()
if y == 1:
# Turn over - got a 1!
turn_total = 0
print "Total points:\t", total
print "Roll:\t", y
print "Your turn is over!"
print "Turn total: \t", turn_total
print raw_input("Press <Enter> to continue... ")
play = 'h'
else:
# Play turn
turn_total = turn_total + y
print "Total points:\t", total
print "Roll:\t", y
print "Turn total: \t", turn_total
play = raw_input("Press <Enter> to roll again or 'h' to hold: \n")
return turn_total
def main():
total = 0
turn = 1
while turn <= n:
print "\n*** TURN %s ***\n" % turn
turn += 1
x = play_single_turn(total)
print "Total points = ", x
total = total + x
if total >= g:
print "\nYOU WIN!"
print "It took you %s turns to reach %s points." % ((turn-1),g)
break
else:
continue
else:
print "\nYOU LOSE!"
print "You failed to reach %s points in %s turns." % (g,(turn-1))
if __name__ == '__main__':
main()
Output:
How many points to win?: 50
What is the maximum number of turns?: 3
*** TURN 1 ***
Total points: 0
Roll: 2
Turn total: 2
Press to roll again or 'h' to hold:
Total points: 0
Roll: 5
Turn total: 7
Press to roll again or 'h' to hold:
Total points: 0
Roll: 3
Turn total: 10
Press to roll again or 'h' to hold:
Total points: 0
Roll: 6
Turn total: 16
Press to roll again or 'h' to hold:
h
Total points = 16
*** TURN 2 ***
Total points: 16
Roll: 1
Your turn is over!
Turn total: 0
Press to continue...
Total points = 0
*** TURN 3 ***
Total points: 16
Roll: 4
Turn total: 4
Press to roll again or 'h' to hold:
Total points: 16
Roll: 1
Your turn is over!
Turn total: 0
Press to continue...
Total points = 0
YOU LOSE!
You failed to reach 50 points in 3 turns.
>>>
How many points to win?: 50
What is the maximum number of turns?: 3
*** TURN 1 ***
Total points: 0
Roll: 3
Turn total: 3
Press to roll again or 'h' to hold:
Total points: 0
Roll: 2
Turn total: 5
Press to roll again or 'h' to hold:
Total points: 0
Roll: 3
Turn total: 8
Press to roll again or 'h' to hold:
Total points: 0
Roll: 6
Turn total: 14
Press to roll again or 'h' to hold:
Total points: 0
Roll: 5
Turn total: 19
Press to roll again or 'h' to hold:
Total points: 0
Roll: 3
Turn total: 22
Press to roll again or 'h' to hold:
h
Total points = 22
*** TURN 2 ***
Total points: 22
Roll: 2
Turn total: 2
Press to roll again or 'h' to hold:
Total points: 22
Roll: 4
Turn total: 6
Press to roll again or 'h' to hold:
Total points: 22
Roll: 5
Turn total: 11
Press to roll again or 'h' to hold:
Total points: 22
Roll: 4
Turn total: 15
Press to roll again or 'h' to hold:
Total points: 22
Roll: 4
Turn total: 19
Press to roll again or 'h' to hold:
h
Total points = 19
*** TURN 3 ***
Total points: 41
Roll: 5
Turn total: 5
Press to roll again or 'h' to hold:
Total points: 41
Roll: 2
Turn total: 7
Press to roll again or 'h' to hold:
Total points: 41
Roll: 6
Turn total: 13
Press to roll again or 'h' to hold:
h
Total points = 13
YOU WIN!
It took you 3 turns to reach 50 points.
Original link
