繁体   English   中英

移动功能,用于将四个位置的移动放在整列上,而不是仅在最低位置上移动

[英]Move function for connect four places the move on an entire column instead of only the lowest possible spot

我正在用python制作四连冠游戏,而我正在制作允许玩家移动的功能。 这是我到目前为止的代码。

#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 false for now. will return 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):
    return False

#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():
    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(myBoardTemp)
    printBoard(myBoard)
    check3 = True
    while not checkWin(myBoard, 0) and not isDraw(myBoard):
        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)
main()

这会发生,而不是正常工作。

Welcome to Connect Four!
Please enter a number of rows: 6
Please enter a number of columns: 20
____________________
____________________
____________________
____________________
____________________
____________________
Please enter a move: 2
_X__________________
_X__________________
_X__________________
_X__________________
_X__________________
_X__________________
Please enter a move: 3
_X0_________________
_X0_________________
_X0_________________
_X0_________________
_X0_________________
_X0_________________
Please enter a move: 

这与我制作董事会的方式有关吗? 每行都“指向”一行吗? 我该如何解决?

在这里,在您的设置中:

for i in range(int(rows)):
    myBoard.append(myBoardTemp)

您已将同一列表多次添加到您的董事会。 这意味着,如果您更改该列表,则面板中的每一行都会更改。

而是为网格中的每一行添加一个不同的列表。

for i in range(int(rows)):
    myBoard.append([0]*int(columns))

或者,更简洁地说:

rows = int(rows)
columns = int(columns)
myBoard = [ [0]*columns for _ in range(rows) ]

暂无
暂无

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

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