@@ -17,6 +17,10 @@ def __init__(self, name, wins=0):
17
17
self .wins = wins
18
18
19
19
20
+ class EvenNumberException (Exception ):
21
+ pass
22
+
23
+
20
24
# parses the csv for possible actions and victories
21
25
victories = defaultdict (list )
22
26
with open ("data/battle-table.csv" , "r" ) as csvfile :
@@ -37,8 +41,7 @@ def main():
37
41
name = input ("Enter your name: " )
38
42
player1 = Player (name )
39
43
player2 = Player ("Computer" )
40
- BEST_OF_NUM = 3
41
- print (f"Welcome { player1 .name } . Let's play best of { BEST_OF_NUM } rounds." )
44
+ BEST_OF_NUM = best_of (player1 )
42
45
game_loop (player1 , player2 , BEST_OF_NUM )
43
46
get_final_score (player1 , player2 , BEST_OF_NUM )
44
47
@@ -48,6 +51,22 @@ def print_header():
48
51
print (" 15-way Rock Paper Scissors" )
49
52
print ("=" * 30 )
50
53
54
+ def best_of (player1 ):
55
+ print (f"Welcome { player1 .name } !\n " )
56
+ while True :
57
+ try :
58
+ best_of_num = int (input ("Enter an odd number of rounds to play:" ))
59
+ if best_of_num % 2 == 0 :
60
+ raise EvenNumberException
61
+ print (f"OK { player1 .name } , let's play best of { best_of_num } rounds. Good Luck!\n " )
62
+ break
63
+ except ValueError :
64
+ print ("You did not enter an number.\n " )
65
+ except EvenNumberException :
66
+ print ("You entered an even number which means the match could end in a tie.\n " )
67
+
68
+ return best_of_num
69
+
51
70
52
71
def game_loop (player1 , player2 , BEST_OF_NUM ):
53
72
while max ([player1 .wins , player2 .wins ]) < BEST_OF_NUM - 1 :
@@ -58,7 +77,7 @@ def game_loop(player1, player2, BEST_OF_NUM):
58
77
print (f"Invalid selection. Enter a value in range { range_str } " )
59
78
continue
60
79
61
- print (f"You choose { p1_turn .action } " )
80
+ print (f"You chose { p1_turn .action } " )
62
81
print ("The computer is thinking." )
63
82
time .sleep (0.5 )
64
83
print ("1..." )
@@ -68,7 +87,7 @@ def game_loop(player1, player2, BEST_OF_NUM):
68
87
print ("3..." )
69
88
time .sleep (0.25 )
70
89
p2_turn = get_computers_selection ()
71
- print (f"The Computer choose { p2_turn .action } " )
90
+ print (f"The Computer chose { p2_turn .action } " )
72
91
determine_winner (p1_turn , p2_turn , player1 , player2 , victories )
73
92
get_score (player1 , player2 )
74
93
input ("Press ENTER to move to the next round." )
@@ -109,7 +128,7 @@ def get_final_score(player1, player2, BEST_OF_NUM):
109
128
print (f"\n { player1 .name } won { player1 .wins } times." )
110
129
print (f"{ player2 .name } won { player2 .wins } times." , end = "\n \n " )
111
130
if player1 .wins == BEST_OF_NUM - 1 :
112
- print ("You won the match! Thanks for playing!" )
131
+ print (f "You won the match! Thanks for playing!" )
113
132
if player2 .wins == BEST_OF_NUM - 1 :
114
133
print ("The Computer won the math! Better luck next time." )
115
134
0 commit comments