загружаю дз по итогам видео 18 встречи

This commit is contained in:
ehermakov 2024-04-27 15:19:30 +03:00
parent 6322addc99
commit 932aa4a8f8
9 changed files with 62 additions and 18 deletions

View File

@ -2,6 +2,14 @@ from enum import Enum
class ShipDirection(Enum):
VERTICAL = 0
HORIZONTAL = 1
UNKNOWN = -1
VERTICAL = 'VERTICAL'
HORIZONTAL = 'HORIZONTAL'
UNKNOWN = 'UNKNOWN'
@staticmethod
def from_string(raw_value):
if raw_value:
value = raw_value.upper()
if value in ShipDirection.__members__:
return ShipDirection[value]
return ShipDirection.UNKNOWN

View File

@ -1,3 +1,5 @@
import copy
from ShootResult import ShootResult
from ShipDirection import ShipDirection
from ShipMode import ShipMode
@ -23,6 +25,14 @@ class ShipField:
self.ship_size = 4
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 = ShipMode.from_string(obj['field_mode'])
self.ship_size = obj['ship_size']
self.ship_direction = ShipDirection.from_string(obj['ship_direction'])
def __getitem__(self, item):
if item is None:
return None
@ -165,14 +175,11 @@ class ShipField:
else:
return ShootResult.UNDEFINED
def check_ship(self, row, col):
#функция должна возвражать тру, если в заданной клетке есть корабль
# функция должна возвражать тру, если в заданной клетке есть корабль
# в противном случае фолс
return self.field[row * self.field_size + col].strip() == "1"
def check_possible(self, row, col):
# Функция должна возвращать True, если можно поставить сюда корабль,
# в противном случае - False
@ -225,7 +232,6 @@ class ShipField:
if value != ShipDirection.UNKNOWN:
self.ship_direction = value
def toggle_ship_direction(self):
if self.ship_direction == ShipDirection.VERTICAL:
self.ship_direction = ShipDirection.HORIZONTAL
@ -253,5 +259,7 @@ class ShipField:
@staticmethod
def convert_to_json(obj):
if isinstance(obj, ShipField):
result = obj.__dict__
result = copy.deepcopy(obj.__dict__)
result['field_mode'] = obj.field_mode.value
result['ship_direction'] = obj.ship_direction.value
return result

View File

@ -2,9 +2,16 @@ from enum import Enum
class ShipMode(Enum):
PUT = 0
SHOOT = 1
PUT = 'PUT'
SHOOT = 'SHOOT'
UNKNOWN = 'UNKNOWN'
@staticmethod
def from_string(raw_value):
if raw_value:
value = raw_value.upper()
if value in ShipMode.__members__:
return ShipMode[value]
return ShipMode.UNKNOWN

View File

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

View File

@ -1,5 +1,8 @@
import json
import os
from tkinter import *
from tkinter import filedialog
from ShipField import ShipField
my_field = ShipField()
@ -80,8 +83,21 @@ def button_enter(buttons, row, col):
def savebutton_click(event):
with open("test.json", 'w') as f:
json.dump({'shapes': my_field}, f, default=ShipField.convert_to_json)
file_path = filedialog.asksaveasfilename(filetypes=[('JSON files', '*.json')])
if file_path:
with open("file_path", 'w') as f:
json.dump({'my_field': my_field}, f, default=ShipField.convert_to_json)
def loadbutton_click(event):
global my_field
file_path = filedialog.askopenfilename(filetypes=[('JSON files', '*.json')])
if os.path.isfile(file_path):
with open("file_path") as lines:
my_field.from_json(json.load(lines)['my_field'])
colorize(my_field, my_buttons)
window = Tk()
@ -108,4 +124,8 @@ savebutton = Button(window, text='Save', width=20, height=2)
savebutton.bind("<Button-1>", savebutton_click)
savebutton.grid(column=0, row=11, columnspan=4)
loadbutton = Button(window, text='Load', width=20, height=2)
loadbutton.bind("<Button-1>", loadbutton_click)
loadbutton.grid(column=5, row=11, columnspan=4)
window.mainloop()

View File

@ -1 +1 @@
{"shapes": {"field": [" ", " ", " ", " ", " ", " ", "1", " ", " ", " ", " ", "1", "1", "1", "1", " ", "1", " ", " ", " ", " ", " ", " ", " ", " ", " ", "1", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "", " ", " ", " ", "1", " ", " ", " ", "", "", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "p", "", " ", " ", " ", " ", " ", " "], "ships": [3, 2, 2, 2, 1, 1, 1], "field_size": 10, "field_mode": null, "ship_size": 1, "ship_direction": null}}
{"my_field": {"field": [" ", " ", " ", " ", " ", " ", "1", " ", "1", " ", " ", "1", "1", "1", "1", " ", "1", " ", "1", " ", " ", " ", " ", " ", " ", " ", "1", " ", "1", " ", " ", "", "", "", "", " ", " ", " ", " ", " ", "", "1", "", " ", "1", " ", "", "1", " ", " ", "", "1", "", " ", " ", " ", " ", " ", " ", " ", "", "", " ", " ", " ", " ", " ", "", " ", " ", " ", "", " ", "1", " ", "1", " ", "1", " ", " ", " ", "1", " ", " ", " ", "1", " ", " ", " ", " ", " ", "1", " ", " ", " ", "", "", "", " ", " "], "ships": [], "field_size": 10, "field_mode": "PUT", "ship_size": 2, "ship_direction": "VERTICAL"}}