日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

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

發(fā)布時(shí)間:2024/1/8 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 战舰_Python战舰:获取用户输入的他们想要多少艘战舰 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

我試圖讓游戲采取一個(gè)用戶想要多少船的輸入,并放置多少船。我把坐標(biāo)放在一個(gè)列表中,這就是我存儲(chǔ)它們的方式,并檢查它是否命中。但是這些船被放在彼此的上方,用這個(gè)列表方法,我不知道如何首先檢查它們是否重疊,其次,改變它們,使它們不重疊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

有什么想法嗎?非常感謝:)

總結(jié)

以上是生活随笔為你收集整理的python 战舰_Python战舰:获取用户输入的他们想要多少艘战舰的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。