记一次墨菲定律

 

Repopulate Form

写了一个抽奖脚本,发生了一个微妙的bug……这个bug的发生概率我估计比中奖还小……(虽说我也中奖了)

while True:
    if(int(time.localtime(time.time()).tm_min)>48 and int(time.localtime(time.time()).tm_hour)>10):
        print("PostAt:", time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
        time.sleep(random.randint(3,20))
    elif(int(time.localtime(time.time()).tm_min)<50):
        print('让我睡一分钟')
        time.sleep(60)

理论上应该不会在11点48前发送的,结果……发生了一件让我惊奇的事情

QQ图片20160520090842

看了一眼判断,在

int(time.localtime(time.time()).tm_min) and int(time.localtime(time.time()).tm_hour)

所以这个bug必须在判断分钟的时候是10:59

判断小时的时候正好跳到11:00

所以要避免这个发生的话……

int(time.localtime(time.time()).tm_hour) and int(time.localtime(time.time()).tm_min)

或者放个变量存一下,还能快一丁点

#EOF

……Python这是有多慢。

无禁手五子棋的胜利验证

2018年3月31日 11:50:22更新:

突然感觉每个逻辑都写一遍特别蠢 于是简化了一下

class Board:
    board = []  # 棋盘
    bangswin = 5  # 可以改成六子棋
 
    def __init__(self, x=10, y=10):  # 初始化棋盘
        for x1 in range(x):
            self.board.append([])
            for y1 in range(y):
                self.board[x1].append(0)
 
    def resetBoard(self):  # 重置棋盘
        for x in range(len(self.board)):
            for y in range(len(self.board[0])):
                self.board[x][y] = 0
 
    def winnerIs(self, who):  # 宣告胜利者
        self.printBoard()
        print("Winner is", who)
        self.resetBoard()
 
    def printBoard(self):  # 打印棋盘
        for row in self.board:
            print(row)
 
    def dropPiece(self, x, y, who):  # 下子
        if (self.board[x][y] == 0):
            self.board[x][y] = who;
            self.checkPiece(x, y, who)
 
    def checkPieceOnDrection(self, x, y, xv, yv, who):  # 参数化方向验证
        x1 = x
        y1 = y
        # 负向检测
        bang = 1
        while (x <= len(self.board) and y <= len(self.board[0]) and x >= 0 and y >= 0):
            x = x - xv
            y = y - yv
            if (self.board[x][y] == who):
                bang += 1
            else:
                break
        x = x1
        y = y1
        # 正向检测
        while (x <= len(self.board) and y <= len(self.board[0]) and x >= 0 and y >= 0):
            x = x + xv
            y = y + yv
            if (self.board[x][y] == who):
                bang += 1
            else:
                break
        if (bang == self.bangswin):
            self.winnerIs(who)
            return
 
    def checkPiece(self, x, y, who):  # 赢家验证
        self.checkPieceOnDrection(x, y, 1, 0, who); # 从左到右
        self.checkPieceOnDrection(x, y, 0, 1, who); # 从上到下
        self.checkPieceOnDrection(x, y, 1, 1, who); # 左上右下
        self.checkPieceOnDrection(x, y, 1, -1, who); # 右上左下

 

原文章:

米字型验证,Python

class Board:
    board = [] #棋盘
    bangswin = 5 #可以改成六子棋
    def __init__(self, x=10, y=10): #初始化棋盘
        for x1 in range(x):
            self.board.append([])
            for y1 in range(y):
                self.board[x1].append(0)
    def resetBoard(self): #重置棋盘
        for x in range(len(self.board)):
            for y in range(len(self.board[0])):
                self.board[x][y] = 0
    def winnerIs(self, who): #宣告胜利者
        self.printBoard()
        print("Winner is", who)
        self.resetBoard()
    def printBoard(self): #打印棋盘
        for row in self.board:
            print(row)
    def dropPiece(self,x,y,who): #下子
        if(self.board[x][y]==0):
            self.board[x][y]=who;
            self.checkPiece(x,y,who)
    def checkPiece(self, x, y, who): #验证
        x1 = x
        y1 = y
        # 左上到右下检测
        bang = 1
        while (x >= 0 and y >= 0):
            x -= 1
            y -= 1
            if (self.board[x][y] == who):
                bang += 1
            else:
                break
        x = x1
        y = y1
        while (x <= len(self.board) and y <= len(self.board[0])):
            x += 1
            y += 1
            if (self.board[x][y] == who):
                bang += 1
            else:
                break
        if (bang >= self.bangswin):
            self.winnerIs(who)
            return
 
        # 上到下
        bang = 1
        x = x1
        y = y1
        while (x >= 0 and y >= 0):
            x -= 1
            if (self.board[x][y] == who):
                bang += 1
            else:
                break
        x = x1
        y = y1
        while (x <= len(self.board) and y <= len(self.board[0])):
            x += 1
            if (self.board[x][y] == who):
                bang += 1
            else:
                break
        if (bang >= self.bangswin):
            self.winnerIs(who)
            return
 
        # 右上到左下
        bang = 1
        x = x1
        y = y1
        while (x >= 0 and y >= 0):
            x -= 1
            y += 1
            if (self.board[x][y] == who):
                bang += 1
            else:
                break
        x = x1
        y = y1
        while (x <= len(self.board) and y <= len(self.board[0])):
            x += 1
            y -= 1
            if (self.board[x][y] == who):
                bang += 1
            else:
                break
        if (bang >= self.bangswin):
            self.winnerIs(who)
            return
 
        # 左到右
        bang = 1
        x = x1
        y = y1
        while (x >= 0 and y >= 0):
            y -= 1
            if (self.board[x][y] == who):
                bang += 1
            else:
                break
        x = x1
        y = y1
        while (x <= len(self.board) and y <= len(self.board[0])):
            y += 1
            if (self.board[x][y] == who):
                bang += 1
            else:
                break
        if (bang >= self.bangswin):
            self.winnerIs(who)
            return

 

比较残念的是这个思路无法验证残局……把每个子都扔进去?