Ich mache gerade ein einfaches Battleships-Spiel mit Python 3, aber ich kann nicht scheinen, um das Brett zur Anzeige zu bekommen. Hier ist mein Code;Python Battleships Spiel
# Battleships
def main():
pass
if __name__ == '__main__':
main()
from random import randint
# this initialises the board
board = []
for x in range(5):
board.append(["O"] * 5)
def print_board(board):
for row in board:
print (" ".join(row))
# this starts the game and prints the board
print ("Let's play Battleship!")
print_board(board)
# defines the location of the ship
def random_row(board):
return randint(0, len(board) - 1)
def random_col(board):
return randint(0, len(board[0]) - 1)
ship_row = random_row(board)
ship_col = random_col(board)
# asks the player to make a guess
for turn in range(5):
guess_row = int(input("Guess Row:"))
guess_col = int(input("Guess Col:"))
# if the player guesses correctly, then the game ends cleanly
if guess_row == ship_row and guess_col == ship_col:
print ("Congratulations! You sunk my battleship!")
else:
# if the player guesses outside the board, then the following message appears
if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
print ("Oh dear, you've hit an island!")
# a warning if the guess has already been made by the player
elif(board[guess_row][guess_col] == "X"):
print ("That guess has already been made.")
# if the guess is wrong, then the relevant board place is marked with an X
else:
print ("You've missed my battleship!")
board[guess_row][guess_col] = "X"
# prints the turn and updates the board accordingly
print ("Turn " + str(turn+1) + " out of 5.")
print_board(board)
# if the user has had 5 guesses, it's game over
if turn >= 3:
print ("You sunk my battleship! We're gonna need a bigger boat.")
Das Spiel nimmt die Koordinaten, aber nichts mit dem Board zu tun, drucken oder wenn der Spieler macht eine wiederholte Vermutung oder eine, die außerhalb des Spielfeldes ist.
Jede Hilfe wäre sehr willkommen!
Sie haben einen Anruf 'print_board' innen' print_board'. Ich vermute, es ist ein Einrückungsproblem. – interjay
Es ist kein Einrückungsproblem. Das Programm hebt es nicht als ein Problem hervor. –
Es ist ein Problem, wenn das Programm sich anders als beabsichtigt verhält. – interjay