Skip to content

Commit e9418fc

Browse files
committed
created new rps game
1 parent 0e8c733 commit e9418fc

File tree

2 files changed

+122
-3
lines changed

2 files changed

+122
-3
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def print_header():
4646

4747

4848
def game_loop(player1, player2, BEST_OF_NUM=3):
49-
while max([player1.wins, player2.wins]) < BEST_OF_NUM-1:
49+
while max([player1.wins, player2.wins]) < BEST_OF_NUM - 1:
5050
try:
5151
p1_turn = get_user_selection()
5252
except ValueError as e:
@@ -64,15 +64,16 @@ def game_loop(player1, player2, BEST_OF_NUM=3):
6464

6565
victories = defaultdict(list)
6666
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( ",")
67+
fieldnames = "Attacker,Rock,Gun,Lightning,Devil,Dragon,Water,Air,Paper,Sponge,Wolf,Tree,Human,Snake,Scissors,Fire".split(
68+
","
69+
)
6870
for line in csv.DictReader(csvfile, fieldnames=fieldnames):
6971
action = line["Attacker"]
7072
for fieldname in fieldnames:
7173
if line[fieldname] == "win":
7274
victories[action].append(fieldname)
7375

7476

75-
7677
def determine_winner(p1_turn, p2_turn, player1, player2, victories):
7778
defeats = victories[p1_turn.name]
7879
if p1_turn.name == p2_turn.name:

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

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
from collections import defaultdict
2+
import csv
3+
import random
4+
import os
5+
import time
6+
7+
8+
class Action:
9+
def __init__(self, action, value):
10+
self.action = action
11+
self.value = value
12+
13+
14+
class Player:
15+
def __init__(self, name, wins=0):
16+
self.name = name
17+
self.wins = wins
18+
19+
20+
# parses the csv for possible actions and possible victories
21+
victories = defaultdict(list)
22+
with open("data/battle-table.csv", "r") as csvfile:
23+
headers = csv.DictReader(csvfile).fieldnames
24+
actions = [
25+
Action(action, value) for value, action in enumerate(headers) if value > 0
26+
]
27+
28+
for line in csv.DictReader(csvfile, fieldnames=headers):
29+
action = line["Attacker"]
30+
for fieldname in headers:
31+
if line[fieldname] == "win":
32+
victories[action].append(fieldname)
33+
34+
35+
def main():
36+
print_header()
37+
name = input("Enter your name: ")
38+
player1 = Player(name)
39+
player2 = Player("Computer")
40+
BEST_OF_NUM = 3
41+
print(f"Welcome {player1.name}. Let's play best of {BEST_OF_NUM} rounds.")
42+
game_loop(player1, player2, BEST_OF_NUM)
43+
get_final_score(player1, player2, BEST_OF_NUM)
44+
45+
46+
def print_header():
47+
print("=" * 30)
48+
print(" 15-way Rock Paper Scissors")
49+
print("=" * 30)
50+
51+
52+
def game_loop(player1, player2, BEST_OF_NUM):
53+
while max([player1.wins, player2.wins]) < BEST_OF_NUM - 1:
54+
try:
55+
p1_turn = get_user_selection()
56+
except ValueError:
57+
range_str = f"[1, {len(actions)}]"
58+
print(f"Invalid selection. Enter a value in range {range_str}")
59+
continue
60+
61+
print(f"You choose {p1_turn.action}")
62+
print("The computer is thinking.")
63+
time.sleep(0.5)
64+
print("1...")
65+
time.sleep(0.25)
66+
print("2...")
67+
time.sleep(0.25)
68+
print("3...")
69+
time.sleep(0.25)
70+
p2_turn = get_computers_selection()
71+
print(f"The Computer choose {p2_turn.action}")
72+
determine_winner(p1_turn, p2_turn, player1, player2, victories)
73+
get_score(player1, player2)
74+
input("Press ENTER to move to the next round.")
75+
os.system("cls" if os.name == "nt" else "clear")
76+
print_header()
77+
78+
79+
def get_user_selection():
80+
choices = [f"[{action.value}] {action.action}" for action in actions]
81+
choices_str = "\n".join(choices)
82+
selection = int(input(f"\nChoose your move:\n\n{choices_str}\n"))
83+
action = actions[selection - 1]
84+
return action
85+
86+
87+
def get_computers_selection():
88+
return random.choice([action for action in actions])
89+
90+
91+
def determine_winner(p1_turn, p2_turn, player1, player2, victories):
92+
defeats = victories[p1_turn.action]
93+
if p1_turn.action == p2_turn.action:
94+
print(f"\nBoth players selected {p1_turn.action}. It's a tie!")
95+
elif p2_turn.action in defeats:
96+
print(f"\n{p1_turn.action} beats {p2_turn.action}! You win!")
97+
player1.wins += 1
98+
else:
99+
print(f"\n{p2_turn.action} beats {p1_turn.action}! You lose.")
100+
player2.wins += 1
101+
102+
103+
def get_score(player1, player2):
104+
print(f"\n{player1.name} has won {player1.wins} times.")
105+
print(f"{player2.name} has won {player2.wins} times.", end="\n\n")
106+
107+
108+
def get_final_score(player1, player2, BEST_OF_NUM):
109+
print(f"\n{player1.name} won {player1.wins} times.")
110+
print(f"{player2.name} won {player2.wins} times.", end="\n\n")
111+
if player1.wins == BEST_OF_NUM - 1:
112+
print("You won the match! Thanks for playing!")
113+
if player2.wins == BEST_OF_NUM - 1:
114+
print("The Computer won the math! Better luck next time.")
115+
116+
117+
if __name__ == "__main__":
118+
main()

0 commit comments

Comments
 (0)