移动功能连接四个位置移动整个列而不是只有最低点

时间:2015-04-12 07:07:54

标签: python

我正在使用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: 

这与我制作电路板的方式有关吗?是每一行&#34;指向&#34;一排?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

在此处,在您的设置中:

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