[英]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.