连接四:无需打印就要求输入

时间:2016-04-18 16:06:17

标签: python

我正在制作连接四种游戏。我试图建立游戏的简单可玩性。我没有收到错误,但是当我希望它打印出来时,它确实会让我输入一个输入。当我为列输入一个数字时,它不会打印任何内容,它只是给我另一个我应填写的输入,这一直持续到我退出程序..我试图远离类,因为我我还不是很擅长编程。我想知道为什么会发生这种情况,以及如何解决这个问题,或者我是否只需要对整个事情进行重新编程。

a = [" ", " ", " ", " ", " ", " ", " "]
b = [" ", " ", " ", " ", " ", " ", " "]
c = [" ", " ", " ", " ", " ", " ", " "]
d = [" ", " ", " ", " ", " ", " ", " "]
e = [" ", " ", " ", " ", " ", " ", " "]
f = [" ", " ", " ", " ", " ", " ", " "]
board = [a, b, c, d, e, f] # sets up the board
print("What is player 1's name?")
player1 = input()
print("What is player 2's name?")
player2 = input()
plays = 0


def print_board(): # prints the board
    p = 0
    for x in board:
        for i in x:
            print(i, end=" | ")
        print()
        p += 1
        if p != 6:
            print("- "*15)
        else:
            print()


def win(): # defines a boolean if one player has won
    i = 0
    k = 0
    while i != 5:
        while k != 6:
            if board[i][k] == "o" or board[i][k] == "x":
                if board[i+1][k] == board[i][k] == board[i+2][k] == board[i+3][k]:
                    return False
                elif board[i][k] == board[i][k+1] == board[i][k+2] == board[i][k+3]:
                    return False
                elif board[i][k] == board[i+1][k+1] == board[i+2][k+2] == board[i+3][k+3]:
                    return False
                elif board[i][k] == board[i-1][k-1] == board[i-2][k-2] == board[i-3][k-3]:
                    return False
            else:
                return True


def play(): # defines the part you play.
    if plays % 2 == 0:
        player = "o"
    else:
        player = "x"
    print_board()
    x = int(input("Where would you like to put your chip?"))
    i = 0
    while i < 5:
        if board[i][x] == " ":
            if board[i+1][x] == "x" or board[i+1][x] == "o":
                board[i][x] = player
    print_board()
    if win():
        print(player+" won!")
    play()

play() # runs the script

1 个答案:

答案 0 :(得分:0)

尝试使用此游戏循环 - 希望它也可以帮助您修复胜利检查:

def play(): # defines the part you play.
    plays = 0
    while True:
        if plays % 2 == 0:
            player = "o"
        else:
            player = "x"
        x = int(input("Where would you like to put your chip?"))
        i = 0
        for i in range(5):
            if board[i][x] == " ":
                if board[i+1][x] == "x" or board[i+1][x] == "o":
                    board[i][x] = player
        else:
            board[5][x] = player

        print_board()
        #if win():
        #    print(player+" won!")
        #    break
        plays += 1

print_board()

play() # runs the script

我评论了胜利检查,因为它还没有工作