python 战舰_如何简化战舰计划?(Python)

请原谅我,我是编程新手,我知道这里面包含了很多不必要的代码。在

有没有一种方法可以在代码不被永久扩展的情况下添加更多的船只?

在while循环下编写比较代码也是一种更好的方法。感谢您抽出时间阅读和评论。在from random import randint

board = []

for x in range(10):

board.append(["O"] * 10)

# creating the "map" of "O"'s for an ocean - 10x10

def print_board(board):

for row in board:

print " ".join(row)

print "Let's play Battleship!"

print_board(board)

# random number for row from 0 to length of board (9) in this case

def random_row(board):

return randint(0, len(board) - 1)

# random number for column from 0 to length of board (9) in this case

def random_col(board):

return randint(0, len(board[0]) - 1)

# 1st ship, size = 1x1

ship_row = random_row(board)

ship_col = random_col(board)

# 2nd ship, size = 1x2

ship_row2 = random_row(board)

if ship_row2 == ship_row: #Did not want a ship coordinate to fall onto a previous ship

ship_row2 = random_row(board) #so I set it to generate a new number if it did

ship_row2_1 = ship_row2 - 1 #2nd part of this ship, needs to be adjacent so I set - 1 (could be +1)

if not ship_row2_1 >= 0: # if first part is 0, then ship_row2_1 would be off the "map",

ship_row2_1 += 2 #so I added 2 to bring it to a positive number

ship_col2 = random_col(board)#same things as above for the column code

if ship_col2 == ship_col:

ship_col2 = random_col(board)

ship_col2_1 = ship_col2

turn = 0

ships_sunk = 0

while True:

try:

guess_row = int(raw_input("Guess Row:"))

guess_col = int(raw_input("Guess Col:"))

# so my code here basically says if you hit the 1x1 ship it should turn it to an X,

#and not be able to hit it again.

if (guess_row == ship_row and guess_col == ship_col) and (board[guess_row][guess_col] != "X"):

print "\nCongratulations! You sunk a battleship!"

print "One more to go!\n"

board[guess_row][guess_col] = "X"

print_board(board)

ships_sunk += 1

if ships_sunk == 2:

print "\n You got them all! Pokemon!"

break

# here is the same but for the 1x2 ship, I had to turn them all to X to prevent user,

# from hitting the same part of the "ship".

elif ((guess_row == ship_row2 and guess_col == ship_col2) or \

(guess_row == ship_row2_1 and guess_col == ship_col2_1)) and (board[guess_row][guess_col] != "X"):

print "\nCongratulations! You sunk a battleship!"

print "One more to go!\n"

board[guess_row][guess_col] = "X"

board[ship_row2][ship_col2] = "X"

board[ship_row2_1][ship_col2_1] = "X"

print_board(board)

ships_sunk += 1

if ships_sunk == 2:

print "\n You got them all! Pokemon!"

break

else:

if (guess_row < 0 or guess_row > (len(board) -1 )) or (guess_col < 0 or guess_col > (len(board[0]) - 1)):

print "\nOops, that's not even in the ocean."

elif(board[guess_row][guess_col] == "X"):

print "\nYou guessed that one already."

elif type(guess_row) is not int or type(guess_col) is not int:

print "Type a number!"

else:

print "\nYou missed my battleship!"

board[guess_row][guess_col] = "X"

turn += 1

print_board(board)

if turn == 4:

print "Game Over"

break

except ValueError:

print "Please type a number!"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值