Poker Dice

Tags: terminal, game

Future Publishing: The Python Book 2021, 32

Todo

write description

Todo

Add GUI version

poker_dice.py
  1#!/usr/bin/env python2
  2
  3import random
  4from itertools import groupby
  5
  6
  7# SETUP {{{
  8nine = 1
  9ten = 2
 10jack = 3
 11queen = 4
 12king = 5
 13ace = 6
 14
 15names = {
 16    nine:   "9",
 17    ten:    "10",
 18    jack:   "J",
 19    queen:  "Q",
 20    king:   "K",
 21    ace:    "A",
 22        }
 23
 24player_score = 0
 25computer_score = 0
 26# }}}
 27
 28
 29def start(): # {{{
 30    print "Let's play a game of Poker Dice"
 31    while game():
 32        pass
 33    scores()
 34#  }}}
 35def game(): # {{{
 36    print "The computer will help you to throw your 5 dice"
 37    throws()
 38    return play_again()
 39#  }}}
 40def throws(): # {{{
 41    roll_number = 5
 42    dice = roll(roll_number)
 43    dice.sort()
 44    for i in range(len(dice)):
 45        print "Dice", i+1, ":", names[dice[i]]
 46
 47    result = hand(dice)
 48    print "You currenly have", result
 49
 50    while True:
 51        rerolls = raw_input("How many dice do you want to throw again? ")
 52        try:
 53            rerolls = int(rerolls)
 54            if rerolls in range(6):
 55                break
 56        except ValueError:
 57            pass
 58        print "Oops! I didn't understand that. Please enter 1, 2, 3, 4 or 5"
 59
 60
 61    if rerolls == 0:
 62        print "You finish with", result
 63    else:
 64        roll_number = rerolls
 65        dice_rerolls = roll(roll_number)
 66        dice_changes = range(rerolls)
 67        print "Enter the number of a dice to reroll: "
 68        iterations = 0
 69
 70        while iterations < rerolls:
 71            iterations += 1
 72            while True:
 73                selection = raw_input("")
 74                selection = int(selection)
 75                try:
 76                    if selection in (1, 2, 3, 4, 5):
 77                        break
 78                except ValueError:
 79                    pass
 80                print "Oops! I didn't understand that. Please enter 1, 2, 3, 4 or 5"
 81            dice_changes[iterations-1] = selection-1
 82            print "You have changed dice", selection
 83
 84        iterations = 0
 85        while iterations < rerolls:
 86            iterations += 1
 87            replacement = dice_rerolls[iterations-1]
 88            dice[dice_changes[iterations-1]] = replacement
 89
 90        dice.sort()
 91
 92        for i in range(len(dice)):
 93            print "Dice", i+1, ":", names[dice[i]]
 94
 95        result = hand(dice)
 96        print "You finish with", result
 97#  }}}
 98def roll(roll_number): # {{{
 99    numbers = range(1, 7)
100    dice = range(roll_number)
101    iterations = 0
102    while iterations < roll_number:
103        iterations += 1
104        dice[iterations-1] = random.choice(numbers)
105    return dice
106#  }}}
107def hand(dice): # {{{
108    dice_hand = [ len(list(group)) for key, group in groupby(dice)]
109    dice_hand.sort(reverse=True)
110    straight1 = [1, 2, 3, 4, 5]
111    straight2 = [2, 3, 4, 5, 6]
112
113    if dice == straight1 or dice ==  straight2:
114        return "a straight!"
115    elif dice_hand[0] == 5:
116        return "five of a kind!"
117    elif dice_hand[0] == 4:
118        return "four of a kind!"
119    elif dice_hand[0] == 3:
120        if dice_hand[1] == 2:
121            return "a full house!"
122        else:
123            return "three of a kind!"
124    elif dice_hand[0] == 2:
125        if dice_hand[1] == 2:
126            return "two pairs."
127        else:
128            return "one pair."
129    else:
130        return "a high card."
131#  }}}
132def play_again(): # {{{
133    answer = raw_input("Would you like to play again? y/n: ")
134    if answer in ("y", "Y"):
135        return True
136    else:
137        print "Thank you very much for playing or game. See you next time!"
138#  }}}
139def scores(): # {{{
140    global player_score, computer_score
141    print "HIGH SCORES"
142    print "Player", player_score
143    print "Computer", computer_score
144#  }}}
145
146if __name__ == "__main__":
147    start()