-
Notifications
You must be signed in to change notification settings - Fork 0
/
rps_back.py
167 lines (127 loc) · 5.49 KB
/
rps_back.py
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import random
import matplotlib.pyplot as plt
import rps
from random import randint
import numpy as np
def instructions(response):
# Add to instruction_string so that it contains information on
# how to play rock-paper-scissors
instruction_string = "Choose rock, paper, or scissors from the buttons. "
instruction_string +="The computer will then choose a move. "
instruction_string += "Rock beats scissors, scissors beats paper, and paper beats rock."
# Use a string method to make response all one case
response = response.lower()
# Use an if statement to check if the response is "yes"
if response == "yes":
rps.print_instructions(instruction_string)
# If the user does want instructions pass instruction_string to
# rps.print_instructions
# Name requirements: must be at least two characters, must be no more than 10 characters, must only be
# one name (i.e. no spaces), must start with an upper case letter.
# Return requirement: name
def check_name(name):
error = "Somethings wrong"
if(name[0] == name[0].upper() and (len(name) > 1) and (len(name) < 10) and (name.find(' ') == -1)):
return name
elif(name[0] == name[0].lower()):
rps.quit_game("Name does not start with an uppercase letter!");
elif(name.find(' ') != -1):
rps.quit_game("Name is more than one name!");
elif(len(name) <= 1):
rps.quit_game("Name is too short!");
elif(len(name) >= 10):
rps.quit_game("Name is too long!");
else:
rps.quit_game("Name is invalid")
if len(name) > 30:
rps.quit_game(error)
# Use if statements, as above, to check if 'name' meets the requirements listed above. If it fails
# any condition call rps.quit_game with an appropriate error message.
# return the variable name once have checked all name requirements
# Num requirements: must be more than two and less than 21.
# Return requirement: num
def check_times_to_play(num):
error = "Number is not correct"
# Check that 'num' meets the requirements above.
if int(num)>2 and int(num)<21:
return num
else:
rps.quit_game(error)
# If 'num' does not meet requirements call rps.quit_game with an error
# message (the variable 'error.')
# Note: use function int, to convert num to integer when using it to compare to another integer, i.e. int(num)
# return the variable num once have checked requirements
def play_game(name):
# This gets the move from a player when they push a button.
player_move = rps.get_player_move()
# Use the random library to choose a random move for the player.
rand = randint(0,2)
if(rand == 0):
rand = "Rock"
elif(rand == 1):
rand = "Paper"
elif(rand == 2):
rand = "Scissors"
# Use if statements to check who won. Set who_won equal to 'Computer', 'Player', or 'Tie'
who_won = ""
if(player_move == "Rock" and rand == "Scissors" or player_move == "Paper" and rand == "Rock" or player_move == "Scissors" and rand == "Paper"):
who_won = "Player"
elif(rand == "Rock" and player_move == "Scissors" or rand == "Paper" and player_move == "Rock" or rand == "Scissors" and player_move == "Paper"):
who_won = "Computer"
elif(player_move == rand):
who_won = "Tie"
else:
who_won = "Error"
# After determines who won, build a results string.
# The next 3 lines partially builds this string. Complete for computer, add for tie, and add line for winner
results = "Player played " + player_move
results += "\n"
results += "Computer played " + rand
if(who_won != "Tie"):
results += "\n " + who_won + " won!"
else:
results += "\n It was a tie!"
# Use rps.display_results to display the results string for the game.
rps.display_results(results)
# return the variable who_won
return who_won
def play_match():
rps.ask_instructions()
# Call rps.ask_instructions() to see if player wants instructions
# Call rps.get_name() to get a string of the players name
# Be sure to save return value, i.e. player_name = rps.get_name()
player_name = rps.get_name()
# Call rps.get_num_play to get the number of games to play
# Be sure to save return value, i.e. num_times = rps.get_num_play()
num_times = rps.get_num_play()
num_times = int(num_times)
# Use these variables to keep track of who won
ties = 0
player_wins = 0
computer_wins = 0
# Use a while or for loop to call play_game the correct amount of times
count = 0
game_winner = ""
while count < num_times:
count = count + 1
game_winner = play_game(player_name)
if(game_winner == "Player"):
player_wins = player_wins + 1
elif(game_winner == "Computer"):
computer_wins = computer_wins + 1
else:
ties = ties + 1
# Now use an if statement to increment total for winner
# i.e. will increment either ties, player_wins, or computer_wins
# Call make_graph using the variables for win counts above.
make_graph(player_name, player_wins, computer_wins, ties)
# Create a graph as we did in the previous lab
def make_graph(name, player_wins, comp_wins, ties):
cat = (name, "Computer", "Tied")
score = (player_wins, comp_wins, ties)
y_pos = np.arange(len(cat))
plt.barh(y_pos, score, align = 'center', alpha = 0.4)
plt.yticks(y_pos, cat)
plt.xlabel('Times won')
plt.title('Winner Totals')
plt.show()