diff --git a/HNC/Exercises/Ship_Battle/ShipDirection.py b/HNC/Exercises/Ship_Battle/ShipDirection.py index 163f2f1..77bae97 100644 --- a/HNC/Exercises/Ship_Battle/ShipDirection.py +++ b/HNC/Exercises/Ship_Battle/ShipDirection.py @@ -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 \ No newline at end of file diff --git a/HNC/Exercises/Ship_Battle/ShipField.py b/HNC/Exercises/Ship_Battle/ShipField.py index 3045881..180b454 100644 --- a/HNC/Exercises/Ship_Battle/ShipField.py +++ b/HNC/Exercises/Ship_Battle/ShipField.py @@ -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 diff --git a/HNC/Exercises/Ship_Battle/ShipMode.py b/HNC/Exercises/Ship_Battle/ShipMode.py index 5517662..42b7342 100644 --- a/HNC/Exercises/Ship_Battle/ShipMode.py +++ b/HNC/Exercises/Ship_Battle/ShipMode.py @@ -2,4 +2,14 @@ from enum import Enum class ShipMode(Enum): PUT = "PUT" - SHOOT = "SHOOT" \ No newline at end of file + 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 \ No newline at end of file diff --git a/HNC/Exercises/Ship_Battle/__pycache__/ShipDirection.cpython-311.pyc b/HNC/Exercises/Ship_Battle/__pycache__/ShipDirection.cpython-311.pyc index 1dbdd77..10fb02d 100644 Binary files a/HNC/Exercises/Ship_Battle/__pycache__/ShipDirection.cpython-311.pyc and b/HNC/Exercises/Ship_Battle/__pycache__/ShipDirection.cpython-311.pyc differ diff --git a/HNC/Exercises/Ship_Battle/__pycache__/ShipField.cpython-311.pyc b/HNC/Exercises/Ship_Battle/__pycache__/ShipField.cpython-311.pyc index 857639c..2d51748 100644 Binary files a/HNC/Exercises/Ship_Battle/__pycache__/ShipField.cpython-311.pyc and b/HNC/Exercises/Ship_Battle/__pycache__/ShipField.cpython-311.pyc differ diff --git a/HNC/Exercises/Ship_Battle/__pycache__/ShipMode.cpython-311.pyc b/HNC/Exercises/Ship_Battle/__pycache__/ShipMode.cpython-311.pyc index 85cfbec..718f118 100644 Binary files a/HNC/Exercises/Ship_Battle/__pycache__/ShipMode.cpython-311.pyc and b/HNC/Exercises/Ship_Battle/__pycache__/ShipMode.cpython-311.pyc differ diff --git a/HNC/Exercises/Ship_Battle/enemy_field.json b/HNC/Exercises/Ship_Battle/enemy_field.json new file mode 100644 index 0000000..e05ab3b --- /dev/null +++ b/HNC/Exercises/Ship_Battle/enemy_field.json @@ -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"}} \ No newline at end of file diff --git a/HNC/Exercises/Ship_Battle/main.py b/HNC/Exercises/Ship_Battle/main.py index a5c85ac..fd68567 100644 --- a/HNC/Exercises/Ship_Battle/main.py +++ b/HNC/Exercises/Ship_Battle/main.py @@ -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('', 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('', 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() \ No newline at end of file diff --git a/HNC/Exercises/Ship_Battle/my_field.json b/HNC/Exercises/Ship_Battle/my_field.json new file mode 100644 index 0000000..de56baf --- /dev/null +++ b/HNC/Exercises/Ship_Battle/my_field.json @@ -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"}} \ No newline at end of file diff --git a/test.json b/test.json index 085d319..685da40 100644 --- a/test.json +++ b/test.json @@ -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"}} \ No newline at end of file +{"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"}} \ No newline at end of file