-3
私はPythonで戦艦ゲームを作っています。どのように私は戦艦を1つ以上のスペースにすることができますか?
私は船を1つのスペースよりも長くする方法を理解できません。
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()
修正とアイデアのおかげで、でもまだコーディングが新しくなっていますので、どのように船をリストに入れるのか正確にはわかりません。私に例を示したり、私がそれをやる方法を説明したりできますか? – 19jam04
こんにちは...ここで動作するコードが必要な場合は、ここでhttp://code.activestate.com/recipes/578836-the-game-of-battleships-in-python/を使用してください。他の多くの例を簡単に見つけることができます。 –
もし私があなたであれば、最初から始め、キャンバスとグラフィックパーツの使用のためのTKモジュールも学ぶのがよいでしょう...それはちょっと難しいですが、 ) –