Python连四游戏获胜函数
除了 win 函数和重力之外,我已经对 Connect Four 的大部分 Python 版本进行了编码。下面是确定游戏是否已经获胜的代码。它不起作用,目前游戏结束的唯一方法是所有名额都被占据。有什么想法吗?
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")
I have most of my Python version of Connect Four coded, except for the win function and the gravity. Below is the code for determining if the game has been won yet.. It doesn't function, currently the only way the game ends is if all the spots are taken up. Any ideas?
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")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你的问题是你正在检查玩家的移动是否完全符合获胜条件之一。这是可能的,但可能不会经常发生,例如玩家可能会采取
[2, 3, 1, 4]
的动作,这应该算作胜利,但是[2, 3, 1, 4]
不在获胜者
中,因此您不会获得匹配。此外,如果玩家的步数超过四步,出于同样的原因,您将永远不会获得胜利(因为[6, 1, 2, 3, 4]
在中找不到>获胜者
要么)。您真正想知道的是,对于任何获胜条件,获胜条件的所有动作是否都是由玩家做出的。这可以这样表达:
这可以大大缩短为以下内容(但可能更难理解):
I think your issue is that you are checking to see if a players moves match one of the win conditions exactly. This is possible but probably won't happen that often, for example a player might make the moves
[2, 3, 1, 4]
which should count as a win, but[2, 3, 1, 4]
is not inwinners
so you will not get a match. Also, if it ever took a player more than four moves you would never get a winner for the same reason (since[6, 1, 2, 3, 4]
wouldn't be found inwinners
either).What you actually want to find out is if for any of the win conditions, all of the moves of the win condition were made by a player. This can be expressed like this:
This can be shortened considerably to the following (but it might be harder to follow):