Skip to content

Commit 55d300f

Browse files
committed
add problem 2.rockpaperscissors.py
1 parent 2fdd7ba commit 55d300f

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

2.rockpaperscissors.p2.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Key the inputs in the terminal
2+
# type stop to output the final score
3+
opp_moves = {
4+
'A': 'Rock', 'B': 'Paper', 'C': 'Scissors'
5+
}
6+
7+
own_moves = {
8+
'Rock': 1, 'Paper': 2, 'Scissors': 3
9+
}
10+
11+
tactic = {
12+
'X': ('Lose', 0),
13+
'Y': ('Draw', 3),
14+
'Z': ('Win', 6)
15+
}
16+
17+
def checkWin(opp_move, own_move): # might wanna change the if-else conditions to hash mapping to reduce complexity
18+
if tactic[own_move][0] == 'Draw':
19+
return own_moves[opp_moves[opp_move]]
20+
elif tactic[own_move][0] == 'Win':
21+
if opp_moves[opp_move] == 'Rock':
22+
return own_moves['Paper']
23+
elif opp_moves[opp_move] == 'Paper':
24+
return own_moves['Scissors']
25+
else:
26+
return own_moves['Rock']
27+
else:
28+
if opp_moves[opp_move] == 'Paper':
29+
return own_moves['Rock']
30+
elif opp_moves[opp_move] == 'Scissors':
31+
return own_moves['Paper']
32+
else:
33+
return own_moves['Scissors']
34+
35+
output = 0
36+
37+
while True:
38+
try:
39+
line = input().split()
40+
41+
if line[0] != 'stop':
42+
# do something
43+
output += checkWin(line[0], line[1])
44+
output += tactic[line[1]][1]
45+
46+
else:
47+
print(output)
48+
break
49+
50+
except EOFError:
51+
break

2.rockpaperscissors.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# opponents moves => A: 'Rock', B: 'Paper', C: 'Scissors'
2+
# own move => X: ('Rock', 1), Y: ('Paper', 2), Z: ('Scissors', 3)
3+
4+
opp_moves = {
5+
'A': 'Rock', 'B': 'Paper', 'C': 'Scissors'
6+
}
7+
8+
own_moves = {
9+
'X': ('Rock', 1), 'Y': ('Paper', 2), 'Z': ('Scissors', 3)
10+
}
11+
12+
def checkWin(opp_move, own_move):
13+
if opp_moves[opp_move] == own_moves[own_move][0]:
14+
return 3
15+
elif opp_moves[opp_move] == 'Rock' and own_moves[own_move][0] == 'Scissors':
16+
return 0
17+
elif opp_moves[opp_move] == 'Paper' and own_moves[own_move][0] == 'Rock':
18+
return 0
19+
elif opp_moves[opp_move] == 'Scissors' and own_moves[own_move][0] == 'Paper':
20+
return 0
21+
else:
22+
return 6
23+
24+
output = 0
25+
26+
while True:
27+
try:
28+
line = input().split()
29+
30+
if line[0] != 'stop':
31+
# do something
32+
output += checkWin(line[0], line[1])
33+
output += own_moves[line[1]][1]
34+
35+
else:
36+
print(output)
37+
break
38+
39+
except EOFError:
40+
break

0 commit comments

Comments
 (0)