-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTop_Trumps_Project
149 lines (120 loc) · 4.96 KB
/
Top_Trumps_Project
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
import random
import requests
import time
import sys
# Get a random Pokemon and their stats
def random_pokemon():
pokemon_number = random.randint(1, 151)
url = f"https://pokeapi.co/api/v2/pokemon/{pokemon_number}/"
response = requests.get(url)
pokemon = response.json()
return {
"name": pokemon["name"],
"id": pokemon["id"],
"height": pokemon["height"],
"weight": pokemon["weight"],
}
# Stats available to player and computer for comparison during game play
stat_names = ["id", "height", "weight"]
# Computer chooses a stat at random from available stat names
def comps_choice():
return random.choice(stat_names)
# Player chooses a stat from available stat names
def players_choice():
while True:
chosen_stat = input("Which stat do you want to use? (id / height / weight): ").lower()
if chosen_stat in stat_names:
break
else:
print("Sorry, that's an invalid entry. Please enter \"id\", \"height\" or \"weight\".")
return chosen_stat
# Results of each round printed to screen and win(1)/lose(0)/draw(0) result returned so round wins can be tracked
def result_of_round(player_stat, comp_stat, stat_choice, player_pokemon_name, comp_pokemon_name):
if player_stat > comp_stat:
print(f"\n ---You Win this round!---\nThe {stat_choice} of {player_pokemon_name} is {player_stat} versus {comp_pokemon_name} at {comp_stat}\n")
round_won_by_player = 1
elif player_stat < comp_stat:
print(f"\n ---You Lose this round!---\nThe {stat_choice} of {player_pokemon_name} is {player_stat} versus {comp_pokemon_name} at {comp_stat}\n")
round_won_by_player = 0
else:
print(f"\n ---This round is a Draw!---\nThe {stat_choice} of both {player_pokemon_name} and {comp_pokemon_name} is {player_stat}\n")
round_won_by_player = 0
time.sleep(4)
return round_won_by_player
# Player gets to choose the stat in current round
def player_first():
# Player's random Pokemon assigned
player_pokemon = random_pokemon()
player_pokemon_name = player_pokemon["name"].capitalize()
# Computer's random Pokemon assigned
comp_pokemon = random_pokemon()
comp_pokemon_name = comp_pokemon["name"].capitalize()
time.sleep(2.4)
print(f"You were given {player_pokemon_name}")
time.sleep(2.4)
# Player chooses stat
stat_choice = players_choice()
player_stat = player_pokemon[stat_choice]
comp_stat = comp_pokemon[stat_choice]
time.sleep(2.4)
print(f"Your opponent received {comp_pokemon_name}")
time.sleep(2.4)
# Return the total of player wins for rounds when player chose the stat
wins = result_of_round(player_stat, comp_stat, stat_choice, player_pokemon_name, comp_pokemon_name)
return wins
# Computer gets to choose the stat in current round
def comp_first():
# Player's random Pokemon assigned
player_pokemon = random_pokemon()
player_pokemon_name = player_pokemon["name"].capitalize()
# Computer's random Pokemon assigned
comp_pokemon = random_pokemon()
comp_pokemon_name = comp_pokemon["name"].capitalize()
time.sleep(2.4)
print(f"You were given {player_pokemon_name}")
# Computer chooses stat
stat_choice = comps_choice()
player_stat = player_pokemon[stat_choice]
comp_stat = comp_pokemon[stat_choice]
time.sleep(2.4)
print(f"Your opponent received {comp_pokemon_name}")
time.sleep(2.4)
print(f"They chose the {stat_choice} stat")
time.sleep(2.4)
# Return the total of player wins for rounds when the computer chose the stat
wins = result_of_round(player_stat, comp_stat, stat_choice, player_pokemon_name, comp_pokemon_name)
return wins
# Alternate between player and computer choosing stat when playing multiple rounds
# Track number of round wins
def play_with_rounds(rounds):
total_wins = 0
for Round in range(1, rounds+1):
print(f"\n *** ROUND {Round} ***")
if Round % 2:
total_wins += player_first()
else:
total_wins += comp_first()
print(f"\nYou won {total_wins}/{rounds} rounds.")
def play_again_or_quit():
while True:
play_again = input("\nWould you like to play again? (yes / no): ").lower()
if play_again == "yes":
play()
elif play_again == "no":
sys.exit("Thanks for playing! The game has now been terminated.")
else:
print("Sorry, your entry has not been recognised.\nPlease type \"yes\" or \"no\".")
# Player chooses number of rounds and begins match
def play():
while True:
try:
rounds = int(input("How many rounds would you like to play? (1-5): "))
if 0 < rounds < 6:
break
else:
print("Sorry, that was an invalid entry. Please enter a number between 1 and 5.")
except ValueError:
print("Sorry, that was an invalid entry. Please try again.")
play_with_rounds(rounds)
play_again_or_quit()
play()