diff --git a/HNC/Exercises/Ship_Battle/ShipDirection.py b/HNC/Exercises/Ship_Battle/ShipDirection.py index 6c002f6..72f4354 100644 --- a/HNC/Exercises/Ship_Battle/ShipDirection.py +++ b/HNC/Exercises/Ship_Battle/ShipDirection.py @@ -1,6 +1,6 @@ from enum import Enum class ShipDirection(Enum): - VERTICAL = 0 - HORIZONTAL = 1 - UNKNOWN = -1 \ No newline at end of file + VERTICAL = "VERTICAL" + HORIZONTAL = "HORIZONTAL" + UNKNOWN = "UNKNOWN" \ No newline at end of file diff --git a/HNC/Exercises/Ship_Battle/ShipField.py b/HNC/Exercises/Ship_Battle/ShipField.py index 9e3790f..50a9e9a 100644 --- a/HNC/Exercises/Ship_Battle/ShipField.py +++ b/HNC/Exercises/Ship_Battle/ShipField.py @@ -1,3 +1,4 @@ +import copy from ShootResult import ShootResult from ShipMode import ShipMode from ShipDirection import ShipDirection @@ -242,4 +243,13 @@ class ShipField: blocked_string += str(self.check_blocked(r, c))[0] + ", " ship_string += self.field[r * self.field_size + c] + ', ' print(blocked_string[:-2] + ' ' + ship_string[:-2]) - print("********************************************************************") \ No newline at end of file + print("********************************************************************") + + + @staticmethod + def convert_to_json(obj): + if isinstance(obj, ShipField): + result = copy.copy(obj.__dict__) + result['field_mode'] = obj.field_mode.value + result['ship_direction'] = obj.ship_direction.value + return result \ No newline at end of file diff --git a/HNC/Exercises/Ship_Battle/ShipMode.py b/HNC/Exercises/Ship_Battle/ShipMode.py index 657f3e7..5517662 100644 --- a/HNC/Exercises/Ship_Battle/ShipMode.py +++ b/HNC/Exercises/Ship_Battle/ShipMode.py @@ -1,5 +1,5 @@ from enum import Enum class ShipMode(Enum): - PUT = 0 - SHOOT = 1 \ No newline at end of file + PUT = "PUT" + SHOOT = "SHOOT" \ 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 31f8f6e..d82802b 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 1b091d4..c3e554d 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 97043df..85cfbec 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/files.py b/HNC/Exercises/Ship_Battle/files.py new file mode 100644 index 0000000..c501b51 --- /dev/null +++ b/HNC/Exercises/Ship_Battle/files.py @@ -0,0 +1,28 @@ +import os + + +def list_levels(): + files = os.listdir(path='.') + result = [] + for file in files: + if file.lower().endswith('.txt'): + result.append(file) + + return result + + +def load(file): + # 1. Загрузить весь файл в строку + f = open(file, 'r') + s = f.read() + + # 2. Заменить все пробелы и переводы строки на "ничего" + s = s.replace(' ', '') + s = s.replace('\n', '') + + # 3. Из полученной строки создать массив, используя разделитель "," + a = s.split(',') + + # a - массив чисел из а + f.close() + return a \ No newline at end of file diff --git a/HNC/Exercises/Ship_Battle/game1.json b/HNC/Exercises/Ship_Battle/game1.json new file mode 100644 index 0000000..badab37 --- /dev/null +++ b/HNC/Exercises/Ship_Battle/game1.json @@ -0,0 +1,7 @@ +{ + "my_field":{ + "field": [], + "ships": [], + "field_mode": "PUT" + } +} \ No newline at end of file diff --git a/HNC/Exercises/Ship_Battle/main.py b/HNC/Exercises/Ship_Battle/main.py index ad1e62c..cbe06d1 100644 --- a/HNC/Exercises/Ship_Battle/main.py +++ b/HNC/Exercises/Ship_Battle/main.py @@ -1,3 +1,4 @@ +import json from tkinter import * from ShipField import ShipField @@ -32,10 +33,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) @@ -76,24 +77,41 @@ def button_enter(buttons, row, col): colorize(my_field, my_buttons) 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 loadbutton_click(event): + data_list = [] + with open('test.json') as lines: + data = json.load(lines) + for i in data['shapes']: + sh1 = create_shape(i) + data_list.append(sh1) + + return data_list + + window = Tk() window.title("Ship Craft!") -window.geometry('940x410') +window.geometry('940x510') window.bind_all('', keypress_handler) -my_field.toggle_ship_direction() -my_field.set_ship_size(4) -my_field.set_ship(1, 1) -my_field.toggle_ship_direction() -my_field.set_ship_size(3) -my_field.set_ship(0, 6) -my_field.set_ship_size(1) -my_field.set_ship(7, 3) - my_buttons = draw_field(window, my_field, 0) 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.grid(column=0, row=11, columnspan=4) + +loadbutton = Button(window, text='Load', width=20, height=2) +loadbutton.bind('', loadbutton_click) +loadbutton.grid(column=5, row=11, columnspan=4) + + window.mainloop() \ No newline at end of file diff --git a/HNC/Exercises/Ship_Battle/test.json b/HNC/Exercises/Ship_Battle/test.json new file mode 100644 index 0000000..3041a88 --- /dev/null +++ b/HNC/Exercises/Ship_Battle/test.json @@ -0,0 +1 @@ +{"shapes": {"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