首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >连接4类游戏

连接4类游戏
EN

Stack Overflow用户
提问于 2015-09-24 21:02:52
回答 1查看 246关注 0票数 2

我正在制作一个连接4,就像python中的游戏一样,到目前为止,我所拥有的是创建板,并在计算机上实现游戏游戏。我遇到的问题不允许我玩过第二排。对原因有什么想法吗?

代码语言:javascript
复制
#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))
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-24 22:40:03

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

这几乎是正确的,但实际上需要的是遍历所有行,直到找到第一个未占用的行为止。我创建了一个循环,它将找到第一行,并将其赋值为x。我对其进行了测试,现在可以播放已传递的2行。

代码语言:javascript
复制
#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))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32770863

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档