简体   繁体   English

Python:连接四轮交替

[英]Python: Connect Four Alternating turns

I've been working on a program that implements the game of connect 4 and I've hit a snag. 我一直在研究实现Connect 4游戏的程序,但遇到了麻烦。 Here's my output: 这是我的输出:

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

So basically the board isn't "updating" when alternating turns Essentially my code should end up looking like this: 因此,基本上,董事会在轮流交替时不会“更新”,基本上我的代码应该最终看起来像这样:

-----------------------------
| 0 | 1 | 2 | 3 | 4 | 5 | 6 |
-----------------------------
|   |   |   |   |   |   |   |
|   |   |   |   |   |   |   |
|   |   |   |   |   |   |   |
|   |   |   |   |   |   |   |
|   |   |   |   |   |   |   |
|   |   |   |   |   | x | o |
-----------------------------

I'm not too sure what I have to do to get the board to update properly. 我不太确定该如何做才能正确更新主板。 I've tried messing around with the print functions but the output ends up getting worse than before. 我尝试弄乱打印功能,但是输出结果变得比以前更糟。 Here is my code: 这是我的代码:

from player import * 从玩家导入*

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

Basically: Player 1 plays, then we check if there's a winner if not player two plays, and so on. 基本上:玩家1玩,然后我们检查是否有赢家(如果不是玩家2玩),依此类推。 And this calls a separate class: 这将调用一个单独的类:

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

Any ideas, input or advice would be greatly appreciated! 任何想法,意见或建议将不胜感激!

The problem is that, although you're passing board objects around, you ignore them and just create new ones everywhere. 问题是,尽管您要传递board对象,但是您会忽略它们,而只是在各处创建新的对象。

First, look at Human.play_turn : 首先,查看Human.play_turn

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

This passes board to the super method Player.play_turn , but then it creates a new board b = ConnectFour() , and everything it does is to that board, not the original one. 这会将board传递给超级方法Player.play_turn ,但是随后它将创建一个新板b = ConnectFour() ,并且它所做的一切都是针对板,而不是原始板。

Likewise, in play_game , you take a board , and a player1 and a player2 , but do nothing with them, and instead create new ones called b , f , and g . 同样,在play_game ,您拿了一个board ,一个player1和一个player2 ,但不对其进行任何操作,而是创建了新的bfg

So, what you want is something like this: 所以,您想要的是这样的:

def play_game(board, player1,player2):
    while True:
        player1.play_turn(board)
        if board.is_game_over() == None:
            player2.play_turn(board)
            if board.is_game_over() == None:
                pass
            else:
                print "Player 2 wins"
                break
        else:
            print "Player 1 wins"
            break

class Human(Player):
    def play_turn(self,board):
        super(Human, self).play_turn(board)
        x = raw_input("Player %s please pick a column: " % self.playernum)
        board.play_turn(self.playernum, int(x))
        board.print_board()

That should fix your immediate problems—but I'm guessing you have very similar problems in all of your other functions. 那应该可以解决您眼前的问题,但是我想您在所有其他功能中都有非常相似的问题。

Meanwhile, there are some odd things about your design. 同时,您的设计有些奇怪。

For example, why are you checking if b.is_game_over() == None: ? 例如,为什么要检查if b.is_game_over() == None: A function called is_game_over should probably be returning something truthy if the game is over, and something falsey if it's not. 如果游戏结束,则称为is_game_over的函数应该返回真实值,否则返回假值。 While None is a reasonable "something falsey", False is even more reasonable—and, either way, it's hard to imagine why you'd want to check for it explicitly, instead of just checking that it's falsey. 尽管“ None是一个合理的“虚假信息”,但“ False则更为合理,而且无论哪种方式,都很难想象您为什么要显式地检查它,而不仅仅是检查它是否是虚假的。 (Plus, even if you do need to check specifically for None for some reason, there's almost never a good reason to use == None instead of is None .) (此外,即使出于某些原因确实需要专门检查None ,也几乎没有理由使用== None而不是is None 。)

Second, why do you need to create board , player1 , and player2 outside of the play_game function? 其次,为什么需要在play_game函数之外创建boardplayer1player2 It doesn't seem like they'd be all that useful outside this function. 在此功能之外,它们似乎并没有那么有用。

Finally, even with just two players, I think your code would be simpler if you refactored the common parts: 最后,即使只有两个参与者,我认为如果重构公共部分,您的代码也会更简单:

def play_game(board, player1,player2):
    while True:
        for player in player1, player2:
            player.play_turn(board)
            if board.is_game_over():
                print "Player {} wins".format(player.playernum)
                break

One last thing: Your logic doesn't seem to account for the possibility that the game might be over because the current player has no move to make. 最后一件事:您的逻辑似乎没有考虑到游戏可能结束的可能性,因为当前玩家没有行动要走。 I'm not sure if that's a tie or a loss in Connect 4, but I'm pretty sure it's not a win, is it? 我不确定在Connect 4中是平局还是输局,但我很确定这不是胜利,是吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM