首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >连接四:要求输入而不打印

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

Stack Overflow用户
提问于 2016-04-18 16:06:17
回答 1查看 143关注 0票数 0

我正在做一个连接四种类型的游戏。我正在尝试设置游戏的简单可玩性。我没有发现错误,但是当我想让它打印出来的时候,它确实会把我输入到一个输入中。当我为该列输入一个数字时,它不会打印任何内容,它只会给我另一个我应该填写的输入,这会一直持续到我退出程序。考虑到我还不太擅长编程,我正试图远离课堂。我想知道为什么会发生这种情况,以及该做些什么来修复它,或者我是否只需要重新编写整个程序。

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

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-18 16:45:39

为您的播放循环尝试这个-希望它将帮助您修复胜利检查也:

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

我注释掉了胜利检查,因为它还不起作用

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36699079

复制
相关文章

相似问题

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