Python 3.x 连接4喜欢的游戏

Python 3.x 连接4喜欢的游戏,python-3.x,matrix,Python 3.x,Matrix,我正在用python制作一个类似connect-four的游戏,到目前为止,我所做的是创建棋盘,并在计算机上实现游戏。我遇到了不允许我打过第二排的问题。你知道为什么吗 #Define the board row = int(input("Please enter # of rows: ")) #User input Rows column = int(input("Please enter # of columns: ")) #User input Columns board = [] #em

我正在用python制作一个类似connect-four的游戏,到目前为止,我所做的是创建棋盘,并在计算机上实现游戏。我遇到了不允许我打过第二排的问题。你知道为什么吗

#Define the board
row = int(input("Please enter # of rows: ")) #User input Rows
column = int(input("Please enter # of columns: ")) #User input Columns

board = [] #empty board
space = ' '
p1 = 'x'
p2 = 'o'

#create board based on user input 
for i in range(0,row):
    board.append([])
    for j in range(0, column):
        board[i].append(space)

print(board)
#print board with character decorations 
for i in board:
    line=''
    print('+'+'-+'*len(i))
    for j in range(len(i)):
        line = line + '|'+i[j]
    line = line + '|'
    print(line)
print('+'+'-+'*len(i))

print(len(board))

while True:
    #User input column
    myC = int(input("Player 1, choose your column: "))

    i = len(board)-1
    x = len(board)-1
    while i >= 0:
        if board[x][myC] == space:
            i = i+1
            board[x][myC] = 'x'
            break
        elif board[x][myC] == p1 or p2:
            i = i+1
            x = x - 1
            print(x)
            board[x][myC] = 'x'
            break

    # print the board!
    for i in board:
        line=''
        print('+'+'-+'*len(i))
        for j in range(len(i)):
            line = line + '|'+i[j]
        line = line + '|'
        print(line)
    print('+'+'-+'*len(i))

    #Computer input column
    from random import randint
    theC = randint(0, len(board)-1)
    print("Computer's Turn: Column " , theC)

    i = len(board)-1
    x = len(board)-1
    while i >= 0:
        if board[x][theC] == space:
            board[x][theC] = 'o'
            i = i+1
            break
        elif board[x][theC] == p1 or p2:
            i = i+1
            x = x - 1
            print(x)
            board[x][theC] = 'o'
            break


    # print the board!
    for i in board:
        line=''
        print('+'+'-+'*len(i))
        for j in range(len(i)):
            line = line + '|'+i[j]
        line = line + '|'
        print(line)
    print('+'+'-+'*len(i))

我想我知道发生了什么。您的x总是初始化为len(board)-1,如果空间被占用,它将减少1。如果制作一个(>2)x(>2)板,并且第0行被占用,那么这将始终强制x=2

这几乎是正确的,但实际上需要遍历所有行,直到找到第一个未占用的行。我创建了一个循环,它将查找第一行并将其指定给x的值。我测试过了,现在我可以打两排了

#Define the board
row = int(input("Please enter # of rows: ")) #User input Rows
column = int(input("Please enter # of columns: ")) #User input Columns

board = [] #empty board
space = ' '
p1 = 'x'
p2 = 'o'

#create board based on user input 
for i in range(0,row):
    board.append([])
    for j in range(0, column):
        board[i].append(space)

print(board)

#print board with character decorations 
for i in board:
    line=''
    print('+'+'-+'*len(i))
    for j in range(len(i)):
        line = line + '|'+i[j]
    line = line + '|'
    print(line)
print('+'+'-+'*len(i))

print(len(board))

while True:
    #User input column
    myC = int(input("Player 1, choose your column: "))

    i = len(board)-1
    x = len(board)-1
    while i >= 0:
        if board[x][myC] == space:
            i = i+1
            board[x][myC] = 'x'
            break
        # Find last empty row for new piece
        elif board[x][myC] == p1 or p2:
            for j in range(x-1,-1,-1):
                if board[j][myC] == space:
                    x = j
                    break

            i = i+1
            #x = x - 1
            print(x)
            board[x][myC] = 'x'
            break

    # print the board!
    for i in board:
        line=''
        print('+'+'-+'*len(i))
        for j in range(len(i)):
            line = line + '|'+i[j]
        line = line + '|'
        print(line)
    print('+'+'-+'*len(i))

    #Computer input column
    from random import randint
    theC = randint(0, len(board)-1)
    print("Computer's Turn: Column " , theC)

    i = len(board)-1
    x = len(board)-1
    while i >= 0:
        if board[x][theC] == space:
            board[x][theC] = 'o'
            i = i+1
            break
        # Find last empty row for new piece
        elif board[x][theC] == p1 or p2:
            for j in range(x-1,-1,-1):
                if board[j][theC] == space:
                    x =j
                    break                
            i = i+1
            #x = x - 1
            print(x)
            board[x][theC] = 'o'
            break


    # print the board!
    for i in board:
        line=''
        print('+'+'-+'*len(i))
        for j in range(len(i)):
            line = line + '|'+i[j]
        line = line + '|'
        print(line)
    print('+'+'-+'*len(i))

非常感谢你,我真的很感谢你的帮助。