formated a document
This commit is contained in:
parent
81a597fc19
commit
6d0b218dc3
1 changed files with 124 additions and 126 deletions
250
spider.py
250
spider.py
|
@ -2,184 +2,182 @@ 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
|
||||||
self.board = {}
|
self.board = {}
|
||||||
|
|
||||||
#create empty board as a dictionary
|
# create empty board as a dictionary
|
||||||
self.b2 = {}
|
self.b2 = {}
|
||||||
for p in range(-self.size,self.size):
|
for p in range(-self.size, self.size):
|
||||||
for q in range(-self.size, self.size):
|
for q in range(-self.size, self.size):
|
||||||
if self.inBoard(p,q):
|
if self.inBoard(p, q):
|
||||||
if not p in self.board:
|
if not p in self.board:
|
||||||
self.board[p] = {}
|
self.board[p] = {}
|
||||||
self.board[p][q] = 0
|
self.board[p][q] = 0
|
||||||
|
|
||||||
if not q in self.b2:
|
if not q in self.b2:
|
||||||
self.b2[q] = {}
|
self.b2[q] = {}
|
||||||
self.b2[q][p] = 0
|
self.b2[q][p] = 0
|
||||||
|
|
||||||
#this is for visualization and to synchronize colors between png/js
|
# this is for visualization and to synchronize colors between png/js
|
||||||
self._colors = {}
|
self._colors = {}
|
||||||
self._colors[-1] = "#fdca40" #sunglow
|
self._colors[-1] = "#fdca40" # sunglow
|
||||||
self._colors[0] = "#ffffff" #white
|
self._colors[0] = "#ffffff" # white
|
||||||
self._colors[1] = "#947bd3" #medium purple
|
self._colors[1] = "#947bd3" # medium purple
|
||||||
self._colors[2] = "#ff0000" #red
|
self._colors[2] = "#ff0000" # red
|
||||||
self._colors[3] = "#00ff00" #green
|
self._colors[3] = "#00ff00" # green
|
||||||
self._colors[4] = "#0000ff" #blue
|
self._colors[4] = "#0000ff" # blue
|
||||||
self._colors[5] = "#566246" #ebony
|
self._colors[5] = "#566246" # ebony
|
||||||
self._colors[6] = "#a7c4c2" #opan
|
self._colors[6] = "#a7c4c2" # opan
|
||||||
self._colors[7] = "#ADACB5" #silver metalic
|
self._colors[7] = "#ADACB5" # silver metalic
|
||||||
self._colors[8] = "#8C705F" #liver chestnut
|
self._colors[8] = "#8C705F" # liver chestnut
|
||||||
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):
|
||||||
|
"""return True if (p,q) is valid coordinate"""
|
||||||
def inBoard(self,p,q):
|
return (
|
||||||
""" return True if (p,q) is valid coordinate """
|
(q >= 0)
|
||||||
return (q>= 0) and (q < self.size) and (p >= -(q//2)) and (p < (self.size - q//2))
|
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
|
||||||
qq = p+q
|
qq = p + q
|
||||||
return pp,qq
|
return pp, qq
|
||||||
|
|
||||||
def rotateLeft(self, p,q):
|
def rotateLeft(self, p, q):
|
||||||
pp = p+q
|
pp = p + q
|
||||||
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))
|
||||||
cellHeight = 2*cellRadius
|
cellHeight = 2 * cellRadius
|
||||||
|
|
||||||
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
|
||||||
cy = cellRadius*(0*p + 3/2*q) + cellRadius
|
cy = cellRadius * (0 * p + 3 / 2 * q) + cellRadius
|
||||||
|
|
||||||
pts = []
|
pts = []
|
||||||
for a in [30,90,150,210,270,330]:
|
for a in [30, 90, 150, 210, 270, 330]:
|
||||||
nx = cx + cellRadius * math.cos(a*math.pi/180)
|
nx = cx + cellRadius * math.cos(a * math.pi / 180)
|
||||||
ny = cy + cellRadius * math.sin(a*math.pi/180)
|
ny = cy + cellRadius * math.sin(a * math.pi / 180)
|
||||||
pts.append(nx)
|
pts.append(nx)
|
||||||
pts.append(ny)
|
pts.append(ny)
|
||||||
color = "#ff00ff" #pink is for values out of range -1,..10
|
color = "#ff00ff" # pink is for values out of range -1,..10
|
||||||
if self.board[p][q] in self._colors:
|
if self.board[p][q] in self._colors:
|
||||||
color = self._colors[self.board[p][q]]
|
color = self._colors[self.board[p][q]]
|
||||||
|
|
||||||
draw.polygon(pts,fill=color)
|
draw.polygon(pts, fill=color)
|
||||||
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")
|
||||||
size = -1
|
size = -1
|
||||||
for line in fread:
|
for line in fread:
|
||||||
p,q,value = list(map(int, line.strip().split()))
|
p, q, value = list(map(int, line.strip().split()))
|
||||||
size = max(size, q)
|
size = max(size, q)
|
||||||
if p not in board:
|
if p not in board:
|
||||||
board[p] = {}
|
board[p] = {}
|
||||||
board[p][q] = value
|
board[p][q] = value
|
||||||
fread.close()
|
fread.close()
|
||||||
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])
|
||||||
|
|
||||||
# prochazeni radku/sloupce:
|
# prochazeni radku/sloupce:
|
||||||
b_copy={}
|
b_copy = {}
|
||||||
for p in b.board:
|
for p in b.board:
|
||||||
b_copy[p]={}
|
b_copy[p] = {}
|
||||||
for q in b.board[p]:
|
for q in b.board[p]:
|
||||||
b_copy[p][q]=b.board[p][q]
|
b_copy[p][q] = b.board[p][q]
|
||||||
if b.board[p][q]==2:
|
if b.board[p][q] == 2:
|
||||||
spider_p = p
|
spider_p = p
|
||||||
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):
|
||||||
p2, q2 = smer[i]
|
p2, q2 = smer[i]
|
||||||
if b.inBoard(p+p2, q+q2):
|
if b.inBoard(p + p2, q + q2):
|
||||||
o[i]=b.board[p+p2][q+q2]
|
o[i] = b.board[p + p2][q + q2]
|
||||||
else:
|
else:
|
||||||
o[i]=-1
|
o[i] = -1
|
||||||
return o
|
return o
|
||||||
|
|
||||||
|
|
||||||
|
def spider_tah1(p, q, o):
|
||||||
|
mozne_tahy = []
|
||||||
|
for i in range(6):
|
||||||
|
if o[i] == 0 and (
|
||||||
|
(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]])
|
||||||
|
return mozne_tahy
|
||||||
|
|
||||||
def spider_tah1(p,q,o):
|
|
||||||
mozne_tahy=[]
|
|
||||||
for i in range(6):
|
|
||||||
if o[i]==0 and ((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]])
|
|
||||||
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)
|
||||||
|
|
||||||
komponenty=0
|
komponenty = 0
|
||||||
for i in range(6):
|
for i in range(6):
|
||||||
if o_spider[i]==1 and o_spider[(i+1)%6]!=1:
|
if o_spider[i] == 1 and o_spider[(i + 1) % 6] != 1:
|
||||||
komponenty+=1
|
komponenty += 1
|
||||||
|
|
||||||
if komponenty>1:
|
if komponenty > 1:
|
||||||
flood_fill()
|
flood_fill()
|
||||||
|
|
||||||
pos=[[spider_p, spider_q]]
|
pos = [[spider_p, spider_q]]
|
||||||
dont_go_back = pos[:]
|
dont_go_back = pos[:]
|
||||||
b.board[spider_p][spider_q]=0
|
b.board[spider_p][spider_q] = 0
|
||||||
|
|
||||||
for i in range(3):
|
for i in range(3):
|
||||||
pos2=[]
|
pos2 = []
|
||||||
print(i,pos)
|
print(i, pos)
|
||||||
for p,q in pos:
|
for p, q in pos:
|
||||||
mozne = spider_tah1(p,q,okoli(b,p,q))
|
mozne = spider_tah1(p, q, okoli(b, p, q))
|
||||||
for x in mozne:
|
for x in mozne:
|
||||||
if not x in dont_go_back:
|
if not x in dont_go_back:
|
||||||
print("jdu vpred",x)
|
print("jdu vpred", x)
|
||||||
pos2.append(x)
|
pos2.append(x)
|
||||||
dont_go_back.append(x)
|
dont_go_back.append(x)
|
||||||
else:
|
else:
|
||||||
print("jdu zpet",x)
|
print("jdu zpet", x)
|
||||||
pos=pos2
|
pos = pos2
|
||||||
pos2.sort()
|
pos2.sort()
|
||||||
print(pos2)
|
print(pos2)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue