Kullanıcıya, randomize edilmiş koordinatların mı yoksa Python'da bir savaş gemisi oyunu için giriş yapmak isteyip istemediklerini seçme şansı vermek istiyorum. Bunu yapmak için PyScripter'ın taşınabilir bir uygulamasını kullanıyorum not edilmelidir.Kullanıcıya hangi modu oynayacağına karar verme seçeneğini nasıl verebilirim?
Randomize Versiyon
from random import randint
Battleship_Board =[]
for x in range (0,5):
Battleship_Board.append(["O"] * 5)
def print_Battleship_Board(Battleship_Board):
for row in Battleship_Board:
print (" ".join(row))
print ("Let's play Battleships!")
print_Battleship_Board(Battleship_Board)
def random_row(Battleship_Board):
return randint(0, len(Battleship_Board) - 1)
def random_col(Battleship_Board):
return randint(0, len(Battleship_Board[0]) - 1)
Battleship_Row = random_row(Battleship_Board)
Battleship_Column = random_col(Battleship_Board)
for turn in range(5):
Guess_Board_Row = int(input("Guesss the X value"))
Guess_Board_Column = int(input("Guess the Y value"))
if Guess_Board_Row == Battleship_Row and Guess_Board_Column == Battleship_Column:
print("You sunk my battleship!")
break
else:
if (Guess_Board_Row < 1 or Guess_Board_Row > 5) or (Guess_Board_Column < 1 or Guess_Board_Column > 5):
print("Apologies, that's not on the grid")
elif(Battleship_Board[Guess_Board_Row][Guess_Board_Column] == "X"):
print("You already guessed that value")
else:
print("You missed my battleship")
Battleship_Board[Guess_Board_Row][Guess_Board_Column] = "X"
print("Turn" + str(turn+1) + " out of 4.")
print_Battleship_Board(Battleship_Board)
if turn >=4:
print("Game Over")
Sana kullanıcı sütun ve satır için bir randomize değerine dayalı bir oyun oynamak izin veriyorum görebileceğiniz gibi.
girilen Sürüm İşte
Battleship_Board =[]
for x in range (0,5):
Battleship_Board.append(["O"] * 5)
def print_Battleship_Board(Battleship_Board):
for row in Battleship_Board:
print (" ".join(row))
print ("Let's play Battleships!")
print_Battleship_Board(Battleship_Board)
Battleship_Row = int(input("Please enter a X value"))
Battleship_Column = int(input("Please enter a Y value"))
if (Battleship_Row < 1 or Battleship_Row > 5) or (Battleship_Column < 1 or Battleship_Row > 5):
print("Apologies, that's not on the grid")
for turn in range(5):
Guess_Board_Row = int(input("Guess the X value"))
Guess_Board_Column = int(input("Guess the Y value"))
if Guess_Board_Row == Battleship_Row and Guess_Board_Column == Battleship_Column:
print("You sunk my battleship!")
print("My Ship was here: [" + str(Battleship_Row) + "][" + str(Battleship_Column) + "]")
break
else:
if turn == 5:
Battleship_Board[Battleship_Row][Battleship_Column] = "X"
print_Battleship_Board(Battleship_Board)
print("Game Over")
print("My Ship was here: [" + str(Battleship_Row) + "][" + str(Battleship_Column) + "]")
else:
if (Guess_Board_Row < 1 or Guess_Board_Row > 5) or (Guess_Board_Column < 1 or Guess_Board_Column > 5):
print("Apologies, that's not on the grid")
elif(Battleship_Board[Guess_Board_Row][Guess_Board_Column] == "X"):
print("You already guessed that value")
else:
print("You missed my battleship!")
Battleship_Board[Guess_Board_Row][Guess_Board_Column] = "X"
print("Turns taken out of 5:", turn + 1)
print_Battleship_Board(Battleship_Board)
kullanıcı değerlerine girdi alır biridir.
Uygulamayı başlatırken kullanıcının seçmesi için bu iki sürüm arasında bir seçenek oluşturmak istiyorum. Bunu nasıl yapabilirim?
Bir "if" ifadesi? –
Ben çok fazla varsaydı, ama tüm kodun bütünlüğünü nasıl düzenleyeceğimi bilemedim, bu iki sürümü python'u anlamak ve çalıştırmak için ayırt etmem gerektiğine inanıyorum. –
Şimdiye kadar neler denediniz? Bir menü komut dosyasında/işlev/sınıfında herhangi bir girişimde bulunuldu mu? – jDo