我已经创建了一个简单的战舰游戏(按照教程创建)。它可以完美地工作,但是现在我试图弄清楚如何使我的飞船占据多个空间并随机地垂直或水平放置。我想学习我需要做什么。谢谢您的时间!
from random import randint
board = []
for i in range(0,5):
board.append(["0"]*5)
def print_board(board):
for i in board:
print (" ".join(i))
print_board(board)
def random_row(board):
return randint(0,len(board)-1)
def random_col(board):
return randint(0,len(board)-1)
ship_row = random_row(board)
ship_col = random_col(board)
print(ship_row)
print(ship_col)
for turn in range(4):
print("Turn", turn + 1)
guess_row = int(input("Guess Row: "))
guess_col = int(input("Guess Column: "))
if guess_row == ship_row and guess_col == ship_col:
print("You sunk my battleship!")
break
else:
if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
print("That's not even in the ocean!")
elif(board[guess_row][guess_col] == "X"):
print("You guess that already.")
else:
print("You missed!")
board[guess_row][guess_col] = "X"
if turn == 3:
print("GAME OVER")
break
print_board(board)