CHO HAN
Cho-han is a dice game played in gambling houses of feudal Japan. Two six-sided dice are rolled in a cup, and gamblers must guess if the sum is even (cho) or odd (han). The house takes a small cut of all winnings. The simple random number generation and basic math used to determine odd or even sums make this project especially suitable for beginners. More information about Cho-han can be found at https://en.wikipedia.org/wiki/Cho-han.
cho_han.py
1"""Cho-Han, by Al Sweigart al@inventwithpython.com
2The traditional Japanese dice game of even-odd.
3View this code athttps://nostarch.com/big-book-small-python-projects
4Tags: short, beginner, game"""
5
6import random, sys
7
8JAPANESE_NUMBERS = {1: 'ICHI', 2: 'NI', 3: 'SAN',
9 4: 'SHI', 5: 'GO', 6: 'ROKU'}
10
11print('''Cho-Han, by Al Sweigart al@inventwithpython.com
12
13In this traditional Japanese dice game, two dice are rolled in a bamboo
14cup by the dealer sitting on the floor. The player must guess if the
15dice total to an even (cho) or odd (han) number.
16''')
17
18purse = 5000
19while True: # Main game loop.
20 # Place your bet:
21 print('You have', purse, 'mon. How much do you bet? (or QUIT)')
22 while True:
23 pot = input('> ')
24 if pot.upper() == 'QUIT':
25 print('Thanks for playing!')
26 sys.exit()
27 elif not pot.isdecimal():
28 print('Please enter a number.')
29 elif int(pot) > purse:
30 print('You do not have enough to make that bet.')
31 else:
32 # This is a valid bet.
33 pot = int(pot) # Convert pot to an integer.
34 break # Exit the loop once a valid bet is placed.
35
36 # Roll the dice.
37 dice1 = random.randint(1, 6)
38 dice2 = random.randint(1, 6)
39
40 print('The dealer swirls the cup and you hear the rattle of dice.')
41 print('The dealer slams the cup on the floor, still covering the')
42 print('dice and asks for your bet.')
43 print()
44 print(' CHO (even) or HAN (odd)?')
45
46 # Let the player bet cho or han:
47 while True:
48 bet = input('> ').upper()
49 if bet != 'CHO' and bet != 'HAN':
50 print('Please enter either "CHO" or "HAN".')
51 continue
52 else:
53 break
54
55 # Reveal the dice results:
56 print('The dealer lifts the cup to reveal:')
57 print(' ', JAPANESE_NUMBERS[dice1], '-', JAPANESE_NUMBERS[dice2])
58 print(' ', dice1, '-', dice2)
59
60 # Determine if the player won:
61 rollIsEven = (dice1 + dice2) % 2 == 0
62 if rollIsEven:
63 correctBet = 'CHO'
64 else:
65 correctBet = 'HAN'
66
67 playerWon = bet == correctBet
68
69 # Display the bet results:
70 if playerWon:
71 print('You won! You take', pot, 'mon.')
72 purse = purse + pot # Add the pot from player's purse.
73 print('The house collects a', pot // 10, 'mon fee.')
74 purse = purse - (pot // 10) # The house fee is 10%.
75 else:
76 purse = purse - pot # Subtract the pot from player's purse.
77 print('You lost!')
78
79 # Check if the player has run out of money:
80 if purse == 0:
81 print('You have run out of money!')
82 print('Thanks for playing!')
83 sys.exit()