Compare commits
2 Commits
d51d6b9fd1
...
60ae127176
Author | SHA1 | Date |
---|---|---|
danii | 60ae127176 | |
danii | 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
|
|
@ -28,10 +28,16 @@ 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']
|
||||||
|
<<<<<<< HEAD
|
||||||
|
self.ship_direction = ShipDirection.from_string(obj['ship_direction'])
|
||||||
|
|
||||||
|
|
||||||
|
=======
|
||||||
self.ship_direction = obj['ship_direction']
|
self.ship_direction = obj['ship_direction']
|
||||||
|
|
||||||
|
>>>>>>> d51d6b9fd1009b5d2eec1276597e4b6e34e005b5
|
||||||
def __getitem__(self, item):
|
def __getitem__(self, item):
|
||||||
if item is None:
|
if item is None:
|
||||||
return None
|
return None
|
||||||
|
@ -77,6 +83,10 @@ class ShipField:
|
||||||
self.field[row * self.field_size + c] = "r"
|
self.field[row * self.field_size + c] = "r"
|
||||||
else:
|
else:
|
||||||
self.field[row * self.field_size + col] += "+"
|
self.field[row * self.field_size + col] += "+"
|
||||||
|
<<<<<<< HEAD
|
||||||
|
|
||||||
|
=======
|
||||||
|
>>>>>>> d51d6b9fd1009b5d2eec1276597e4b6e34e005b5
|
||||||
|
|
||||||
def clear_marker(self):
|
def clear_marker(self):
|
||||||
for i in range(0, len(self.field)):
|
for i in range(0, len(self.field)):
|
||||||
|
@ -86,6 +96,7 @@ 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
|
||||||
|
@ -108,6 +119,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
|
||||||
|
|
|
@ -3,3 +3,13 @@ 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
|
||||||
|
|
||||||
|
@ -37,7 +40,10 @@ def colorize(field, buttons):
|
||||||
bg = 'red'
|
bg = 'red'
|
||||||
if "+" in field.field[i]:
|
if "+" in field.field[i]:
|
||||||
bg = 'orange'
|
bg = 'orange'
|
||||||
|
<<<<<<< HEAD
|
||||||
|
=======
|
||||||
|
|
||||||
|
>>>>>>> d51d6b9fd1009b5d2eec1276597e4b6e34e005b5
|
||||||
buttons[i].configure(bg=bg)
|
buttons[i].configure(bg=bg)
|
||||||
|
|
||||||
|
|
||||||
|
@ -79,20 +85,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')
|
||||||
|
@ -104,12 +136,21 @@ 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)
|
||||||
|
|
||||||
|
<<<<<<< HEAD
|
||||||
|
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()
|
||||||
|
=======
|
||||||
|
window.mainloop()
|
||||||
|
>>>>>>> d51d6b9fd1009b5d2eec1276597e4b6e34e005b5
|
||||||
|
|
|
@ -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