结对-结对编项目作业名称-开发过程
实现了灯板框架
def InitTable(size, value):
return [[value for i in range(size)] for j in range(size)]
class LightBoard:
def __init__(self, size, status=""):
self.clickBoard = InitTable(size, -1)
self.lightBoard = InitTable(size, 0)
self.toCheck = []
for i in range(size):
for j in range(size):
self.PushToCheck((i,j))
for pos, value in enumerate(status):
if value == "1":
self.lightBoard[pos/size][pos%size] = 1
self.size = size
def Solve(board):
clickpos = board.PopToCheck()
if clickpos == None:
return board.clickBoard
pos = board.NotCheckLight(clickpos)[0]
board.SetCheck(pos, 1)
if not board.CheckError( pos ):
r = Solve(board)
if r: return r
board.SetCheck(pos, -1)
board.Pass(pos)
if not board.CheckError( pos ):
r = Solve(board)
if r: return r
board.CancelPass(pos)
board.PushToCheck(clickpos)
return False
from Tkinter import *
class LightButton(Button):
def onRightClick(self, event):
self.status = 1 - self.status
self.SetColor()
if __name__ == "__main__":
from tkSimpleDialog import askinteger
def SolveIt():
boardStatus = ["0"] * Size * Size
for pos, light in buttons.items():
if light.status == 1:
boardStatus[pos[1]*Size+pos[0]] = "1"
clickBoard = Solve(LightBoard(Size, "".join(boardStatus)))
def Clear():
for button in buttons.values():
button.status = 0
button.SetColor()
root = Tk()
root.title("Light Solver")
buttons = {}
Size = askinteger("Light Solver", "Please Input Board Size")