This commit is contained in:
danii 2024-05-06 18:47:26 +02:00
parent dc1ba63fef
commit dbc29e73f5
10 changed files with 67 additions and 19 deletions

View File

@ -9,7 +9,7 @@ class ShipDirection(Enum):
@staticmethod
def from_string(raw_value):
if raw_value:
value = raw_value.lower().capitalize()
value = raw_value.upper()
if value in ShipDirection.__members__:
return ShipDirection[value]
return ShipDirection.UNKNOWN

View File

@ -29,9 +29,9 @@ class ShipField:
self.field = obj['field']
self.ships = obj['ships']
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_direction = obj['ship_direction']
self.ship_direction = ShipDirection.from_string(obj['ship_direction'])
def __getitem__(self, item):
@ -82,7 +82,8 @@ class ShipField:
else:
self.field[row * self.field_size + col] = "+"
self.field[row * self.field_size + col] += "+"
def clear_marker(self):
for i in range(0, len(self.field)):
@ -91,7 +92,8 @@ class ShipField:
if "+" in self.field[i]:
self.field[i] = self.field[i].replace("+", "")
def set_ship(self, row, col):
if row < 0 or row > self.field_size:
return
@ -114,6 +116,7 @@ class ShipField:
if self.ship_size in self.ships:
self.ships.remove(self.ship_size)
def get_ship(self, row, col):
if row < 0 or row > self.field_size:
return

View File

@ -2,4 +2,14 @@ from enum import Enum
class ShipMode(Enum):
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

View File

@ -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"}}

View File

@ -1,4 +1,7 @@
import json
import os
import time
from tkinter import filedialog
from tkinter import *
from ShipField import ShipField
@ -33,10 +36,10 @@ def colorize(field, buttons):
bg = 'black'
if field.field[i] == "p":
bg = 'blue'
if field.field[i] == "r":
bg = 'red'
if "+" in field.field[i]:
bg = 'orange'
if "r" in field[i]:
bg = 'red'
buttons[i].configure(bg=bg)
@ -78,20 +81,46 @@ def button_enter(buttons, row, col):
colorize(enemy_field, enemy_buttons)
def savebutton_click(event):
with open('test.json', 'w') as f:
json.dump({'my_field': my_field}, f, default=ShipField.convert_to_json)
def savebutton_click():
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):
def loadbutton_click():
global my_field
with open('test.json') as lines:
my_field.from_json(json.load(lines)["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)
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.title("Ship Craft!")
window.geometry('940x510')
@ -103,13 +132,17 @@ enemy_buttons = draw_field(window, enemy_field, 11)
lbl = Label(window, text='', width=5, height=2)
lbl.grid(column=10, row=0)
savebutton = Button(window, text='Save', width=20, height=2)
savebutton.bind('<Button-1>', savebutton_click)
savebutton = Button(window, text='Save', width=20, height=2, command=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 = Button(window, text='Load', width=20, height=2, command=loadbutton_click)
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()

View File

@ -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"}}

View File

@ -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"}}