我正在努力让游戏对用户想要的船只数量进行输入,并放置那么多船只。我已将坐标放入列表中,这就是我存储它们的方式,并检查它是否是命中。但是这些船只相互叠加,并且使用这种列表方法,我不知道如何首先检查它们是否重叠,其次,更改它们以使它们不重叠。
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 :(得分:1)
您的代码的一些建议:
您可以使用这样的numpy数组为播放器板建模:
一旦他们不再是2,就意味着有一个胜利者。 使用此系统,您可以告诉玩家他是否在同一地点拍摄两次,您还可以生成显示。
此外,要随机定位船舶,您可以使用此程序:
现在我们必须查看它是放置在水平位置还是垂直位置。 如果它的位置如下:
5 * 5板的示例,水平船的位置选择(1,0)
00000
22220
00000
00000
00000
然后,该解决方案不会阻止其他人使用该船。一个简单的解决方案是使用此循环:
while True:
if all ship placed:
break
pick number
Try to place the ship. (with an if statement, check if one of the spot your gonna change is already a 2.)
只要可以将所有船放在电路板上,该解决方案就应该有效。如果不可能,那么你的循环就不会结束。此外,如果船的数量不受董事会规模的限制,放置它们可能会很长。
然后,您可以使用更复杂的方案继续改进方法。另一个解决方案是在每艘船定位后检查你还有位置。