【发布时间】:2021-01-14 00:04:04
【问题描述】:
我想写一个船舶游戏,玩家必须猜测计算机在棋盘上“隐藏”船舶的哪个位置。我的问题是,一切看起来都很好,程序编译没有错误,但我预定的功能不起作用,我不知道为什么。我觉得我的变量 board 在被函数 ship 处理后丢失了,之后我无法打印带有船的板。我尝试在 ship 函数的末尾使用 return bo ,但它也不起作用。 可能有什么问题? 我真的很迷茫,不知道这里可能有什么问题:(
import random
board = []
for i in range(0,10):
board.append([0])
for j in range(0,9):
board[i].append(0)
def board_print(bo):
for i in range(len(bo)):
if i == 0:
print("--------------------")
for j in range(len(bo[0])):
if j == 9:
print(bo[i][j])
else:
print(str(bo[i][j]) + " ", end="")
print("--------------------")
def ship_start():
pos_x = random.randint(0,9)
pos_y = random.randint(0,9)
return(pos_x,pos_y) # coordinates of ships first point
def ship(bo): # creates ship with 4 hp points
row , col = ship_start()
print(row, col)
coin_flip = 1 #random.randint(0,1) # 0 - horizontal, 1 - vertical
if coin_flip == 1:
i = 0
while 3 - i > 0:
if bo[row + i][col] in bo == True:
bo[row][col] = 1
i =i + 1
else:
j = 4 - i
for x in range(j):
bo[row - x][col] = 1
ship(board)
board_print(board)
【问题讨论】:
-
你在这里缩进了吗? -->
def board_print(bo): for i in range(len(bo)):
标签: python function variables return