我正在用Python做一个战舰游戏。在
我想不出怎么使飞船比一个空间长。在
这是我的代码:
x = 0
def game():
while x == 0:
from random import randint
board = []
row_num = 1
hit = 0
for i in range(1,11):
board.append(10*["O"])
row_num += 1
def print_board(board):
for row in board:
print(" ".join(row))
print("Let's Play Battleship!")
print("Turn 1")
print_board(board)
def random_row(board):
return randint(0, len(board) - 1)
def random_col(board):
return randint(0, len(board) - 1)
ship1_row = random_row(board)
ship1_col = random_col(board)
ship2_row = random_row(board)
ship2_col = random_col(board)
ship3_row = random_row(board)
ship3_col = random_col(board)
def check_ship2():
if ship2_row == ship1_row and ship2_col == ship1_col:
ship2_row = random_row(board)
ship2_col = random_col(board)
return ship2_row
return ship2_col
check_ship2()
def check_ship3():
if ship3_row == ship1_row and ship3_col == ship1_col:
ship3_row = random_row(board)
ship3_col = random_col(board)
return ship3_row
return ship3_col
check_ship3()
elif ship3_row == ship2_row and ship3_col == ship2_col:
ship3_row = random_row(board)
ship3_col = random_col(board)
return ship3_row
return ship3_col
check_ship3()
def restart():
again = input("Do You want to play again?(Y/N)")
if again == "Y" or again == "y":
print("\n" * 80)
game()
elif again == "N" or again == "n":
print("Thanks for playing!")
exit()
else:
print("Sorry that wasn't Y or N, try again")
restart()
for turn in range(1,16):
guess_row = int(input("Guess Row with a number 0-9:"))
guess_col = int(input("Guess Col with a number 0-9:"))
print("\n" * 80)
if (guess_row == ship1_row and guess_col == ship1_col) or (guess_row == ship2_row and guess_col == ship2_col) or (guess_row == ship3_row and guess_col == ship3_col):
board[guess_row][guess_col] = "S"
print("Congratulations! You sank my battleship!")
hit += 1
else:
if (guess_row < 0 or guess_row > 9) or (guess_col < 0 or guess_col > 9):
print("Oops, that's not even in the ocean!")
elif (board[guess_row][guess_col] == "X"):
print("You guessed that one already!")
else:
print("You missed my battleship!")
board[guess_row][guess_col] = "X"
if turn == 15:
print("Game Over")
board[ship1_row][ship1_col] = "S"
board[ship2_row][ship2_col] = "S"
board[ship3_row][ship3_col] = "S"
print_board(board)
print("The first ship was at row number " + str(ship1_row) + " and column number " + str(ship1_col))
print("The second ship was at row number " + str(ship2_row) + " and column number " + str(ship2_col))
print("The third ship was at row number " + str(ship3_row) + " and column number " + str(ship3_col))
print(restart())
if hit == 3:
print("You sank all of my battleships!")
print("You Win!")
print_board(board)
restart()
print ("Turn " + str(turn + 1))
print_board(board)
game()
喂,小心!在你的代码中,你做了一些非常错误的事情:
如果您返回一个值,就是它…所以继续添加行是没有意义的。请检查一下你怎样才能把你的船交给这个项目。在
当然,选择把船放在哪里的一个好的选择是使用一个列表,在那里你可以存储一艘船的所有点。在
如果这个人找到了一艘船的所有点……那么这艘船就毁了。 希望这有点帮助…祝你有个愉快的一天!在
相关问题 更多 >
编程相关推荐