Skip to content

Commit d58f3e9

Browse files
committed
rock paper scissors
1 parent 76da91c commit d58f3e9

File tree

6 files changed

+171
-31
lines changed

6 files changed

+171
-31
lines changed
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,17 @@
1-
Attacker,Rock,Gun,Lightning,Devil,Dragon,Water,Air,Paper,Sponge,Wolf,Tree,Human,Snake,Scissors,FireRock,draw,lose,lose,lose,lose,lose,lose,lose,win,win,win,win,win,win,winGun,win,draw,lose,lose,lose,lose,lose,lose,lose,win,win,win,win,win,winLightning,win,win,draw,lose,lose,lose,lose,lose,lose,lose,win,win,win,win,winDevil,win,win,win,draw,lose,lose,lose,lose,lose,lose,lose,win,win,win,winDragon,win,win,win,win,draw,lose,lose,lose,lose,lose,lose,lose,win,win,winWater,win,win,win,win,win,draw,lose,lose,lose,lose,lose,lose,lose,win,winAir,win,win,win,win,win,win,draw,lose,lose,lose,lose,lose,lose,lose,winPaper,win,win,win,win,win,win,win,draw,lose,lose,lose,lose,lose,lose,loseSponge,lose,win,win,win,win,win,win,win,draw,lose,lose,lose,lose,lose,loseWolf,lose,lose,win,win,win,win,win,win,win,draw,lose,lose,lose,lose,loseTree,lose,lose,lose,win,win,win,win,win,win,win,draw,lose,lose,lose,loseHuman,lose,lose,lose,lose,win,win,win,win,win,win,win,draw,lose,lose,loseSnake,lose,lose,lose,lose,lose,win,win,win,win,win,win,win,draw,lose,loseScissors,lose,lose,lose,lose,lose,lose,win,win,win,win,win,win,win,draw,loseFire,lose,lose,lose,lose,lose,lose,lose,win,win,win,win,win,win,win,draw
1+
Attacker,Rock,Gun,Lightning,Devil,Dragon,Water,Air,Paper,Sponge,Wolf,Tree,Human,Snake,Scissors,Fire
2+
Rock,draw,lose,lose,lose,lose,lose,lose,lose,win,win,win,win,win,win,win
3+
Gun,win,draw,lose,lose,lose,lose,lose,lose,lose,win,win,win,win,win,win
4+
Lightning,win,win,draw,lose,lose,lose,lose,lose,lose,lose,win,win,win,win,win
5+
Devil,win,win,win,draw,lose,lose,lose,lose,lose,lose,lose,win,win,win,win
6+
Dragon,win,win,win,win,draw,lose,lose,lose,lose,lose,lose,lose,win,win,win
7+
Water,win,win,win,win,win,draw,lose,lose,lose,lose,lose,lose,lose,win,win
8+
Air,win,win,win,win,win,win,draw,lose,lose,lose,lose,lose,lose,lose,win
9+
Paper,win,win,win,win,win,win,win,draw,lose,lose,lose,lose,lose,lose,lose
10+
Sponge,lose,win,win,win,win,win,win,win,draw,lose,lose,lose,lose,lose,lose
11+
Wolf,lose,lose,win,win,win,win,win,win,win,draw,lose,lose,lose,lose,lose
12+
Tree,lose,lose,lose,win,win,win,win,win,win,win,draw,lose,lose,lose,lose
13+
Human,lose,lose,lose,lose,win,win,win,win,win,win,win,draw,lose,lose,lose
14+
Snake,lose,lose,lose,lose,lose,win,win,win,win,win,win,win,draw,lose,lose
15+
Scissors,lose,lose,lose,lose,lose,lose,win,win,win,win,win,win,win,draw,lose
16+
Fire,lose,lose,lose,lose,lose,lose,lose,win,win,win,win,win,win,win,draw
17+
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
1-
import csv
1+
import csv
2+
23

