-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
79 lines (70 loc) · 2.74 KB
/
model.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
import RandomOpponent
from AbstractOpponent import AbstractOpponent
class RockPaperScissors:
_ROCK = 0 # TODO hide better
_PAPER = 1
_SCISSORS = 2
def __init__(self, opponent: AbstractOpponent):
"""
Sets all the values to default
:param opponent: Which Opponent to use
"""
self._player_score = 0
self._computer_score = 0
self._playerLast = 0
self._computerLast = 0
self._round = 0
self._opponent = opponent
def play(self, players_choice: int, dchoice=-1) -> tuple:
"""
Play a new game
:param players_choice: a number betweeen
:param dchoice: DO NOT TOUCH only for test purpuses
:return: tuple with current round, player score, computer score, computer last and player's last pick
"""
self._playerLast = players_choice
ochoice = self._opponent.choice() if dchoice == -1 else dchoice
self._computerLast = ochoice
won = None
if players_choice - 1 == ochoice or (players_choice == self._ROCK and ochoice == self._SCISSORS):
self._player_score += 1
won = "player"
elif players_choice != ochoice:
self._computer_score += 1
won = "computer"
self._round += 1
self._opponent.addToHistory({"player": self.toString(players_choice), "computer": self.toString(ochoice), "won": won if won is not None else "tie"})
return self.stats()
def reset(self) -> None:
"""
Resets all the values
:return: nothing
"""
self._player_score = 0
self._computer_score = 0
self._round = 0
def stats(self) -> tuple:
"""
Returns the current stats as a tuple
:return: tuple with current round, player score, computer score, computer last and player's last pick
"""
#TODO not so clean i guess
return (self._round, self._player_score, self._computer_score, self._computerLast, self._playerLast)
def toString(self, a: int) -> str:
"""
To map a number to the corresponding string
:param a: between -1 and 3
:return: the according String to the number
"""
return {0: "Rock", 1: "Paper", 2: "Scissors"}[a]
if __name__ == "__main__":
# Test is kinda shitty
rps = RockPaperScissors(RandomOpponent.RandomOpponent())
for players_choice in range(3):
for ochoice in range(3):
print(f"Player: {rps.toString(players_choice)}, Opponent: {rps.toString(ochoice)}")
rps.play(players_choice, ochoice)
if rps._player_score == 1: print("Player won")
elif rps._computer_score == 1: print("Opponent won")
else: print("Tie")
rps.reset()