Python Connect Four游戏获胜功能

时间:2011-10-18 23:25:31

标签: python tkinter

除了win函数和引力之外,我的Connect Four编码的大部分都是编码的。下面是用于确定游戏是否已经获胜的代码..它不起作用,目前游戏结束的唯一方式是如果所有的点都被占用。有什么想法吗?

winners = [ [1,2,3,4],[2,3,4,5],[3,4,5,6],[4,5,6,7],[8,9,10,11],[9,10,11,12],[10,11,12,13],[11,12,13,14],
                    [15,16,17,18],[16,17,18,19],[17,18,19,20],[18,19,20,21],[22,23,24,25],[23,24,25,26],[24,25,26,27],
                    [25,26,27,28],[29,30,31,32],[30,31,32,33],[31,32,33,34],[32,33,34,35],[36,37,38,39],[37,38,39,40],
                    [38,39,40,41],[39,40,41,42],[1,8,15,22],[8,15,22,29],[15,22,29,36],[2,9,16,23],[9,16,23,30],[16,23,30,37],
                    [3,10,17,24],[10,17,24,31],[17,23,31,38],[4,11,18,25],[11,18,25,32],[18,24,32,39],[5,12,19,26],[12,19,26,33],
                    [19,25,33,40],[6,13,20,27],[13,20,27,34],[20,28,34,41],[7,14,21,28],[14,21,28,35],[21,28,35,42],[1,9,17,25],
                    [9,17,25,33],[17,25,33,41],[8,16,24,32],[16,24,32,40],[15,23,31,39],[2,10,18,26],[10,18,26,34],[18,26,34,42],
                    [3,11,19,27],[11,19,27,35],[4,12,20,28] ]

    def makeMove(self):
        self.Player = self.game.Player

        if self.game.Player == 'Black':
            self.b.config(image=self.Black, command=self.invalidMove)
            num = self.num
            self.game.moves['Black'].append(num)
            self.game.free.remove(num)
            w = self.won(self.game.Player)
            if self.game.moves['Black'] in winners:
                self.game.turninfo.config(text=self.game.Player+' WON!')
            elif self.game.free == [ ]:
                self.game.turninfo.config(text='Game is a draw!')
            else:
                self.game.Player = 'Red'
                self.game.turninfo.config(text=self.game.Player+"'s Turn")
        elif self.game.Player == 'Red':
            self.b.config(image=self.Red, command=self.invalidMove)
            num = self.num
            self.game.moves['Red'].append(num)
            self.game.free.remove(num)
            w = self.won(self.game.Player)
            if contains(self.game.moves['Red'],winners):
                self.game.turninfo.config(text=Player+' WON!')
                for c in self.game.cells:
                    c.freeze()
            elif self.game.free == [ ]:
                self.game.turninfo.config(text='Game is a draw!')
                for c in self.game.cells:
                    c.freeze()
            else:
                self.game.Player = 'Black'
                self.game.turninfo.config(text=self.game.Player+"'s Turn")

1 个答案:

答案 0 :(得分:1)

我认为你的问题是你正在检查一下球员是否完全匹配其中一个胜利条件。这是可能的,但通常情况下可能不会发生,例如玩家可能会将移动[2, 3, 1, 4]视为胜利,但[2, 3, 1, 4]不在winners中,因此您不会得到一个匹配。此外,如果玩家超过四次移动,你将永远不会因为同样的原因而获胜(因为在[6, 1, 2, 3, 4]中也找不到winners)。

你真正想知道的是,如果对于任何一个胜利条件,胜利条件的所有动作都是由玩家做出的。这可以这样表达:

for winner in winners:
    won = True
    for move in winner:
        if move not in self.game.moves['Black']:
            won = False
    if won:
        # game ending logic
        break

这可以大大缩短到以下几点(但可能更难以遵循):

if any(all(m in self.game.moves['Black'] for m in w) for w in winners)):
    # game ending logic