34
def read_rolls():
4-
with open('battle-table.csv') as fin:
5+
with open("battle-table.csv") as fin:
56
reader = csv.DictReader(fin)
67
for row in reader:
78
read_roll(row)
89

910

1011
def read_roll(row: dict):
11-
name = row['Attacker']
12-
del row['Attacker']
12+
name = row["Attacker"]
13+
del row["Attacker"]
1314
del row[name]
1415

1516
print("Roll: {}".format(name))
1617
for k in row.keys():
17-
can_defeat = row[k].strip().lower() == 'win'
18+
can_defeat = row[k].strip().lower() == "win"
1819
print(" * {} will defeat {}? {}".format(name, k, can_defeat))
1920

2021
print()
2122

23+
2224
read_rolls()

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ def defensive_roll(self):
2727

2828

2929
class Wizard(Creature):
30-
3130
def attack(self, creature):
3231
my_roll = self.defensive_roll()
3332
their_roll = creature.defensive_roll()
Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,58 @@
11
from actors import Creature, Wizard, Dragon
22
import random
33

4+
45
def main():
56
print_header()
67
game_loop()
78

89

910
def print_header():
10-
print('---------------------------------')
11-
print(' WIZARD GAME')
12-
print('---------------------------------')
11+
print("---------------------------------")
12+
print(" WIZARD GAME")
13+
print("---------------------------------")
1314
print()
1415

1516

1617
def game_loop():
1718
creatures = [
18-
Creature('Bat', 5),
19-
Creature('Toad', 1),
20-
Creature('Tiger', 12),
21-
Dragon('Black Dragon', 50, scaliness=2, breaths_fire=False),
22-
Wizard('Evil wizard', 1000),
19+
Creature("Bat", 5),
20+
Creature("Toad", 1),
21+
Creature("Tiger", 12),
22+
Dragon("Black Dragon", 50, scaliness=2, breaths_fire=False),
23+
Wizard("Evil wizard", 1000),
2324
]
2425

25-
hero = Wizard('Gandolf', 75)
26+
hero = Wizard("Gandolf", 75)
2627

2728
while True:
2829

2930
active_creature = random.choice(creatures)
3031

31-
print('A {} of level {} has appear from a dark and foggy forest...'
32-
.format(active_creature.name, active_creature.level))
32+
print(
33+
"A {} of level {} has appear from a dark and foggy forest...".format(
34+
active_creature.name, active_creature.level
35+
)
36+
)
3337
print()
3438

35-
cmd = input('Do you [a]ttack, [r]unaway, or [l]ook around? ')
36-
if cmd == 'a':
39+
cmd = input("Do you [a]ttack, [r]unaway, or [l]ook around? ")
40+
if cmd == "a":
3741
if hero.attack(active_creature):
3842
creatures.remove(active_creature)
3943
print("The wizard defeated {}".format(active_creature.name))
4044
else:
41-
print("The wizard has been defeat by the powerful {}".format(active_creature.name))
42-
elif cmd == 'r':
43-
print('The wizard has become unsure of his power and flees!!!')
44-
elif cmd == 'l':
45-
print('The wizard {} takes in the surroundings and sees:'
46-
.format(hero.name))
45+
print(
46+
"The wizard has been defeat by the powerful {}".format(
47+
active_creature.name
48+
)
49+
)
50+
elif cmd == "r":
51+
print("The wizard has become unsure of his power and flees!!!")
52+
elif cmd == "l":
53+
print("The wizard {} takes in the surroundings and sees:".format(hero.name))
4754
for c in creatures:
48-
print(" * {} of level {}".format(
49-
c.name, c.level
50-
))
55+
print(" * {} of level {}".format(c.name, c.level))
5156
else:
5257
print("OK, exiting game... bye!")
5358
break
@@ -59,5 +64,5 @@ def game_loop():
5964
print()
6065

6166

62-
if __name__ == '__main__':
63-
main()
67+
if __name__ == "__main__":
68+
main()

