From c4563dc1844d4d6e19862291eed55e3c523cb970 Mon Sep 17 00:00:00 2001 From: danii Date: Mon, 6 Nov 2023 15:35:22 +0100 Subject: [PATCH] Up_to_date_commit --- HNC/Exercises/JSON/Circle.py | 13 ++++ HNC/Exercises/JSON/Enums.py | 4 + HNC/Exercises/JSON/Rectangle.py | 13 ++++ HNC/Exercises/JSON/Shape.py | 23 ++++++ HNC/Exercises/JSON/Square.py | 12 +++ HNC/Exercises/JSON/main.py | 119 ++++++++++++++++++++++++++++++ HNC/Exercises/JSON/shapes.json | 1 + HNC/Exercises/Ship_Battle/main.py | 81 ++++++++++++++++++++ 8 files changed, 266 insertions(+) create mode 100644 HNC/Exercises/JSON/Circle.py create mode 100644 HNC/Exercises/JSON/Enums.py create mode 100644 HNC/Exercises/JSON/Rectangle.py create mode 100644 HNC/Exercises/JSON/Shape.py create mode 100644 HNC/Exercises/JSON/Square.py create mode 100644 HNC/Exercises/JSON/main.py create mode 100644 HNC/Exercises/JSON/shapes.json create mode 100644 HNC/Exercises/Ship_Battle/main.py diff --git a/HNC/Exercises/JSON/Circle.py b/HNC/Exercises/JSON/Circle.py new file mode 100644 index 0000000..ab888b8 --- /dev/null +++ b/HNC/Exercises/JSON/Circle.py @@ -0,0 +1,13 @@ +import math +from Shape import Shape + + +class Circle(Shape): + def __init__(self, radius): + self.radius = radius + + def perimeter(self): + return 2 * math.pi * self.radius + + def area(self): + return math.pi + (self.radius ** 2) diff --git a/HNC/Exercises/JSON/Enums.py b/HNC/Exercises/JSON/Enums.py new file mode 100644 index 0000000..97901ac --- /dev/null +++ b/HNC/Exercises/JSON/Enums.py @@ -0,0 +1,4 @@ +from enum import Enum + +def enumeration(Enum): + \ No newline at end of file diff --git a/HNC/Exercises/JSON/Rectangle.py b/HNC/Exercises/JSON/Rectangle.py new file mode 100644 index 0000000..99e5e29 --- /dev/null +++ b/HNC/Exercises/JSON/Rectangle.py @@ -0,0 +1,13 @@ +from Shape import Shape + + +class Rectangle(Shape): + def __init__(self, width, height): + self.width = width + self.height = height + + def perimeter(self): + return 2 * (self.width + self.height) + + def area(self): + return self.width * self.height diff --git a/HNC/Exercises/JSON/Shape.py b/HNC/Exercises/JSON/Shape.py new file mode 100644 index 0000000..8990f48 --- /dev/null +++ b/HNC/Exercises/JSON/Shape.py @@ -0,0 +1,23 @@ +import math +from abc import abstractmethod + + +class Shape: + color = '' + + @abstractmethod + def area(self): + pass + + @abstractmethod + def perimeter(self): + pass + + @staticmethod + def convert_to_json(obj): + if isinstance(obj, Shape): + result = obj.__dict__ + result['type'] = obj.__class__.__name__ + result['color'] = obj.color + return result + diff --git a/HNC/Exercises/JSON/Square.py b/HNC/Exercises/JSON/Square.py new file mode 100644 index 0000000..1733cf7 --- /dev/null +++ b/HNC/Exercises/JSON/Square.py @@ -0,0 +1,12 @@ +from Shape import Shape + + +class Square(Shape): + def __init__(self, side): + self.side = side + + def perimeter(self): + return 4 * self.side + + def area(self): + return self.side ** 2 diff --git a/HNC/Exercises/JSON/main.py b/HNC/Exercises/JSON/main.py new file mode 100644 index 0000000..754ba7b --- /dev/null +++ b/HNC/Exercises/JSON/main.py @@ -0,0 +1,119 @@ +import json +import random + +from Shape import Shape +from Circle import Circle +from Rectangle import Rectangle +from Square import Square + + +def safe_read(obj, property, default_value): + if property in obj: + return obj[property] + + return default_value + + +def create_shape(json): + if 'type' in json: + shape_type = json['type'].lower() + + if shape_type == 'circle': + radius = safe_read(json, 'radius', 0) + obj = Circle(radius) + + elif shape_type == 'square': + side = safe_read(json, 'side', 0) + obj = Square(side) + + elif shape_type == 'rectangle': + width = safe_read(json, 'width', 0) + height = safe_read(json, 'height', 0) + obj = Rectangle(width, height) + + else: + raise TypeError(f'вот тебе >>>(оIo), а не фигура {shape_type}') + + obj.color = safe_read(json, 'color', 'unknown') + + return obj + + +def generate_shape(): + types = ['square', 'circle', 'rectangle'] + + rnd = random.randint(0, len(types) - 1) + + shape_type = types[rnd] + + if shape_type == 'circle': + obj = Circle(rnd) + + elif shape_type == 'square': + obj = Square(rnd) + + elif shape_type == 'rectangle': + obj = Rectangle(rnd, rnd) + + else: + raise TypeError(f'Происходит что-то непонятное') + + obj.color = 'unknown' + + return obj + +def generate_shape(): + max_length = [0, 100] + types = ['square', 'circle', 'rectangle'] + colors = ['red', 'green', 'blue', 'yellow', 'black', 'white'] + + type_index = random.randint(0, len(types) - 1) + shape_type = types[type_index] + + if shape_type == 'circle': + obj = Circle(random.randint(1, 100)) + elif shape_type == 'square': + obj = Square(random.randint(1, 100)) + elif shape_type == 'rectangle': + obj = Rectangle(random.randint(1, 100), random.randint(1, 100)) + else: + raise TypeError(f'Происходит что-то непонятное') + + color_index = random.randint(0, len(colors) - 1) + obj.color = colors[color_index] + + return obj + + +def json_to_python(filename): + data_list = [] + with open(filename) as lines: + data = json.load(lines) + for i in data['shapes']: + sh1 = create_shape(i) + data_list.append(sh1) + + return data_list + + +def python_to_json(data, filename): + with open(filename, 'w') as f: + json.dump({'shapes': data}, f, default=Shape.convert_to_json) + + +def filter_shapes(data, area): + return [shape for shape in data if shape.area() >= area] + + +shape_list = json_to_python('shapes.json') + +shape_list = filter_shapes(shape_list, 20) + +shape_list.append(generate_shape()) + +python_to_json(shape_list, 'shapes.json') + + + + + diff --git a/HNC/Exercises/JSON/shapes.json b/HNC/Exercises/JSON/shapes.json new file mode 100644 index 0000000..e8fd2ce --- /dev/null +++ b/HNC/Exercises/JSON/shapes.json @@ -0,0 +1 @@ +{"shapes": [{"width": 5, "height": 10, "color": "red", "type": "Rectangle"}, {"radius": 7, "color": "blue", "type": "Circle"}, {"side": 0, "color": "unknown", "type": "Square"}]} \ No newline at end of file diff --git a/HNC/Exercises/Ship_Battle/main.py b/HNC/Exercises/Ship_Battle/main.py new file mode 100644 index 0000000..b583d68 --- /dev/null +++ b/HNC/Exercises/Ship_Battle/main.py @@ -0,0 +1,81 @@ +from tkinter import * + +empty_field = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '] + +# set_ship(1, 1, 4, 1) +step1_field = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', '1', '1', '1', '1', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '] + +# set_ship(0, 5, 3, 0) +step2_field = [' ', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '] + +my_field = list(empty_field) +enemy_field = list(empty_field) + + +def set_ship(row, col, size, direction, field): + # size: 1, 2, 3, 4 + # direction: вниз(0), вправо(1) + direction_right_or_left = 0 + direction_down_or_up = 1 + size_of_ship = [1, 2, 3, 4] + + if size >= len(my_field): + size = len(my_field) - 1 + + + pass + + +def draw_field(window, field): + canvas = Canvas(window, width=500, height=500) + canvas.pack() + + square_size = 50 + + for row in range(10): + for col in range(10): + if (row + col) % 2 == 0: + color = 'white' + else: + color = 'blue' + x1 = col * square_size + y1 = row * square_size + x2 = x1 * square_size + y2 = y1 * square_size + canvas.create_rectangle(x1, y1, x2, y2, fill=color) + + +window = Tk() +window.title("Ship Craft!") +window.geometry('800x600') + +draw_field(window, my_field) + +window.mainloop()