-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.py
199 lines (169 loc) · 6.43 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
from Player import *
from SakClass import *
import random
from datetime import date
import json
class Game:
def __init__(self):
self.sak = SakClass()
self.human = Human()
self.algorithm = '1'
self.computer = Computer(self.algorithm)
self.playing = None
self.stats = Game.load_stats()
self.winner = None
def __repr__(self):
return f'Class: {self.__class__}, algorithm = {self.algorithm}, playing = {self.playing.__class__}'
def __str__(self):
"""
Εμφανίζει την κατάσταση του παιχνιδιού αν τελείωνε την δεδομένη στηγμή.
"""
if self.winner == self.computer:
return "Νικητής ο Η/Υ με Σκορ: " + str(self.computer.score)+'\n'\
+"Σκορ Παίκτη: " + str(self.human.score)
elif self.winner == self.human:
return "Νικητής ο Παίκτης με Σκορ: " + str(self.human.score)+'\n'\
+"Σκορ Η/Υ: " + str(self.computer.score)
else:
return "Ισοπαλία"+'\n'\
+"Σκορ Η/Υ: " + str(self.computer.score)+'\n'\
+"Σκορ Παίκτη: " + str(self.human.score)
def setup(self):
"""
Αρχικοποιεί ένα παιχνίδι
"""
self.sak = SakClass()
self.human = Human()
self.computer = Computer(self.algorithm)
self.sak.randomize_sak()
self._initialize_player(self.human)
self._initialize_player(self.computer)
self.playing = random.choice([self.human, self.computer])
self.stats = Game.load_stats()
self.winner = None
def menu(self):
"""
Προσομοιώνει το main menu του παιχνιδιού
Δέχετε τις εισόδους τους χρήστη και ανακατευθεύνει στις επιλογές
"""
print("***** SCRABBLE *****")
print("--------------------")
print("1: Σκορ")
print("2: Ρυθμίσεις")
print("3: Παιχνίδι")
print("4: Έξοδος")
print("--------------------")
menu_choice = input("Επιλογή Μενού: ")
if menu_choice == '1':
self.stats = Game.load_stats()
if len(self.stats) == 0:
print("Σκορ : Κενό")
else:
print("Σκορ :")
for stat in self.stats:
print(stat)
print()
game.menu()
if menu_choice == '2':
game.settings()
game.menu()
if menu_choice == '3':
game.run()
game.menu()
if menu_choice == '4':
print("---- Έξοδος ----")
exit()
print("--------------------")
print("Πληκρολόγησε 1, 2, 3 ή 4")
print("--------------------")
game.menu()
def settings(self):
"""
Προσομοιώνει το menu ρυθμίσεων του παιχνιδιού.
Ορίζει τον αλγόριθμο με τον οποίο παίζει ο Η/Υ
"""
print("***** Ρυθμίσεις *****")
print("---------------------")
print("1: MIN_letters")
print("2: MAX_letters")
print("3: SMART")
print("4: FAIL")
print("---------------------")
settings_choice = input("Επιλογή Ρυθμίσεων: ")
if settings_choice in ['1', '2', '3', '4']:
self.algorithm = settings_choice
print("--------------------")
print("Πληκρολόγησε 1, 2, 3 ή 4")
print("--------------------")
def run(self):
"""
Προσομειώνει ένα παιχνίδι scrabble
:return:
"""
self.setup()
while True:
if not self.playing.play(self.sak):
break
if self.playing == self.human:
self.playing = self.computer
else:
self.playing = self.human
if self.computer.score > self.human.score:
self.winner = self.computer
elif self.computer.score < self.human.score:
self.winner = self.human
self.end()
def end(self):
"""
Εκτελεί τις ενέργειες τερματισμού του παιχνιδιού
"""
print("----------------------------------------------------------")
print("Τέλος παιχνιδιού")
print(self)
# Κατασκευάζει το στατιστικό του παιχνιδιού
# temp = f'{len(self.stats) + 1}. '
#
# temp += f'{date.today()} '
#
# if self.winner == self.computer:
# temp += f'Lose'
# elif self.winner == self.human:
# temp += f'Win'
# else:
# temp += f'Draw'
#
# temp += f' Your score: {self.human.score}'
temp = f'{len(self.stats) + 1}. {date.today()} '
temp += self.__str__()
# Προσθέτει το στατιστικό στα υπόλοιπα στατιστικά
self.stats.append(temp)
Game.store_stats(self.stats)
def _initialize_player(self, player):
player.take_letters(self.sak.get_letters(7))
@staticmethod
def load_stats():
"""
Διαβάζει τα στατιστικά του παίκτη
:return: τα στατιστικά
"""
try:
with open('stats.json', 'r') as f:
return json.load(f)["stats"]
except FileNotFoundError:
temp = []
with open("stats.json", 'w') as f:
json.dump({"stats": temp}, f)
return temp
@staticmethod
def store_stats(stats):
"""
Αποθηκεύει τα στατιστικά του παίκτη στο αρχείο
"""
try:
with open('stats.json', 'w') as f:
json.dump({"stats": stats}, f)
except FileNotFoundError:
print("File Error!!")
if __name__ == '__main__':
game = Game()
game.menu()