days/13-15-text-games/rps.csv

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
user_move,rock,paper,scissors
2+
Rock,tie,lose,win
3+
Paper,win,tie,lose
4+
Scissors,lose,win,tie
5+

days/13-15-text-games/rps.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
from collections import defaultdict
2+
from enum import IntEnum, auto
3+
import csv
4+
import random
5+
import time
6+
7+
8+
class Action(IntEnum):
9+
Rock = auto()
10+
Gun = auto()
11+
Lightning = auto()
12+
Devil = auto()
13+
Dragon = auto()
14+
Water = auto()
15+
Air = auto()
16+
Paper = auto()
17+
Sponge = auto()
18+
Wolf = auto()
19+
Tree = auto()
20+
Human = auto()
21+
Snake = auto()
22+
Scissors = auto()
23+
Fire = auto()
24+
25+
26+
class Player:
27+
def __init__(self, name, wins=0):
28+
self.name = name
29+
self.wins = wins
30+
31+
32+
def main():
33+
print_header()
34+
name = input("Enter your name: ")
35+
player1 = Player(name)
36+
player2 = Player("Computer")
37+
print(f"Welcome {player1.name}")
38+
BEST_OF_NUM = 3
39+
game_loop(player1, player2, BEST_OF_NUM)
40+
41+
42+
def print_header():
43+
print("=" * 30)
44+
print(" 15-way Rock Paper Scissors")
45+
print("=" * 30)
46+
47+
48+
def game_loop(player1, player2, BEST_OF_NUM=3):
49+
while max([player1.wins, player2.wins]) < BEST_OF_NUM-1:
50+
try:
51+
p1_turn = get_user_selection()
52+
except ValueError as e:
53+
range_str = f"[1, {len(Action)}]"
54+
print(f"Invalid selection. Enter a value in range {range_str}")
55+
continue
56+
p2_turn = get_computers_selection()
57+
58+
determine_winner(p1_turn, p2_turn, player1, player2, victories)
59+
get_score(player1, player2)
60+
61+
print()
62+
print("Thanks for playing!")
63+
64+
65+
victories = defaultdict(list)
66+
with open("data/battle-table.csv", "r") as csvfile:
67+
fieldnames = "Attacker,Rock,Gun,Lightning,Devil,Dragon,Water,Air,Paper,Sponge,Wolf,Tree,Human,Snake,Scissors,Fire".split( ",")
68+
for line in csv.DictReader(csvfile, fieldnames=fieldnames):
69+
action = line["Attacker"]
70+
for fieldname in fieldnames:
71+
if line[fieldname] == "win":
72+
victories[action].append(fieldname)
73+
74+
75+
76+
def determine_winner(p1_turn, p2_turn, player1, player2, victories):
77+
defeats = victories[p1_turn.name]
78+
if p1_turn.name == p2_turn.name:
79+
print()
80+
print(f"Both players selected {p1_turn.name}. It's a tie!")
81+
elif p2_turn.name in defeats:
82+
print()
83+
print(f"{p1_turn.name} beats {p2_turn.name}! You win!")
84+
player1.wins += 1
85+
else:
86+
print()
87+
print(f"{p2_turn.name} beats {p1_turn.name}! You lose.")
88+
player2.wins += 1
89+
90+
91+
def get_user_selection():
92+
choices = [f"{action.name} [{action.value}]" for action in Action]
93+
choices_str = "\n".join(choices)
94+
selection = int(input(f"Enter a choice:\n{choices_str}\n"))
95+
action = Action(selection)
96+
return action
97+
98+
99+
def get_computers_selection():
100+
selection = random.choice([action for action in Action])
101+
action = Action(selection)
102+
return action
103+
104+
105+
def get_score(player1, player2):
106+
print()
107+
print(f"{player1.name} has won {player1.wins} times.")
108+
print(f"{player2.name} has won {player2.wins} times.")
109+
print()
110+
111+
112+
if __name__ == "__main__":
113+
main()

0 commit comments

Comments
 (0)