Homework
This commit is contained in:
parent
dc1ba63fef
commit
dbc29e73f5
|
@ -9,7 +9,7 @@ class ShipDirection(Enum):
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_string(raw_value):
|
def from_string(raw_value):
|
||||||
if raw_value:
|
if raw_value:
|
||||||
value = raw_value.lower().capitalize()
|
value = raw_value.upper()
|
||||||
if value in ShipDirection.__members__:
|
if value in ShipDirection.__members__:
|
||||||
return ShipDirection[value]
|
return ShipDirection[value]
|
||||||
return ShipDirection.UNKNOWN
|
return ShipDirection.UNKNOWN
|
|
@ -29,9 +29,9 @@ class ShipField:
|
||||||
self.field = obj['field']
|
self.field = obj['field']
|
||||||
self.ships = obj['ships']
|
self.ships = obj['ships']
|
||||||
self.field_size = obj['field_size']
|
self.field_size = obj['field_size']
|
||||||
self.field_mode = obj['field_mode']
|
self.field_mode = ShipMode.from_string(obj['field_mode'])
|
||||||
self.ship_size = obj['ship_size']
|
self.ship_size = obj['ship_size']
|
||||||
self.ship_direction = obj['ship_direction']
|
self.ship_direction = ShipDirection.from_string(obj['ship_direction'])
|
||||||
|
|
||||||
|
|
||||||
def __getitem__(self, item):
|
def __getitem__(self, item):
|
||||||
|
@ -82,7 +82,8 @@ class ShipField:
|
||||||
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
self.field[row * self.field_size + col] = "+"
|
self.field[row * self.field_size + col] += "+"
|
||||||
|
|
||||||
|
|
||||||
def clear_marker(self):
|
def clear_marker(self):
|
||||||
for i in range(0, len(self.field)):
|
for i in range(0, len(self.field)):
|
||||||
|
@ -91,7 +92,8 @@ class ShipField:
|
||||||
|
|
||||||
if "+" in self.field[i]:
|
if "+" in self.field[i]:
|
||||||
self.field[i] = self.field[i].replace("+", "")
|
self.field[i] = self.field[i].replace("+", "")
|
||||||
|
|
||||||
|
|
||||||
def set_ship(self, row, col):
|
def set_ship(self, row, col):
|
||||||
if row < 0 or row > self.field_size:
|
if row < 0 or row > self.field_size:
|
||||||
return
|
return
|
||||||
|
@ -114,6 +116,7 @@ class ShipField:
|
||||||
if self.ship_size in self.ships:
|
if self.ship_size in self.ships:
|
||||||
self.ships.remove(self.ship_size)
|
self.ships.remove(self.ship_size)
|
||||||
|
|
||||||
|
|
||||||
def get_ship(self, row, col):
|
def get_ship(self, row, col):
|
||||||
if row < 0 or row > self.field_size:
|
if row < 0 or row > self.field_size:
|
||||||
return
|
return
|
||||||
|
|
|
@ -2,4 +2,14 @@ from enum import Enum
|
||||||
|
|
||||||
class ShipMode(Enum):
|
class ShipMode(Enum):
|
||||||
PUT = "PUT"
|
PUT = "PUT"
|
||||||
SHOOT = "SHOOT"
|
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 @@
|
||||||
|
{"enemy_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": 1, "ship_direction": "VERTICAL"}}
|
|
@ -1,4 +1,7 @@
|
||||||
import json
|
import json
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
from tkinter import filedialog
|
||||||
from tkinter import *
|
from tkinter import *
|
||||||
from ShipField import ShipField
|
from ShipField import ShipField
|
||||||
|
|
||||||
|
@ -33,10 +36,10 @@ def colorize(field, buttons):
|
||||||
bg = 'black'
|
bg = 'black'
|
||||||
if field.field[i] == "p":
|
if field.field[i] == "p":
|
||||||
bg = 'blue'
|
bg = 'blue'
|
||||||
|
if field.field[i] == "r":
|
||||||
|
bg = 'red'
|
||||||
if "+" in field.field[i]:
|
if "+" in field.field[i]:
|
||||||
bg = 'orange'
|
bg = 'orange'
|
||||||
if "r" in field[i]:
|
|
||||||
bg = 'red'
|
|
||||||
buttons[i].configure(bg=bg)
|
buttons[i].configure(bg=bg)
|
||||||
|
|
||||||
|
|
||||||
|
@ -78,20 +81,46 @@ def button_enter(buttons, row, col):
|
||||||
colorize(enemy_field, enemy_buttons)
|
colorize(enemy_field, enemy_buttons)
|
||||||
|
|
||||||
|
|
||||||
def savebutton_click(event):
|
def savebutton_click():
|
||||||
with open('test.json', 'w') as f:
|
file_path = filedialog.asksaveasfilename(filetypes=[("JSON files", "*.json")])
|
||||||
json.dump({'my_field': my_field}, f, default=ShipField.convert_to_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):
|
def loadbutton_click():
|
||||||
global my_field
|
global my_field
|
||||||
|
|
||||||
with open('test.json') as lines:
|
file_path = filedialog.askopenfilename(filetypes=[("JSON files", "*.json")])
|
||||||
my_field.from_json(json.load(lines)["my_field"])
|
|
||||||
|
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)
|
colorize(my_field, my_buttons)
|
||||||
|
|
||||||
|
|
||||||
|
def savebutton_click_enemy():
|
||||||
|
file_path = filedialog.asksaveasfilename(filetypes=[("JSON files", "*.json")])
|
||||||
|
|
||||||
|
if file_path:
|
||||||
|
with open(file_path, 'w') as f:
|
||||||
|
json.dump({'enemy_field': enemy_field}, f, default=ShipField.convert_to_json)
|
||||||
|
|
||||||
|
|
||||||
|
def loadbutton_click_enemy():
|
||||||
|
global enemy_field
|
||||||
|
|
||||||
|
file_path = filedialog.askopenfilename(filetypes=[("JSON files", "*.json")])
|
||||||
|
|
||||||
|
if os.path.isfile(file_path):
|
||||||
|
with open(file_path) as lines:
|
||||||
|
enemy_field.from_json(json.load(lines)["enemy_field"])
|
||||||
|
|
||||||
|
colorize(enemy_field, enemy_buttons)
|
||||||
|
|
||||||
|
|
||||||
window = Tk()
|
window = Tk()
|
||||||
window.title("Ship Craft!")
|
window.title("Ship Craft!")
|
||||||
window.geometry('940x510')
|
window.geometry('940x510')
|
||||||
|
@ -103,13 +132,17 @@ enemy_buttons = draw_field(window, enemy_field, 11)
|
||||||
lbl = Label(window, text='', width=5, height=2)
|
lbl = Label(window, text='', width=5, height=2)
|
||||||
lbl.grid(column=10, row=0)
|
lbl.grid(column=10, row=0)
|
||||||
|
|
||||||
savebutton = Button(window, text='Save', width=20, height=2)
|
savebutton = Button(window, text='Save', width=20, height=2, command=savebutton_click)
|
||||||
savebutton.bind('<Button-1>', savebutton_click)
|
|
||||||
savebutton.grid(column=0, row=11, columnspan=4)
|
savebutton.grid(column=0, row=11, columnspan=4)
|
||||||
|
|
||||||
loadbutton = Button(window, text='Load', width=20, height=2)
|
loadbutton = Button(window, text='Load', width=20, height=2, command=loadbutton_click)
|
||||||
loadbutton.bind('<Button-1>', loadbutton_click)
|
|
||||||
loadbutton.grid(column=5, row=11, columnspan=4)
|
loadbutton.grid(column=5, row=11, columnspan=4)
|
||||||
|
|
||||||
|
savebutton_enemy = Button(window, text='Save_enemy', width=20, height=2, command=savebutton_click_enemy)
|
||||||
|
savebutton_enemy.grid(column=11, row=11, columnspan=4)
|
||||||
|
|
||||||
|
loadbutton_enemy = Button(window, text='Load_enemy', width=20, height=2, command=loadbutton_click_enemy)
|
||||||
|
loadbutton_enemy.grid(column=16, row=11, columnspan=4)
|
||||||
|
|
||||||
|
|
||||||
window.mainloop()
|
window.mainloop()
|
|
@ -0,0 +1 @@
|
||||||
|
{"my_field": {"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 +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"}}
|
{"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": 4, "ship_direction": "VERTICAL"}}
|
Loading…
Reference in New Issue