zoukankan      html  css  js  c++  java
  • 结对-结对编项目作业名称-最终程序

    码云地址:https://gitee.com/546840006/22222/tree/master

    结对成员:祁昊   刘孝东 学号:2015035107047 学号:2015035107044 结对成员合照:

     

     

     

     

    打开game.py直接运行python即可

    实际运行图片:

     

     

     

    代码:

    import random

    import ctypes

     

    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 PopToCheck(self):

            if len(self.toCheck) > 0:

                pos = self.toCheck[-1]

                del self.toCheck[-1]

     

            else:

                pos = None

            return pos

     

        def PushToCheck(self, pos):

            self.toCheck.append(pos)

     

        def ConnectedLight(self, pos):

            x, y = pos

            yield pos

            if x - 1 >= 0: yield x-1,y

            if x + 1 <self.size: yield x+1,y

            if y -1 >= 0: yield x, y-1

            if y+1 < self.size: yield x, y+1

     

        def NotCheckLight(self, pos):

            return [(x,y) for x, y in self.ConnectedLight(pos) if self.clickBoard[x][y] == -1]

     

        def SetCheck(self, pos, value):

            x, y = pos

            self.clickBoard[x][y] = value

     

            for x, y in self.ConnectedLight(pos):

                self.lightBoard[x][y] = 1 - self.lightBoard[x][y]

     

        def Pass(self, pos):

            x, y = pos

            self.clickBoard[x][y] = 0

     

        def CancelPass(self, pos):

            x, y = pos

            self.clickBoard[x][y] = -1

     

        def CheckError(self, pos):

            for p in self.ConnectedLight(pos):

                x, y = p

     

                if self.lightBoard[x][y] == 0 and len(self.NotCheckLight(p)) == 0:

                    return True

            return False

     

    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 __init__(self, parent, pos, board):

            Button.__init__(self, parent, width=2)

            self.board = board

            self.status = 0

            self.x, self.y = pos

            self.SetColor()

            self.bind("<Button-1>",self.onClick)

            self.bind("<Button-3>",self.onRightClick)

     

        def SetColor(self):

            if self.status == 0:

                self.config(bg="#cccccc")

     

            if self.status == 1:

                self.config(bg="#ffcccc")

     

        def onClick(self,event):

            toclick = [(0, 0), (-1,0), (1,0), (0, 1), (0, -1)]

            self.config(text="")

            for x,y in toclick:

                try:

                    b = self.board[(self.x-x, self.y-y)]

                    b.status = 1 - b.status

                    b.SetColor()

                except:

                    pass

     

            temp = 0

            for button in buttons.values():

                if button.status == 1:

                    temp += 1

                if temp == Size * Size:

                    # ctypes.windll.user32.MessageBoxW(0, u'Over!', u'Information', 0)

                    Clear()

     

        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)))

     

            for x, r in enumerate(clickBoard):

                for y, c in enumerate(r):

                    if c == 1:

                        buttons[(y,x)].config(text="*")

     

        def Clear():

            for button in buttons.values():

                button.status = 0

                button.SetColor()

     

                for y in range(3):

                    j = random.randint(0, Size - 1)

                    for x in range(3):

                        k = random.randint(0, Size - 1)

                        pos = j, k

                        c = LightButton(f, pos, buttons)

                        c.onClick("<Button-1>")

        root = Tk()

        root.title("Light Solver")

        f = Frame(root)

        f.pack(side = TOP)

        # Button(f, text="Solve", command=SolveIt).pack(side=LEFT)

        Button(f, text="Clear", command=Clear).pack(side=LEFT)

        buttons = {}

        Size = askinteger("Light Solver", "Please Input Board Size", initialvalue=5)

        for y in range(Size):

            f = Frame(root)

            f.pack(side = LEFT)

            for x in range(Size):

                pos = x, y

                b = LightButton(f, pos, buttons)

                b.pack()

                buttons[pos] = b

     

        for y in range(3):

            j = random.randint(0,Size - 1)

            for x in range(3):

                k = random.randint(0, Size - 1)

                pos = j, k

                c = LightButton(f, pos, buttons)

                c.onClick("<Button-1>")

     

     

        mainloop()

     

     

  • 相关阅读:
    SQL Server外键关系是强制约束,外键值也可以是空(NULL)
    SQL怎么实现SLEEP功能(等待时间) -(转载)
    Why use async requests instead of using a larger threadpool?(转载)
    c# &与&& 和 |与||的区别(转载)
    SQL Server分区表,能否按照多个列作为分区函数的分区依据(转载)
    怎样避免C#中将小数转换为字符串时出现科学记数法
    锁不住的查询(转载)
    JSON中如何转义字符串中的双引号(转载)
    StreamWriter结合UTF-8编码使用不当,会造成BOM(Byte Order Mark )问题生成乱码(转载)
    HttpWebRequest的timeout和ReadWriteTimeout(转载)
  • 原文地址:https://www.cnblogs.com/qihao10086/p/7840634.html
Copyright © 2011-2022 走看看