连接4喜欢游戏

时间:2015-09-24 21:02:52

标签: python-3.x matrix

我在python中创建了一个类似连接的游戏,到目前为止,我正在创建电路板并对计算机实现游戏。我遇到的问题是不允许我玩过第二行。有什么想法吗?

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

1 个答案:

答案 0 :(得分:0)

我相信我弄清楚发生了什么。你的x总是初始化为len(board)-1然后如果空间被占用则减少1。如果你制作一个(> 2)x(> 2)板并且第0行被占用,那么这将总是强制x = 2.

这接近正确,但您实际需要的是迭代所有行,直到找到第一个未占用的行。我创建了一个循环,它将找到第一行并将其分配给x的值。我测试了它,我现在能够通过2行。

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