我一直在开发一个实现connect 4游戏的程序,但遇到了障碍。这是我的输出:
>>> ================================ RESTART ================================
>>>
Player 1 please pick a column: 5
-----------------------------
| 0 | 1 | 2 | 3 | 4 | 5 | 6 |
-----------------------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | x | |
-----------------------------
Player 2 please pick a column: 6
-----------------------------
| 0 | 1 | 2 | 3 | 4 | 5 | 6 |
-----------------------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | o |
-----------------------------
所以基本上板子在交替轮流时不会“更新”基本上我的代码最终应该看起来像这样:
-----------------------------
| 0 | 1 | 2 | 3 | 4 | 5 | 6 |
-----------------------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | x | o |
-----------------------------
我不太确定我必须做什么才能让董事会正确更新。我试过弄乱打印功能,但输出最终变得比以前更糟。这是我的代码:
从播放器导入 *
def play_game(board, player1,player2):
b = ConnectFour()
f = Human(1)
g = Human(2)
while True:
f.play_turn(1)
if b.is_game_over() == None:
g.play_turn(2)
if b.is_game_over() == None:
pass
else:
print "Player 2 wins"
break
else:
print "Player 1 wins"
break
基本上:玩家 1 玩,然后我们检查是否有赢家,如果没有玩家 2 玩,依此类推。这调用了一个单独的类:
class Human(Player):
def play_turn(self,board):
super(Human, self).play_turn(board)
b = ConnectFour()
x = raw_input("Player %s please pick a column: " % self.playernum)
b.play_turn(self.playernum, int(x))
b.print_board()
任何想法、意见或建议将不胜感激!