为什么程序无法加载?

时间:2017-05-29 12:59:06

标签: python

我被告知需要在我的代码中加入一个main()函数来防止错误并确保程序的顺畅。但是在加载时,当第一块板出现时,我的游戏退出。

如果我遗漏了任何错误(未来潜在的错误),我想知道。

将来我还计划将这个游戏放在一个使用烧瓶的网站上,玩家使用箭头键选择他/她想要击中的位置,然后再按空格键进行操作。如果有人能给我一个关于如何开始这个的想法,我会非常感激。

提前感谢您的帮助。

该程序是基于控制台的2人战舰舰战游戏:

from random import randint

game_board = []
player_one = {
    "name": "Player 1",
    "wins": 0,
    "lose": 0
}
player_two = {
    "name": "Player 2",
    "wins": 0,
    "lose": 0
}
total_turns = 0

# Building our 5 x 5 board
def build_game_board(board):
    for item in range(5):
        board.append(["O"] * 5)

def show_board(board):
    print("Find and sink the ship!")
    for row in board:
        print(" ".join(row))

# Defining ships locations
def load_game(board):
    print("WELCOME TO BATTLESHIP!")
    print("START")
    del board[:]
    build_game_board(board)
    show_board(board)
    ship_col = randint(1, len(board))
    ship_row = randint(1, len(board[0]))
    return {
        'ship_col': ship_col,
        'ship_row': ship_row,
    }

ship_points = load_game(game_board)

# Players will alternate turns.
def player_turns():
    if total_turns % 2 == 0:
        return player_two

    else:
        return player_one

# Allows new game to start
def play_again():
    answer = input("Would you like to play again?")
    if answer == "yes" or answer == "y":
        total_turns = 0
        ship_points = load_game(game_board)

    else:
        print("Thanks for playing!")
        exit()

# What will be done with players guesses
def input_check(ship_row, ship_col, player, board):
    guess_col = 0
    guess_row = 0
    while True:
        try:
            guess_row = int(input("Guess Row:")) - 1
            guess_col = int(input("Guess Col:")) - 1
        except ValueError:
            print("Enter a number only.")
            continue
        else:
            break
    match = guess_row == ship_row - 1 and guess_col == ship_col - 1
    not_on_game_board = (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4)

    if match:
        player["wins"] += 1
        print("Congratulations! You sunk my battleship!")
        print("Thanks for playing!")
        play_again()

    elif not match:
        if not_on_game_board:
            print("Oops, that's not even in the ocean.")

        elif board[guess_row][guess_col] == "X":
            print("You guessed that one already.")

        else:
            print("You missed my battleship!")
            board[guess_row][guess_col] = "X"
        show_board(game_board)

    else:
        return 0


def main():
    begin = input('Type \'start\' to begin.')
    while (begin != str('start')):
        begin = input('Type \'start\' to begin.')
    for games in range(3):
        for turns in range(6):
            total_turns += 1
            if player_turns() == player_one:
                print("Player One")
                input_check(
                    ship_points['ship_row'],
                    ship_points['ship_col'],
                    player_one, game_board
                )

            elif player_turns() == player_two:
                print("Player Two")
                input_check(
                    ship_points['ship_row'],
                    ship_points['ship_col'],
                    player_two, game_board
                )

            else:
                continue

            if total_turns == 6:
                print("The game is a draw")
                play_again()

            else:
                continue

if __name__ == "main":
    main()

1 个答案:

答案 0 :(得分:3)

更改为以下内容:

if __name__ == "__main__":