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.
When you run chohan.py, the output will look like this:
Cho-Han, by Al Sweigart [email protected]
In this traditional Japanese dice game, two dice are rolled in a bamboo
cup by the dealer sitting on the floor. The player must guess if the
dice total to an even (cho) or odd (han) number.
You have 5000 mon. How much do you bet? (or QUIT)
> 400
The dealer swirls the cup and you hear the rattle of dice.
The dealer slams the cup on the floor, still covering the
dice and asks for your bet.
CHO (even) or HAN (odd)?
> cho
The dealer lifts the cup to reveal:
GO - GO
5 - 5
You won! You take 800 mon.
The house collects a 40 mon fee.
--snip--
The random.randint(1, 6)
call returns a random integer between 1
and 6
, making it ideal for representing a six-sided die roll. However, we also need to display the Japanese words for the numbers one to six. Instead of having an if
statement followed by five elif
statements, we have a dictionary, stored in JAPANESE_NUMBERS
, that maps the integers 1
to 6
to strings of the Japanese words. This is how line 57’s JAPANESE_NUMBERS[dice1]
and JAPANESE_NUMBERS[dice2]
can display the Japanese words for the dice results in just one line of code.
1. """Cho-Han, by Al Sweigart [email protected]
2. The traditional Japanese dice game of even-odd.
3. View this code athttps://nostarch.com/big-book-small-python-projects
4. Tags: short, beginner, game"""
5.
6. import random, sys
7.
8. JAPANESE_NUMBERS = {1: 'ICHI', 2: 'NI', 3: 'SAN',
9. 4: 'SHI', 5: 'GO', 6: 'ROKU'}
10.
11. print('''Cho-Han, by Al Sweigart [email protected]
12.
13. In this traditional Japanese dice game, two dice are rolled in a bamboo
14. cup by the dealer sitting on the floor. The player must guess if the
15. dice total to an even (cho) or odd (han) number.
16. ''')
17.
18. purse = 5000
19. while 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()
After entering the source code and running it a few times, try making experimental changes to it. On your own, you can also try to figure out how to do the following:
Try to find the answers to the following questions. Experiment with some modifications to the code and rerun the program to see what effect the changes have.
random.randint(1, 6)
on line 37 to random.randint(1, 1)
?pot // 10
on line 73 (not line 74) to 0
?