我正在编写一个代码,试图复制Martin Kool的0hn0游戏。 在任务的最后一部分,我需要创建一个函数“ autosolve(archive)”来解决0hn0电路板本身。
这是原始游戏:https://0hn0.com/(玩法)
我尝试使用与“ isCompleted(self)”部分相同的结构,用值区分每个点,并在其相邻的正方形中添加蓝色点;一旦它们的值等于计数器,则停止放置蓝色点,如果蓝色点附近有灰色的点,则将它们变成红色。 当蓝点开始与不同值的点进行交互时,就会出现问题。
class Dot: #__init__, __str__
def __init__(self, color, updatable = True, value = None):
if not(color in ["Blue", "Grey", "Red"]) :
raise Exception("Wrong color")
self.color = color
self.updatable = updatable
self.value = value
def __str__(self): #dots as str
if self.color == "Grey" :
return "[ ]"
if self.color == "Red" :
return "[R]"
if self.color == "Blue" :
if self.value != None :
return "[{0}]".format(self.value)
else :
return "[B]"
class Board: #__init__, __str__, fromFile, addDot, isCompleted
def __init__(self, archive):
self.board = Board.fromFile(archive)
@staticmethod
def fromFile(archive):
fileBoard = [] #we'll get a square matrix shaped board
file = open(archive, 'r')
for line in file :
row = []
chain = line.strip().split(",") #converts every str line in list
for character in chain :
if character == "" :
row.append(Dot("Grey", True, None))
elif character == "." :
row.append(Dot("Red", False, None))
elif character == "*" :
row.append(Dot("Blue", False, None))
elif character.isdigit() :
row.append(Dot("Blue", False, int(character)))
else :
raise Exception("Wrong file")
fileBoard.append(row) #creates the rows of the base matrix
return fileBoard
def __str__(self) : #gives the board as a str
chain = "\n"
for row in self.board :
for dot in row :
chain += str(dot)
chain += "\n"
return chain
def addDot(self, x, y, color): #add dot in row x+1 & col y+1, color
if self.board[x][y].updatable == True : #updatable dot
self.board[x][y].color = color
else :
print("You can't move this dot")
def isCompleted(self): #board cheker
completed = True
counter = 0
for x in range(len(self.board)) : #Check if there are dots left to put
for y in range(len(self.board)) :
if self.board[x][y].color == "Grey" :
completed = False
print("There are still free positions")
return completed
else :
continue
for x in range(len(self.board)) : #Check if the values are correct
for y in range(len(self.board)) :
if self.board[x][y].value != None : #chek all directions
for i in range(1, len(self.board) - x) :
if self.board[x+i][y].color == "Blue" :
counter += 1
else:
break
for i in range(1, len(self.board) - y) :
if self.board[x][y+i].color == "Blue" :
counter += 1
else:
break
for i in range(0, x) :
i += 1
if self.board[x-i][y].color == "Blue" :
counter += 1
else :
break
for i in range(0, y) :
i += 1
if self.board[x][y-i].color == "Blue" :
counter += 1
else :
break
if counter == self.board[x][y].value : #all correct
counter = 0
else : #board dots must be changed
counter = 0
completed = False
print(self.board[x][y], "in {0}x{1}".format(x+1, y+1),\
"sees wrong")
else :
continue
return completed
通过此功能,可以使用“ run0hn0(archive)”功能在控制台中玩游戏。 (正在将一个.txt文件归档,该文件包含4行,每行4个逗号。
def run0hn0(archive): #archive is a .txt
myBoard = Board(archive)
while myBoard.isCompleted() == False :
print(myBoard)
x = input("Row number: ")
y = input("Col number: ")
color = input("Dot color: ")
myBoard.addDot(int(x)-1, int(y)-1, str(color))
if myBoard.isCompleted() == True:
print("Congratulations", myBoard)
return None
谁必须自动解决给定板的功能,我必须写:def autosolve(board)
建议我们使用递归来找到所有可能的解决方案,并在它们之间选择正确的解决方案,但我想不起。 谢谢