Merge remote-tracking branch 'origin/main'

This commit is contained in:
Artur Savitskiy 2024-04-29 19:33:17 +02:00
commit 34e7474d52
7 changed files with 27 additions and 11 deletions

View File

@ -3,4 +3,13 @@ from enum import Enum
class ShipDirection(Enum): class ShipDirection(Enum):
VERTICAL = "VERTICAL" VERTICAL = "VERTICAL"
HORIZONTAL = "HORIZONTAL" HORIZONTAL = "HORIZONTAL"
UNKNOWN = "UNKNOWN" UNKNOWN = "UNKNOWN"
@staticmethod
def from_string(raw_value):
if raw_value:
value = raw_value.lower().capitalize()
if value in ShipDirection.__members__:
return ShipDirection[value]
return ShipDirection.UNKNOWN

View File

@ -25,6 +25,15 @@ class ShipField:
self.ship_direction = ShipDirection.VERTICAL self.ship_direction = ShipDirection.VERTICAL
def from_json(self, obj):
self.field = obj['field']
self.ships = obj['ships']
self.field_size = obj['field_size']
self.field_mode = obj['field_mode']
self.ship_size = obj['ship_size']
self.ship_direction = obj['ship_direction']
def __getitem__(self, item): def __getitem__(self, item):
if item is None: if item is None:
return None return None
@ -244,8 +253,8 @@ class ShipField:
ship_string += self.field[r * self.field_size + c] + ', ' ship_string += self.field[r * self.field_size + c] + ', '
print(blocked_string[:-2] + ' ' + ship_string[:-2]) print(blocked_string[:-2] + ' ' + ship_string[:-2])
print("********************************************************************") print("********************************************************************")
@staticmethod @staticmethod
def convert_to_json(obj): def convert_to_json(obj):
if isinstance(obj, ShipField): if isinstance(obj, ShipField):

View File

@ -84,14 +84,12 @@ def savebutton_click(event):
def loadbutton_click(event): def loadbutton_click(event):
data_list = [] global my_field
with open('test.json') as lines:
data = json.load(lines)
for i in data['shapes']:
sh1 = create_shape(i)
data_list.append(sh1)
return data_list with open('test.json') as lines:
my_field.from_json(json.load(lines)["my_field"])
colorize(my_field, my_buttons)
window = Tk() window = Tk()

View File

@ -1 +0,0 @@
{"shapes": {"field": ["1", "1", "1", "1", " ", " ", " ", " ", "", "1", "", "", " ", " ", " ", "1", " ", "", "", "1", "", "", "", "", "", "", "", "1", "", "", "1", "", "", "", "", "1", "", "", "", "", "1", "", "", "1", "", "", "", "1", " ", " ", "1", "", "", "1", " ", " ", " ", "", "", "", " ", "", "", "", " ", " ", "", "", "", "", " ", "1", "", " ", " ", " ", " ", " ", "", "", " ", "1", "", " ", " ", " ", " ", " ", " ", " ", " ", "1", "", " ", " ", " ", "r", "", "1", "1"], "ships": [], "field_size": 10, "field_mode": "PUT", "ship_size": 1, "ship_direction": "VERTICAL"}}

1
test.json Normal file
View File

@ -0,0 +1 @@
{"my_field": {"field": ["1", "", "", " ", " ", "1", "1", "1", " ", "1", " ", " ", " ", "", " ", " ", "", "", " ", "1", " ", "1", "", "", "", " ", "", "1", "", "", " ", "1", "", "", "", "", "", "", "", "", " ", "1", "", "", "", "1", "", "", "", "", " ", "1", "", "", "", "1", "", "", "", "", "", "", "", "", "", "", "", "", "1", "", "", "", "", "", "", "", "", "", "", "", "1", "", "1", "", "r", " ", " ", " ", " ", " ", "1", " ", "", " ", " ", " ", " ", "1", "1", "1"], "ships": [], "field_size": 10, "field_mode": "PUT", "ship_size": 1, "ship_direction": "VERTICAL"}}