python 战舰_Python战舰:获取用户输入的他们想要多少艘战舰

我试图让游戏采取一个用户想要多少船的输入,并放置多少船。我把坐标放在一个列表中,这就是我存储它们的方式,并检查它是否命中。但是这些船被放在彼此的上方,用这个列表方法,我不知道如何首先检查它们是否重叠,其次,改变它们,使它们不重叠from random import randint

print('Welcome to Battleships for 1 player! Please be careful with entries, as if you get it wrong, you will still lose a go!')

print('Good luck!')

print('')

no_of_goes = int(input("How many goes would you like: "))

size_of_board = int(input("And how big would you like the board to be: "))

if size_of_board > 56:

print("That board will be too big to fit on the screen (max 56)")

size_of_board = int(input("And choose a more sensible number: "))

no_of_ships = int(input("And finally, how many ships would you like?: "))

board = []

# printing out the board

for x in range(size_of_board):

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

def print_board(board):

for row in board:

print (" ".join(row))

print_board(board)

# the lists that will become the locations

ship_rows = []

ship_cols = []

# generating random locations

def random_row(board):

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

def random_col(board):

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

# adding locations to lists

ships = list(range(no_of_ships))

for i in ships:

row = random_row(board)

col = random_col(board)

ship_rows.append(row)

ship_cols.append(col)

##

## And this is my attempt (didn't work)

##

for row in ship_cols:

if row == ship_cols and col == ship_cols:

ship_rows[-1] = random_row(board)

ship_cols[-1] = random_col(board)

# allowing to see where ships are and logging how many ships have been hit

print(ship_rows)

print(ship_cols)

ship_count = [1]

## I couldn't find a way of ending the game once the ships were sunk, so I stuck in a recursive function (sorry)

def printing_stars():

print('You have won!! ' +'*' * 56)

printing_stars()

for turn in range(no_of_goes):

# asking for their guesses

print('Turn ' + str(turn + 1) + ' out of ' + str(no_of_goes))

guess_col = int(input("Guess Col:")) - 1

guess_row = int(input("Guess Row:")) - 1

for item in ship_rows:

# If they hit, it gives a '!'

if len(ship_count) == no_of_ships and guess_row == item and guess_col == ship_cols[ship_rows.index(item)]:

print("You have won!!!! Congratulations")

board [guess_row][guess_col] = '!'

print_board(board)

printing_stars()

elif guess_row == item and guess_col == ship_cols[ship_rows.index(item)]:

print ("Congratulations! You sunk one of my battleships")

board [guess_row][guess_col] = '!'

print_board(board)

ship_count.append(1)

break

else:

# all misses

if (guess_row < 0 or guess_row > size_of_board - 1) or (guess_col < 0 or guess_col > size_of_board - 1):

print ("Oops, that's not even in the ocean.")

elif board[guess_row][guess_col] == "X" or board[guess_row][guess_col] == "!":

print ("You guessed that one already.")

turn -= 1

else:

print ("You missed my battleship!")

board[guess_row][guess_col] = "X"

print_board(board)

if turn == (no_of_goes - 1):

print('Game Over')

break

有什么想法吗?非常感谢:)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值