繁体   English   中英

连接四:要求输入而不打印

[英]connect four: asking for inputs without printing

我正在制作四联游戏。 我正在尝试设置游戏的简单可玩性。 我没有收到错误,但是当我希望它打印出电路板时,它的确将我发送到输入中。 当我在该列中键入数字时,它不会显示任何内容,它只为我提供我应填写的另一项输入,此输入将一直持续到我退出程序为止。.我正在尝试远离类,因为我没有非常擅长对这些进行编程。 我想知道为什么会这样,如何解决它,或者我是否只需要重新编程整个程序。

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

在您的游戏循环中尝试此操作-希望它也可以帮助您解决赢球检查:

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

我注释掉了赢钱支票,因为它还不起作用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM