Python 我的战舰巨蟒
我正在尝试制作自己版本的codecademy战舰 这是到目前为止我的代码`Python 我的战舰巨蟒,python,Python,我正在尝试制作自己版本的codecademy战舰 这是到目前为止我的代码` board = [] for x in range(0, 5): board.append(["O"] * 5) def print_board(board): for row in board: print " ".join(row) def random_row(board): return randint(0, len(board) - 1) def random_c
board = []
for x in range(0, 5):
board.append(["O"] * 5)
def print_board(board):
for row in board:
print " ".join(row)
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)
running = True
while running:
print_board(board)
guess_row = int(raw_input("Guess Row:")) - 1 #-1 in order to make rows numbered from 1 to 5 for the user
guess_col = int(raw_input("Guess Col:")) - 1 #-1 in order to make cols numbered from 1 to 5 for the user
if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
print "Error: index out of bounds"
elif guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sank my battleship!"
running = False
else:
print "You missed my battleship!"
board[guess_row][guess_col] = "X"`
board=[]
对于范围(0,5)内的x:
董事会追加([“O”]*5)
def打印板(板):
对于板中的行:
打印“”连接(行)
def随机_行(板):
返回randint(0,len(板)-1)
def随机_柱(板):
返回randint(0,len(板[0])-1)
船行=随机船行(板)
船列=随机列(船列)
运行=真
运行时:
印刷电路板
guess_row=int(原始输入(“guess row:”)-1#-1,以便为用户将行编号为1到5
guess_col=int(原始输入(“guess col:”)-1#-1,以便为用户将col编号为1到5
如果(猜测行<0或猜测行>4)或(猜测列<0或猜测列>4):
打印“错误:索引超出范围”
elif guess_row==ship_row和guess_col==ship_col:
打印“祝贺你!你击沉了我的战舰!”
运行=错误
其他:
打印“你错过了我的战舰!”
棋盘[猜一排][猜一列]=“X”`
程序运行良好。我只是想制造多艘不同长度的船,并且有两个不同的板,这样两个玩家就可以玩和猜对方的船。但是让我们一次做一件事
如果我想拥有多艘长度为2或3的船只,代表这些船只的好策略是什么?我的想法是使用一个列表来表示船舶在网格上的坐标,但每个点都有一个x和一个y坐标。那么一艘长度为3的船是一个长度为3的列表,由3个长度为2的列表组成,每个列表都有一个x和y坐标?这就是我感到困惑的地方。在战舰中,每个玩家都有两块板:一块用来放置他的战舰,另一块用来跟踪敌人的射击 要存储船舶,只需将代码放入整数网格中:
0 1 1 1 1 0
0 2 0 0 0 0
0 2 0 0 0 0
0 2 0 3 3 0
0 0 0 0 0 0