загружаю дз по итогам видео 18 встречи
This commit is contained in:
parent
6322addc99
commit
932aa4a8f8
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
@ -130,7 +140,7 @@ class ShipField:
|
|||
|
||||
if ship_direction == ShipDirection.UNKNOWN:
|
||||
|
||||
# проверим горизонталь
|
||||
# проверим горизонталь
|
||||
for c in range(col + 1, self.field_size):
|
||||
if self.check_ship(row, c):
|
||||
ship_size += 1
|
||||
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -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"}}
|
|
@ -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()
|
||||
|
|
|
@ -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"}}
|
Loading…
Reference in New Issue