连接四局(Python)的获胜条件?

时间:2018-11-16 15:46:18

标签: python

我一直在研究四连体游戏(开始是程序员)已经有几个星期了,我几乎完成了。但是我唯一的问题是我似乎无法获得胜利条件。老实说,我不知道如何解决它,所以我希望有人在这里可以看到出了什么问题。直到最后一个注释(#)的上部都工作正常,它打印了电路板,在错误的输入下给出了错误消息,并将块放到所需的位置。唯一仍然不起作用的是获胜条件。我无法完成游戏,它将使我连续达到7,并且没有给出获胜条件。我的代码可以在下面找到!

ROW_COUNT = 6                               #CREEEREN VAN HET BORD
COLUMN_COUNT = 7

Board = []
for x in range(ROW_COUNT): Board.append(list([0] * COLUMN_COUNT))

def drop_piece(Board, row, Column, piece):  #PLAATSEN VAN EEN RONDJE
    Board[row][Column] = piece

def is_valid_location(Board, Column):       #CHECKEN OF DE PLEK OP HET BORD LEEG IS
    return Board[-1][Column] == 0

def get_next_open_row(Board, Column):       #CHECKEN OF DE ROW LEEG IS
    for r in range(ROW_COUNT):
        if Board[r][Column]==0:
            return r

gameOver = False
turn = True

while not gameOver:
    if turn: player = 1
    else: player = 2
    UserInput = input("Player " + str(player) + ", Make your turn(0-6):")
    if UserInput.isdigit():
        Column = int(UserInput)
    else:
        print("Your input has to be between 0 and 6!")
        UserInput = input("Player " + str(player) + ", Make your turn(0-6):")

    if Column != 0 and Column != 1 and Column != 2 and Column != 3 and Column != 4 and Column != 5 and Column != 6:
       print("Your input has to be between 0 and 6!")
       UserInput = input("Player " + str(player) + ", Make your turn(0-6):")
    else:
        if is_valid_location(Board, Column):
            row = get_next_open_row(Board, Column)
            drop_piece(Board, row, Column, player)
            turn = not turn
        else: print("Invalid selection")

        for row in reversed(Board):
            print(row)                  # EVERYTHING UNTIL HERE WORKS FINE!

#HORIZONTAAL
for C in range(COLUMN_COUNT - 3):
    for R in range(ROW_COUNT):
        if Board[R][C] == 1 and Board[R][C + 1] == 1 and Board[R][C + 2] == 1 and Board[R][C + 3] == 1:
            print("You've won")
#VERTICAAL
for C in range(COLUMN_COUNT):
    for R in range(ROW_COUNT - 3):
        if Board[R][C] == 1 and Board[R + 1][C] == 1 and Board[R + 2][C] == 1 and Board[R + 3][C] == 1:
            print("You've won")
#DIAGONAAL
for C in range(COLUMN_COUNT - 3):
    for R in range(ROW_COUNT - 3):
        if Board[R][C] == 1 and Board[R + 1][C + 1] == 1 and Board[R + 2][C + 2] == 1 and Board[R + 3][C + 3] == 1:
            print ("You've won")

2 个答案:

答案 0 :(得分:0)

根据您的缩进,直到while循环结束,才检查那些检查。缩进所有三个检查,使它们位于while循环中。此外,您的支票需要将gameOver设置为True,以便当您获胜时游戏才能真正结束。

答案 1 :(得分:0)

只是提醒您:对于向后的对角线,您还需要一个获胜条件。

很久以前就在这里提出我的解决方案,看看能做什么。

 for x in range(len(Board)):
        for y in range(len(Board[x])):
            for turn in ['r', 'b']:
                try:
                    # Sideways line checking
                    if Board[x][y] == turn and Board[x][y+1] == turn and Board[x][y+2] == turn and Board[x][y+3]: pass
                except:# In case of a RangeError, meaning it checks for Board off the edge of the board
                    pass
                else:
                    if Board[x][y] == turn and Board[x][y+1] == turn and Board[x][y+2] == turn and Board[x][y+3] == turn:
                        winner = turn
                        gamestate = "Win"
                try: # Diagonal-right check
                    if Board[x][y] == turn and Board[x+1][y+1] == turn and Board[x+2][y+2] == turn and Board[x+3][y+3]: pass
                except:
                    pass
                else:
                    if Board[x][y] == turn and Board[x+1][y+1] == turn and Board[x+2][y+2] == turn and Board[x+3][y+3] == turn:
                        winner = turn
                        gamestate = "Win"
                try: #Vertical Check
                    if Board[x][y] == turn and Board[x+1][y] == turn and Board[x+2][y] == turn and Board[x+3][y]: pass
                except:
                    pass
                else:
                    if Board[x][y] == turn and Board[x+1][y] == turn and Board[x+2][y] == turn and Board[x+3][y] == turn:
                        winner = turn
                        gamestate = "Win"
                try: # Diagonal-left check
                    if Board[x][y] == turn and Board[x-1][y+1] == turn and Board[x-2][y+2] == turn and Board[x-3][y+3]: pass
                except:
                    pass
                else:
                    if Board[x][y] == turn and Board[x-1][y+1] == turn and Board[x-2][y+2] == turn and Board[x-3][y+3] == turn:
                        winner = turn
                        gamestate = "Win"
    if 'e' not in Board[0] and gamestate != "Win": # Checks for Tie (if there is any emty spaces in the top row after checking for a win)