简体   繁体   English

为什么我的战舰游戏代码会一遍又一遍地运行?

[英]Why is my code for a battleship game running over and over again?

So I am trying to create a very simple 1-player battleship game in python.所以我试图在 python 中创建一个非常简单的单人战舰游戏。 Where the computer randomly places its ships in the rows and columns, and the player guesses which row and column a ship is in. My current problem is that my code does that, however it prints the board out each time and also seems to make the choices for the player as well.计算机随机将它的船放置在行和列中,玩家猜测船在哪一行和哪一列。我目前的问题是我的代码是这样做的,但是它每次都会打印出棋盘,而且似乎也使玩家的选择也是如此。

import random
#Make board
board = [
    [' ', ' ', ' ', ' ', ' '],
    [' ', ' ', ' ', ' ', ' '],
    [' ', ' ', ' ', ' ', ' '],
    [' ', ' ', ' ', ' ', ' '],
    [' ', ' ', ' ', ' ', ' '],
]

# List to refer to columns by letter, since that is how a typical battleship game is
#initiated
letters2Numbers = {
    'A': 0,
    'B': 1,
    'C': 2,
    'D': 3,
    'E': 4,
}


#Function to ask the user for their board position
def generateShips():
    for a in range (0,55):
        row = random.randrange(1,6)
        column = random.choice(['A','B','C','D','E'])
        rowNumber = row
        columnNumber = column

        return int(row) - 1, letters2Numbers[column]

def askUser():
    column = input("column (A to E):")
    while column not in "ABCDE":
        print("That column is wrong! It should be A, B, C, D or E")
        column = input("column (A to E):")

    row = input("row (1 to 5):")
    while row not in "12345":
        print("That row is wrong! it should be 1, 2, 3, 4 or 5")
        row = input("row (1 to 5):")
    return int(row) - 1, letters2Numbers[column]

def printBoard(board):
    # Shows the board, one row at a time, labelled
    print("  A B C D E")
    print(" +-+-+-+-+-+")
    rowNumber = 1
    for row in board:
        print("%d|%s|" % (rowNumber, "|".join(row)))
        print(" +-+-+-+-+-+")
        rowNumber = rowNumber + 1




# Now clear the screen, and the other player starts guessing
print("\n"*50)

guessesBoard = [
    [' ', ' ', ' ', ' ', ' '],
    [' ', ' ', ' ', ' ', ' '],
    [' ', ' ', ' ', ' ', ' '],
    [' ', ' ', ' ', ' ', ' '],
    [' ', ' ', ' ', ' ', ' '],
]


# Keep playing until we have 5 right guesses
guesses = 0
while guesses < 5:
    print("Guess a battleship location")
    rowNumber, columnNumber = askUser()

    if guessesBoard[rowNumber][columnNumber] != ' ':
        print("You have already guessed that place!")
        continue

    # Check that there are no repeats
    if board[rowNumber][columnNumber] == 'X':
        print("HIT!")
        guessesBoard[rowNumber][columnNumber] = 'X'
        guesses = guesses + 1
    else:
        guessesBoard[rowNumber][columnNumber] = '.'
        print("MISS!")

    printBoard(guessesBoard)
print("GAME OVER!")

This is my code so far.到目前为止,这是我的代码。 If you run it, you'll see the error I'm talking about.如果你运行它,你会看到我所说的错误。

You never ask the player for choices;你从不要求玩家选择; you print a message suggesting that you're about to do so, but then your code charges ahead and uses the row and column from ship generation.您打印一条消息,暗示您即将这样做,但随后您的代码提前收费并使用船舶生成的行和列。 Nowhere in your program do you accept user input.您的程序中没有任何地方接受用户输入。

You want to use function input() rather than random.SOMETHING.您想使用 function input() 而不是 random.SOMETHING。 Then your input will not be choosen by PC.那么您的输入将不会被 PC 选择。

Then to convert user input (which always returns a string) to a number, you can use something like:然后要将用户输入(始终返回字符串)转换为数字,您可以使用以下内容:

a = input("perhaps a number: ")
if not a.isdigit():
    raise ValueError("I said number :(")
a = int(a)

You probably don't want to raise the error, I am just demonstrating what you can build on.您可能不想引发错误,我只是在展示您可以构建的内容。

I am waiting for further development of this question.我正在等待这个问题的进一步发展。 :) :)

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

相关问题 如何在 GAME OVER 出现后重播游戏而不关闭程序并在 PYGAME 中再次运行它? - How to replay a game once GAME OVER comes without closing the program and running it again in PYGAME? 字典代码只是一遍又一遍地循环 - Dictionary code just loops over and over again 我的while循环重复一遍又一遍 - My while loop is repeating over and over again 我的游戏结束代码中的冲突在 pygame 中无法正常工作 - My collision in my code for game over is not working right in pygame Python 运行我的游戏时 True 循环未迭代 - Python while True loop not iterating over when running my game 为什么我没有在这个高低代码中收到游戏结束消息 - why am i not getting the game over message in this high low code 为什么我的猜谜游戏会跳过“较低”和“较高”部分 - why does my guessing game skip over the 'lower' and 'higher' parts 为什么此Python代码只是一遍又一遍地打印变量之一? - Why does this Python code just print one of the variables over and over again? 另一个循环问题:为什么我要一遍又一遍地将相同的内容添加到列表中? - Aparrent looping issue: Why am I appending the same thing to my list over and over again? 为什么我的一根弦在我尝试后一遍又一遍地重复 - Why does one of my strings get repeated over and over again after I try
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM