私はコーディングが新しく、コードアカデミーデザインに基づいたPythonで戦艦ゲームをコーディングしようとしています。変数を表示および更新するにはどうしたらいいですか?
ほとんどのものが動作しますが、ボードの表示は...最初に1回印刷されますが、更新されず、何をすべきかわかりません... 誰かが私を助けてくれますか? (ここに私のコードです!:)
import random
def board_ini():
board = []
for x in range(0,5):
board.append(["O"] * 5)
for row in board:
print(" ".join(row))
return board
def random_row(board):
rr = random.randint(0, len(board) - 1)
return rr
def random_col(board):
rc = random.randint(0, len(board[0]) - 1)
return rc
def Battleship():
print ("\nLet's play Battleship!\n")
print ("You are going to guess coordinate. You can type integers between 0 and 4.\n")
print ("You have a total of 4 tries!\n",end ="" "Good luck!!\n")
theboard = board_ini()
ship_row = random_row(theboard)
ship_col = random_col(theboard)
print(ship_row)
print(ship_col)#debugging
for turn in range(1,5):
try:
guess_row = int(input("Guess Row:")) #let the user try again?
except Exception as e:
print (e.args)
try:
guess_col = int(input("Guess Col:")) #let the user try again?
except Exception as e:
print (e.args)
if guess_row == ship_row and guess_col == ship_col:
print ("\nCongratulations! You sunk my battleship!")
break
else:
if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
print ("Oops, that's not even in the ocean.")
elif(theboard[guess_row][guess_col] == "X"):
print ("You guessed that one already.")
turn = turn - 1 #would be nice if it did not count as a turn (turn function ?)
else:
print ("You missed my battleship!")
theboard[guess_row][guess_col] = "X"
if turn == 4:
print ("\nDefeat, Game over!!")
print ("My Ship was in: %s,%s" % (ship_row,ship_col))
turn += 1
print ("Turn: %s" % turn) #prints turn 5(it should not)
theboard #the board is not displaying correctly (should display the coordinate (X) after each turn but here it does not display at all)
def Game():
i = True
while i == True:
Battleship()
r = input("Do you want to rematch (Y/N)?")
if r == "Y":
r.lower()
" ".join(r)
i = True
else:
i = False
print ("Thank you! Game over")
Game()
ありがとうたくさんの人!
私はこのコードを実行しました。それは正常に動作しているように見えます。 – Nurjan