-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrategy_analyser.py
More file actions
136 lines (109 loc) · 4.63 KB
/
strategy_analyser.py
File metadata and controls
136 lines (109 loc) · 4.63 KB
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
from codemaker import CodeMaker
from mathematician_codebreaker import MathematicianCodeBreaker
from logician_codebreaker import LogicianCodeBreaker
from random_codebreaker import RandomCodeBreaker
from knowledge_manager import KnowledgeManager
from game import NUMBER_OF_CHANCES
from printer import print_simulation_results
NUMBER_OF_SIMULATIONS = 20
class StrategyAnalyser:
"""
Provides an interface for the code-maker and multiple code-breakers
to play the game. An instance each of mathematician code-breaker,
logician code-breaker and random code-breaker is created. Each of
them try to solve the same code genrated by the code-maker.
Simulation is performed 'n' number of times, and the different
strategies taken up by each code-breaker are monitored in terms of
number of codes sucessfully guessed by each.
Game rules are the same as those in the normal Game class.
"""
def __init__(self, number_of_games):
self.number_of_games = number_of_games
self.mathematician_codebreaker_score = 0
self.logician_codebreaker_score = 0
self.random_codebreaker_score = 0
def __play(self, codebreaker):
"""
Provides interface to conduct the game.
Provides opportunities to the code-breaker to
make moves, passes them to code-maker, gets the feedback
and passes it to the code-breaker, to get the next move.
Also, keeps track of the winner for the game, and updates
the scores accordingly.
"""
feedback = self.__handle_first_move(codebreaker)
if self.__codebreaker_won(feedback, codebreaker):
return
for i in range(2, NUMBER_OF_CHANCES+1):
next_move = codebreaker.get_next_move(feedback)
feedback = self.codemaker.analyze_move(next_move)
self.__update_knowledge(next_move, feedback)
if self.__codebreaker_won(feedback, codebreaker):
return
return
def __handle_first_move(self, codebreaker):
"""
Handles the first move for the game.
Gets the move from code-breaker, passes it
onto the code-maker, and gets feedback from it.
"""
first_move = codebreaker.get_first_move()
feedback = self.codemaker.analyze_move(first_move)
self.__update_knowledge(first_move, feedback)
return feedback
def __update_knowledge(self, move, feedback):
"""
Updates the knowledge model of the game, with
the knowledge acquired from this move and its feedback.
"""
self.knowledge_manager.handle_move(move, feedback)
return
def __codebreaker_won(self, feedback, codebreaker):
"""
Checks if the codebreaker has correctly
guessed the secret code, and won the game.
If yes, updates the score accordingly.
"""
for val in feedback:
if val == 0 or val == -1:
return False
self.__update_score(codebreaker)
return True
def __update_score(self, codebreaker):
"""
Checks the type of the code-breaker
and updates the score by 1 accordingly.
"""
if (isinstance(codebreaker, MathematicianCodeBreaker)):
self.mathematician_codebreaker_score = self.mathematician_codebreaker_score + 1
return
if (isinstance(codebreaker, LogicianCodeBreaker)):
self.logician_codebreaker_score = self.logician_codebreaker_score + 1
return
if (isinstance(codebreaker, RandomCodeBreaker)):
self.random_codebreaker_score = self.random_codebreaker_score + 1
def run_simulation(self):
"""
Runs the strategy analyser simulation.
Allows 'n' games to be played, with new instances
of all the players and the knowledge model
for each game.
"""
for i in range(0, self.number_of_games):
self.knowledge_manager = KnowledgeManager()
self.codemaker = CodeMaker()
print("Running Game : ", i+1, " : Code : ", self.codemaker.code)
self.mathematician_codebreaker = MathematicianCodeBreaker()
self.logician_codebreaker = LogicianCodeBreaker(
self.knowledge_manager)
self.random_codebreaker = RandomCodeBreaker()
self.__play(self.mathematician_codebreaker)
self.__play(self.logician_codebreaker)
self.__play(self.random_codebreaker)
return
def main():
analyser = StrategyAnalyser(NUMBER_OF_SIMULATIONS)
analyser.run_simulation()
print_simulation_results(analyser)
if __name__ == "__main__":
main()