forked from iamadamhair/ispy_python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
72 lines (56 loc) · 1.82 KB
/
game.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
import time
import logging as log
import objects
import models
import database as db
class Game:
"""
Main class that handles game logic
"""
def play(self):
"""
Play the game
"""
log.info('Playing game %d', self.id)
start = time.time()
# Generate all image probabilities at once since this takes a little while
Pi = models.gen_image_probabilities(self)
# Initialize game stats to empty
NoOfQuestions = 0
round_wins = 0
round_losses = 0
avg_win = 0
avg_lose = 0
question_answers = {}
questions_asked = {}
# For each object in the set
for obj in objects.get_all():
# Record individual object stats
result, number_of_questions, answers, askedQuestions = obj.play(self, Pi)
log.info("Game %d, object %d complete, updating stats", self.id, obj.id)
if result == 0: # Loss
round_losses = round_losses + 1
avg_lose = avg_lose + number_of_questions
else: # Win
round_wins = round_wins + 1
avg_win = avg_win + number_of_questions
NoOfQuestions = number_of_questions + NoOfQuestions
# Save questions and answers for later
questions_asked[obj.id] = askedQuestions
question_answers[obj.id] = answers
# Save results
self._record(round_wins, round_losses, NoOfQuestions)
end = time.time()
log.info("Game %d complete (Took %ds)", self.id, int(end - start))
return round_wins, round_losses, NoOfQuestions, avg_win, avg_lose, question_answers, questions_asked
def _record(self, wins, losses, num_questions):
"""
Record game data
"""
with open("game.txt", "a") as myfile:
myfile.write("Round " + str(self.id) + ": ")
myfile.write("Wins=" + str(wins) + ', Losses='+str(losses))
myfile.write(" Accuracy: " + str(wins/float(17)) + "\n")
myfile.write("Average number of questions: " + str(num_questions/float(17)) + "\n")
def __init__(self, id):
self.id = id