Skip to content

Commit 9a4fc2e

Browse files
committed
Day 14 and 15. Created Rock Paper Scissors yesterday and then today
created a 15 way game using the battle table csv file they gave us to dictate which player won and what they can play.
1 parent 7ce27f6 commit 9a4fc2e

File tree

4 files changed

+217
-1
lines changed

4 files changed

+217
-1
lines changed

days/13-15-text-games/dnd_game/program.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from actors import Creature, Wizard, Dragon
22
import random
33

4+
45
def main():
56
print_header()
67
game_loop()
@@ -60,4 +61,5 @@ def game_loop():
6061

6162

6263
if __name__ == '__main__':
63-
main()
64+
main()
65+
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import csv
2+
import random
3+
4+
FILE_NAME = "data/battle-table.csv"
5+
win_or_lose = {}
6+
7+
8+
class Player:
9+
def __init__(self, name, games_won = 0):
10+
self.name = name
11+
self.games_won = games_won
12+
13+
def increase_games_won(self):
14+
self.games_won += 1
15+
16+
def get_games_won(self):
17+
return self.games_won
18+
19+
20+
def print_header():
21+
print('-----------------------------------')
22+
print(' Fifteen Way Rock, Paper, Scissors ')
23+
print('-----------------------------------')
24+
print()
25+
26+
27+
def store_the_options():
28+
with open(FILE_NAME) as fin:
29+
reader = csv.DictReader(fin)
30+
31+
# Date from the csv file:
32+
# OrderedDict([('Attacker', 'Rock'), ('Rock', 'draw'), ('Gun', 'lose'), ('Lightning', 'lose'), ('Devil', 'lose'), ('Dragon', 'lose'), ('Water', 'lose'), ('Air', 'lose'), ('Paper', 'lose'), ('Sponge', 'win'), ('Wolf', 'win'), ('Tree', 'win'), ('Human', 'win'), ('Snake', 'win'), ('Scissors', 'win'), ('Fire', 'win')])
33+
# OrderedDict([('Attacker', 'Gun'), ('Rock', 'win'), ('Gun', 'draw'), ('Lightning', 'lose'), ('Devil', 'lose'), ('Dragon', 'lose'), ('Water', 'lose'), ('Air', 'lose'), ('Paper', 'lose'), ('Sponge', 'lose'), ('Wolf', 'win'), ('Tree', 'win'), ('Human', 'win'), ('Snake', 'win'), ('Scissors', 'win'), ('Fire', 'win')])
34+
35+
for row in reader:
36+
attacker_name = row['Attacker']
37+
del row['Attacker']
38+
del row[attacker_name]
39+
40+
win_or_lose[attacker_name] = {}
41+
42+
for k in row.keys():
43+
can_defeat = row[k].strip().lower()
44+
win_or_lose[attacker_name][k] = can_defeat
45+
46+
47+
def get_players_name():
48+
return input("What is your name? ").strip()
49+
50+
51+
def player_chooses_roll(name):
52+
roll_choice = ", ".join(win_or_lose)
53+
choice = input(f"Which do you choose {name}? {roll_choice}")
54+
while choice not in win_or_lose:
55+
choice = input(f"Did you spell it wrong? Which do you choose {name}?")
56+
return choice
57+
58+
59+
def game_loop(player1, player2):
60+
count = 0
61+
max_games = 3
62+
63+
while count < max_games:
64+
p1_roll = player_chooses_roll(player1.name)
65+
p2_roll = random.choice(list(win_or_lose.keys()))
66+
67+
outcome = win_or_lose[p1_roll][p2_roll]
68+
69+
print(f"Player1 ({player1.name}) rolled {p1_roll} against Player2 ({player2.name}) {p2_roll}.")
70+
71+
if outcome == "draw":
72+
print("You tied! Adding a game to the play.")
73+
max_games += 1
74+
elif outcome == "win":
75+
player1.increase_games_won()
76+
print(f"{player1.name} won!")
77+
else:
78+
player2.increase_games_won()
79+
print(f"{player2.name} won!")
80+
count += 1
81+
82+
print()
83+
print(f"Game over! Out of a total of {count} games:")
84+
if player1.get_games_won() == player2.get_games_won():
85+
print(" You tied!")
86+
elif player1.get_games_won() > player2.get_games_won():
87+
print(f" Player1 ({player1.name}) won!")
88+
else:
89+
print(f" Player2 ({player2.name}) won!")
90+
91+
92+
def main():
93+
print_header()
94+
95+
store_the_options()
96+
97+
player1 = Player(get_players_name())
98+
player2 = Player("computer")
99+
100+
print(f"Welcome to the game {player1.name}!")
101+
print()
102+
103+
game_loop(player1, player2)
104+
105+
106+
if __name__ == '__main__':
107+
main()
108+
109+
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import random
2+
3+
4+
class Player:
5+
def __init__(self, name, games_won = 0):
6+
self.name = name
7+
self.games_won = games_won
8+
9+
def increase_games_won(self):
10+
self.games_won += 1
11+
12+
def get_games_won(self):
13+
return self.games_won
14+
15+
16+
def main():
17+
print_header()
18+
19+
rolls = build_the_three_rolls()
20+
21+
name = get_players_name()
22+
23+
player1 = Player(name)
24+
player2 = Player("computer")
25+
26+
game_loop(player1, player2, rolls)
27+
28+
29+
def print_header():
30+
print('---------------------------------')
31+
print(' Rock, Paper, Scissors ')
32+
print('---------------------------------')
33+
34+
35+
def get_players_name():
36+
name = input("What is your name? ")
37+
print(f"Welcome to the game {name}!")
38+
print()
39+
return name
40+
41+
42+
def build_the_three_rolls():
43+
return ['rock', 'paper', 'scissor']
44+
45+
46+
def player_chooses_roll(name, rolls):
47+
roll_choice = ", ".join(rolls)
48+
choice = input(f"Which do you choose {name}? {roll_choice}")
49+
while not choice in rolls:
50+
choice = input(f"Which do you choose {name}? {roll_choice}")
51+
return choice
52+
53+
54+
def can_defeat(p1_roll, p2_roll):
55+
if p1_roll == "rock" and p2_roll == "rock" or \
56+
p1_roll == "paper" and p2_roll == "paper" or \
57+
p1_roll == "scissor" and p2_roll == "scissor":
58+
return -1 # tie
59+
elif p1_roll == "rock" and p2_roll == "scissor" or \
60+
p1_roll == "paper" and p2_roll == "rock" or \
61+
p1_roll == "scissor" and p2_roll == "paper":
62+
return 1 # player one wins
63+
else:
64+
return 2 # player two wins
65+
66+
67+
def game_loop(player1, player2, rolls):
68+
count = 0
69+
max_games = 3
70+
71+
while count < max_games:
72+
p2_roll = random.choice(rolls)
73+
p1_roll = player_chooses_roll(player1.name, rolls)
74+
75+
outcome = can_defeat(p1_roll, p2_roll)
76+
77+
print(f"Player1 ({player1.name}) rolled {p1_roll} against Player2 ({player2.name}) {p2_roll}")
78+
79+
if outcome == -1:
80+
print("You tied!")
81+
max_games += 1
82+
elif outcome == 1:
83+
player1.increase_games_won()
84+
print(f"{player1.name} won!")
85+
else:
86+
player2.increase_games_won()
87+
print(f"{player2.name} won!")
88+
count += 1
89+
90+
print()
91+
print(f"Game over! Out of a total of {count} games")
92+
if player1.get_games_won() == player2.get_games_won():
93+
print("you tied!")
94+
elif player1.get_games_won() > player2.get_games_won():
95+
print(f"Player1 ({player1.name}) won!")
96+
else:
97+
print(f"Player2 ({player2.name}) won!")
98+
99+
100+
if __name__ == '__main__':
101+
main()
102+
103+
104+
105+

days/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)