Skip to content

Commit 422e5a2

Browse files
committed
grosso do histórico pra json
1 parent 13b2b0d commit 422e5a2

File tree

4 files changed

+107
-22
lines changed

4 files changed

+107
-22
lines changed

.idea/workspace.xml

Lines changed: 24 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

minesweeper/controller.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import model
2+
3+
14
class Controller:
25
"""Controller class for the connection between field Model and View."""
36

@@ -120,7 +123,8 @@ def clear_timer(self):
120123
pass
121124

122125
def save_as_json(self):
123-
pass
126+
self.model.save.strategy = model.To_json()
127+
self.model.save_state()
124128

125129
def save_as_csv(self):
126130
pass

minesweeper/model.py

Lines changed: 77 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from random import randint
22
from cell import Cell
33
from abc import ABC, abstractmethod
4+
import json
45

56

67
class Model:
@@ -25,7 +26,7 @@ def __init__(self):
2526
self.playersMid = []
2627
self.playersHard = []
2728
self.playersRandom = []
28-
self.save = Strategy
29+
self.save = SaveGame()
2930

3031
"""Getter and setter"""
3132

@@ -245,7 +246,7 @@ def next_mark(self, x, y):
245246
self.flagged_cells -= 1
246247

247248
def store_played_games(self):
248-
if self.MINES_MAX == 3:
249+
if self.MINES_MAX == 10:
249250
nome = self.controller.get_text_input("Noobie mode", "Insert your name")
250251
time = self.seconds_from_start
251252
self.playersEasy.append(Player(nome, time))
@@ -262,6 +263,9 @@ def store_played_games(self):
262263
time = self.seconds_from_start
263264
self.playersRandom.append(Player(nome, time))
264265

266+
def save_state(self):
267+
self.save.start_save(self)
268+
265269

266270
class Player:
267271
def __init__(self, nome, time):
@@ -275,22 +279,84 @@ def get_time(self):
275279
return self.time
276280

277281

278-
class Strategy(ABC):
282+
"""Fake interface in python """
283+
284+
285+
class TypeFile(ABC):
279286
@abstractmethod
280-
def create_file(self):
287+
def create_file(self, model):
281288
pass
282289

283290

284-
class to_json(Strategy):
285-
def create_file(self):
291+
"""Concrete strategies implements"""
292+
293+
294+
class To_json(TypeFile):
295+
def create_file(self, model):
296+
print("OK")
297+
if len(model.playersEasy) != 0:
298+
easy_dict = {
299+
"GameMode": "Easy",
300+
"NamePlayer": "PlayTime"
301+
}
302+
for player in model.playersEasy:
303+
easy_dict[player.get_nome()] = player.get_time()
304+
json_obj = json.dumps(easy_dict, indent=4)
305+
with open("historico.json", "a") as outfile:
306+
outfile.write(json_obj)
307+
if len(model.playersMid) != 0:
308+
mid_dict = {
309+
"GameMode": "Mid",
310+
"NamePlayer": "PlayTime"
311+
}
312+
for player in model.playersMid:
313+
mid_dict[player.get_nome()] = player.get_time()
314+
json_obj = json.dumps(mid_dict, indent=4)
315+
with open("historico.json", "a") as outfile:
316+
outfile.write(json_obj)
317+
if len(model.playersHard) != 0:
318+
hard_dict = {
319+
"GameMode": "Hard",
320+
"NamePlayer": "PlayTime"
321+
}
322+
for player in model.playersHard:
323+
hard_dict[player.get_nome()] = player.get_time()
324+
json_obj = json.dumps(hard_dict, indent=4)
325+
with open("historico.json", "a") as outfile:
326+
outfile.write(json_obj)
327+
if len(model.playersRandom) != 0:
328+
rand_dict = {
329+
"GameMode": "Random",
330+
"NamePlayer": "PlayTime"
331+
}
332+
for player in model.playersRandom:
333+
rand_dict[player.get_nome()] = player.get_time()
334+
json_obj = json.dumps(rand_dict, indent=4)
335+
with open("historico.json", "a") as outfile:
336+
outfile.write(json_obj)
337+
338+
339+
class To_csv(TypeFile):
340+
def create_file(self, model):
286341
pass
287342

288343

289-
class to_csv(Strategy):
290-
def create_file(self):
344+
class To_xml(TypeFile):
345+
def create_file(self, model):
291346
pass
292347

293348

294-
class to_xml(Strategy):
295-
def create_file(self):
296-
pass
349+
"""Context to choose option"""
350+
351+
352+
class SaveGame:
353+
strategy: TypeFile
354+
355+
def __init__(self, strategy: TypeFile = None):
356+
if strategy is not None:
357+
self.strategy = strategy
358+
else:
359+
self.strategy = To_json()
360+
361+
def start_save(self, model):
362+
self.strategy.create_file(model)

minesweeper/view.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def input_box_text(self, title, info):
8686
exit()
8787

8888
def input_box_int(self, title, info):
89-
metric_num, status = QInputDialog.getInt(self, title, info, min=6)
89+
metric_num, status = QInputDialog.getInt(self, title, info, min=1)
9090
if status:
9191
return metric_num
9292
else:

0 commit comments

Comments
 (0)