Up_to_date_commit
This commit is contained in:
parent
65b627e032
commit
c4563dc184
|
@ -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)
|
|
@ -0,0 +1,4 @@
|
||||||
|
from enum import Enum
|
||||||
|
|
||||||
|
def enumeration(Enum):
|
||||||
|
|
|
@ -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
|
|
@ -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
|
||||||
|
|
|
@ -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
|
|
@ -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')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
{"shapes": [{"width": 5, "height": 10, "color": "red", "type": "Rectangle"}, {"radius": 7, "color": "blue", "type": "Circle"}, {"side": 0, "color": "unknown", "type": "Square"}]}
|
|
@ -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()
|
Loading…
Reference in New Issue