-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathcube.py
executable file
·131 lines (114 loc) · 4.35 KB
/
cube.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python3
"""
CUBE
Converted from BASIC to Python by Trevor Hobson
"""
import random
from typing import Tuple
def mine_position() -> Tuple[int, int, int]:
return (random.randint(1, 3), random.randint(1, 3), random.randint(1, 3))
def parse_move(move: str) -> Tuple[int, int, int]:
coordinates = [int(item) for item in move.split(",")]
if len(coordinates) == 3:
return tuple(coordinates) # type: ignore
raise ValueError
def play_game() -> None:
"""Play one round of the game"""
money = 500
print("\nYou have", money, "dollars.")
while True:
mines = []
for _ in range(5):
while True:
mine = mine_position()
if (
mine not in mines
and mine != (1, 1, 1)
and mine != (3, 3, 3)
):
break
mines.append(mine)
wager = -1
while wager == -1:
try:
wager = int(input("\nHow much do you want to wager? "))
if not 0 <= wager <= money:
wager = -1
print("Tried to fool me; bet again")
except ValueError:
print("Please enter a number.")
prompt = "\nIt's your move: "
position = (1, 1, 1)
while True:
move = (-1, -1, -1)
while move == (-1, -1, -1):
try:
move = parse_move(input(prompt))
except (ValueError, IndexError):
print("Please enter valid coordinates.")
if (
abs(move[0] - position[0])
+ abs(move[1] - position[1])
+ abs(move[2] - position[2])
) > 1:
print("\nIllegal move. You lose")
money = money - wager
break
elif (
move[0] not in [1, 2, 3]
or move[1] not in [1, 2, 3]
or move[2] not in [1, 2, 3]
):
print("\nIllegal move. You lose")
money = money - wager
break
elif move == (3, 3, 3):
print("\nCongratulations!")
money = money + wager
break
elif move in mines:
print("\n******BANG******")
print("You lose!")
money = money - wager
break
else:
position = move
prompt = "\nNext move: "
if money > 0:
print("\nYou now have", money, "dollars.")
if not input("Do you want to try again ").lower().startswith("y"):
break
else:
print("\nYou bust.")
print("\nTough luck")
print("\nGoodbye.")
def print_instructions() -> None:
print("\nThis is a game in which you will be playing against the")
print("random decisions of the computer. The field of play is a")
print("cube of side 3. Any of the 27 locations can be designated")
print("by inputing three numbers such as 2,3,1. At the start,")
print("you are automatically at location 1,1,1. The object of")
print("the game is to get to location 3,3,3. One minor detail:")
print("the computer will pick, at random, 5 locations at which")
print("it will plant land mines. If you hit one of these locations")
print("you lose. One other detail: You may move only one space")
print("in one direction each move. For example: From 1,1,2 you")
print("may move to 2,1,2 or 1,1,3. You may not change")
print("two of the numbers on the same move. If you make an illegal")
print("move, you lose and the computer takes the money you may")
print("have bet on that round.\n")
print("When stating the amount of a wager, print only the number")
print("of dollars (example: 250) you are automatically started with")
print("500 dollars in your account.\n")
print("Good luck!")
def main() -> None:
print(" " * 34 + "CUBE")
print(" " * 15 + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n")
if input("Do you want to see the instructions ").lower().startswith("y"):
print_instructions()
keep_playing = True
while keep_playing:
play_game()
keep_playing = input("\nPlay again? (yes or no) ").lower().startswith("y")
if __name__ == "__main__":
main()