繁体   English   中英

连接四的win功能无法正常工作

[英]win function for connect four isn't working correctly

我试着做一个函数来检查是否有人赢了它是上下赢了,而不是对角线赢了。

它有时可以工作,但有时却说连续三局获胜。

这是我的代码。

#myBoard is a 2d array storing the board. col is the column a player is                                                                                                                                     
#trying to move, and player is the player to move. If it is a valid move,                                                                                                                                   
#the program will go ahead and change myBoard.                                                                                                                                                              
def move2(myBoard, col, player):
     if player == True:
         for i in range(len(myBoard) - 1,-1,-1):
             if myBoard[i][col] == 0:
                 myBoard[i][col] = 1
                 player = False
                 break
     else:
         for i in range(len(myBoard) - 1,-1,-1):
            if myBoard[i][col] == 0:
                myBoard[i][col] = -1
                player = True
                break
     return myBoard, player


#Returns 1 if player 1 has won, a -1 if player 2 has won, and 0 otherwise.                                                                                                                                  
#lastColPlayed is the last valid move that was made.                                                                                                                                                        
def checkWin(myBoard, lastColPlayed):
    player1amount = 0
    player2amount = 0
    for i in range(len(myBoard) - 1):
         if myBoard[i][lastColPlayed] == 1:
              player2amount = 0
              player1amount += 1
         if myBoard[i][lastColPlayed] == -1:
              playet1amount = 0
              player2amount += 1
    if player1amount == 3:
         return 1
    elif player2amount == 3:
         return -1
    else:
         return 0

#prints myBoard to the screen                                                                                                                                                                               
def printBoard(myBoard):
    for row in myBoard:
        for item in row:
            if item == 0:
                print("_", end="")
            elif item == -1:
                print("0", end="")
            elif item == 1:
                print("X", end="")
        print()

#returns true if it's a draw                                                                                                                                                                                
def isDraw(myBoard):
    return False

def main():
    won = 0
    draw = False
    player1turn = True
    print("Welcome to Connect Four!")
    rows = input("Please enter a number of rows: ")
 check = True
    while check == True:
        try:
            if int(rows) <= 5:
                while int(rows) <= 5:
                    rows = input("Please enter a Valid choice: ")
            else:
                check = False
        except ValueError:
            rows = input("Please enter a Valid choice: ")
    columns = input("Please enter a number of columns: ")
    check2 = True
    while check2 == True:
        try:
            if int(columns) <= 5:
                while int(columns) <= 5:
                    columns = input("Please enter a Valid choice: ")
            else:
                check2 = False
        except ValueError:
            columns = input("Please enter a Valid choice: ")
    myBoard = []
    myBoardTemp = []
    for i in range(int(columns)):
        myBoardTemp.append(0)
    for i in range(int(rows)):
        myBoard.append([0] * int(columns))
    printBoard(myBoard)
    check3 = True
while won == 0 and draw == False:
        move = input("Please enter a move: ")
        while check3 == True:
            try:
                if int(move) < 0 or int(move) > len(myBoard[0]):
                    while int(move) < 0 or int(move) > len(myBoard[0]):
                        move = input("Please enter a valid choice: ")
                else:
                    check3 = False
            except ValueError:
                move = input("Please enter a valid choice: ")
        myBoard, player1turn = move2(myBoard,int(move) - 1,player1turn)
        printBoard(myBoard)
        won = checkWin(myBoard,int(move) - 1)
        draw = isDraw(myBoard)
        if won == 1:
             print("Player 1 has won!")
        elif won == -1:
             print("Player 2 has won!")
        elif draw == True:
             print("It is a draw!")
main()

这次它起作用了

____________________
____________________
X___________________
X_0_________________
X_0_________________
X_0_________________
Player 1 has won!

这次没有

____________________
____________________
__XX________________
_X0X0_______________
_0XX0_______________
X0X00_______________
Player 1 has won! 

怎么了?

一些东西:

1)range(n)返回0到n-1之间的所有值,因此在checkWin键入:

for i in range(len(myBoard) - 1):

您实际上没有考虑底行。

2)您只连续检查3个。 第一个示例给出看似正确答案的唯一原因是因为它不考虑最底行。 在第二个示例中,您连续有三个X(不包括底行),因此这就是它错误地宣称胜利的原因。

所以答案是:

  • 从for循环中删除-1
  • 检查player1amountplayer2amount是否为4(或更佳:> 3)

暂无
暂无

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

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