Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
155 lines (117 loc) · 4.08 KB
/
Copy pathapp.py
File metadata and controls
155 lines (117 loc) · 4.08 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import os
from turtle import color
from termcolor import colored
from BeetleVisualiser import draw_beetle, BeetleParts
import random
from time import sleep
def prompt_main_menu():
print(colored(
"""
Please choose from the following:
=================================
1. Roll the dice.
2. Visualise your beetle.
3. Visualise computer's beetle.
""", "cyan"))
response = input("> ")
if len(response) == 0:
print(colored("Please submit a response.", "red"))
return prompt_main_menu()
try:
responseInt = int(response)
except:
print(colored("Please enter a number from the options.", "red"))
return prompt_main_menu()
return responseInt
def process_dice_result(result):
# return [Bool, BeetlePart]
qualifyingParts = DICE_THROW_RESULTS[str(result)]
if turnToPlay == "COMPUTER":
stats = computerStats
if turnToPlay == "PLAYER":
stats = playerStats
if result != 6:
if BeetleParts.BODY not in stats['beetleParts']:
return [False, None]
if result == 2 or result == 3:
if BeetleParts.HEAD not in stats['beetleParts']:
return [False, None]
for qualifyingPart in qualifyingParts:
if qualifyingPart not in stats['beetleParts']:
partToAppend = qualifyingPart
else:
partToAppend = None
continue
if partToAppend == None:
return [False, partToAppend]
else:
stats['beetleParts'].append(partToAppend)
return [True, partToAppend]
def computer_turn_to_play():
os.system("cls")
print(colored("Computer is rolling dice...", "grey"))
diceResult = roll_dice()
processResult = process_dice_result(diceResult)
sleep(2)
print(colored(f"Computer rolled a {diceResult}!", "yellow"))
if processResult[0] == True:
print(colored(F"It drew a {processResult[1]['key']}.", "yellow"))
else:
print(colored("Nothing new has been drawn.", "yellow"))
def player_turn_to_play():
os.system("cls")
mainMenuResponse = prompt_main_menu()
if mainMenuResponse == 1:
print(colored("Rolling dice...", "grey"))
diceResult = roll_dice()
processResult = process_dice_result(diceResult)
sleep(2)
print(colored(f"You rolled a {diceResult}!", "yellow"))
if processResult[0] == True:
print(colored(F"You drew a {processResult[1]['key']}."))
else:
print(colored("Nothing new has been drawn.", "yellow"))
elif mainMenuResponse == 2:
draw_beetle(playerStats["beetleParts"], "Player Beetle")
return player_turn_to_play()
else:
draw_beetle(computerStats["beetleParts"], "Computer Beetle")
return player_turn_to_play()
def StartGame():
global turnToPlay
if turnToPlay == "PLAYER":
player_turn_to_play()
turnToPlay = "COMPUTER"
else:
computer_turn_to_play()
turnToPlay = "PLAYER"
if len(playerStats["beetleParts"]) == 13:
print(
colored("Congratulations! You won!\nRelaunch this app to play again.", "green"))
return
if len(computerStats["beetleParts"]) == 13:
print(
colored("Oh no! Unfortunately, you lost.\nRelaunch this app to play again.", "red"))
return
sleep(5)
StartGame()
def roll_dice():
return random.randint(1, 6)
def main():
os.system("color")
print(colored("Welcome!\nReady to play? Press ENTER to begin.", "cyan"))
input()
StartGame()
if __name__ == "__main__":
DICE_THROW_RESULTS = {
"6": [BeetleParts.BODY],
"5": [BeetleParts.HEAD],
"4": [BeetleParts.LEFT_LEG_TOP, BeetleParts.LEFT_LEG_MIDDLE, BeetleParts.LEFT_LEG_BOTTOM, BeetleParts.RIGHT_LEG_TOP, BeetleParts.RIGHT_LEG_MIDDLE, BeetleParts.RIGHT_LEG_BOTTOM],
"3": [BeetleParts.LEFT_EYE, BeetleParts.RIGHT_EYE],
"2": [BeetleParts.LEFT_ANTENNA, BeetleParts.RIGHT_ANTENNA],
"1": [BeetleParts.TAIL]
}
playerStats = {"beetleParts": []}
computerStats = {"beetleParts": []}
turnToPlay = "COMPUTER"
main()