formated a document

This commit is contained in:
Heli-o 2023-12-29 16:35:56 +01:00
parent 81a597fc19
commit 6d0b218dc3

View file

@ -2,6 +2,7 @@ import copy
import math import math
from PIL import Image, ImageDraw from PIL import Image, ImageDraw
class Board: class Board:
def __init__(self, size=0): def __init__(self, size=0):
self.size = size self.size = size
@ -35,12 +36,14 @@ class Board:
self._colors[9] = "#FA7921" # pumpkin self._colors[9] = "#FA7921" # pumpkin
self._colors[10] = "#566E3D" # dark olive green self._colors[10] = "#566E3D" # dark olive green
def inBoard(self, p, q): def inBoard(self, p, q):
"""return True if (p,q) is valid coordinate""" """return True if (p,q) is valid coordinate"""
return (q>= 0) and (q < self.size) and (p >= -(q//2)) and (p < (self.size - q//2)) return (
(q >= 0)
and (q < self.size)
and (p >= -(q // 2))
and (p < (self.size - q // 2))
)
def rotateRight(self, p, q): def rotateRight(self, p, q):
pp = -q pp = -q
@ -52,11 +55,8 @@ class Board:
qq = -p qq = -p
return pp, qq return pp, qq
def saveImage(self, filename): def saveImage(self, filename):
""" draw actual board to png """draw actual board to png"""
"""
cellRadius = 60 cellRadius = 60
cellWidth = int(cellRadius * (3**0.5)) cellWidth = int(cellRadius * (3**0.5))
@ -65,13 +65,12 @@ class Board:
width = cellWidth * self.size + cellRadius * 3 width = cellWidth * self.size + cellRadius * 3
height = cellHeight * self.size height = cellHeight * self.size
img = Image.new('RGB',(width,height),"white") img = Image.new("RGB", (width, height), "white")
draw = ImageDraw.Draw(img) draw = ImageDraw.Draw(img)
lineColor = (50, 50, 50) lineColor = (50, 50, 50)
for p in self.board: for p in self.board:
for q in self.board[p]: for q in self.board[p]:
cx = cellRadius * (math.sqrt(3) * p + math.sqrt(3) / 2 * q) + cellRadius cx = cellRadius * (math.sqrt(3) * p + math.sqrt(3) / 2 * q) + cellRadius
@ -91,10 +90,11 @@ class Board:
pts.append(pts[0]) pts.append(pts[0])
pts.append(pts[1]) pts.append(pts[1])
draw.line(pts, fill="black", width=1) draw.line(pts, fill="black", width=1)
draw.text([cx-3,cy-3], "{} {}".format(p,q), fill="black", anchor="m") draw.text(
[cx - 3, cy - 3], "{} {}".format(p, q), fill="black", anchor="m"
)
img.save(filename) img.save(filename)
def loadBoard(self, filename): def loadBoard(self, filename):
board = {} board = {}
fread = open(filename, "rt") fread = open(filename, "rt")
@ -109,7 +109,9 @@ class Board:
self.board = board self.board = board
self.size = size + 1 self.size = size + 1
import sys import sys
# alternativa: nacteni ze souboru # alternativa: nacteni ze souboru
b = Board() b = Board()
b.loadBoard(sys.argv[1]) b.loadBoard(sys.argv[1])
@ -125,6 +127,8 @@ for p in b.board:
spider_q = q spider_q = q
smer = ((0, -1), (1, -1), (1, 0), (0, 1), (-1, 1), (-1, 0)) smer = ((0, -1), (1, -1), (1, 0), (0, 1), (-1, 1), (-1, 0))
def okoli(b, p, q): def okoli(b, p, q):
o = [0] * 6 o = [0] * 6
for i in range(6): for i in range(6):
@ -135,14 +139,18 @@ def okoli(b, p, q):
o[i] = -1 o[i] = -1
return o return o
def spider_tah1(p, q, o): def spider_tah1(p, q, o):
mozne_tahy = [] mozne_tahy = []
for i in range(6): for i in range(6):
if o[i]==0 and ((o[(i-1)%6]==0 and o[(i+1)%6]==1) if o[i] == 0 and (
or(o[(i-1)%6]==1 and o[(i+1)%6]==0)): (o[(i - 1) % 6] == 0 and o[(i + 1) % 6] == 1)
or (o[(i - 1) % 6] == 1 and o[(i + 1) % 6] == 0)
):
mozne_tahy.append([p + smer[i][0], q + smer[i][1]]) mozne_tahy.append([p + smer[i][0], q + smer[i][1]])
return mozne_tahy return mozne_tahy
o_spider = okoli(b, spider_p, spider_q) o_spider = okoli(b, spider_p, spider_q)
print(o_spider) print(o_spider)
@ -173,13 +181,3 @@ for i in range(3):
pos = pos2 pos = pos2
pos2.sort() pos2.sort()
print(pos2) print(pos2)