Code: Alles auswählen
from Ship import Ship
from random import randint
print("""
___ ____ ___ ______________ ____ _______________ _______________ _____ _____ ______________
\ \ / \ / / | ________| | | / _______| / \ | _ \ / _ | | ________|
\ \ / /\ \ / / | |____ | | | / / ___ \ | | \ \ / / | | | |____
\ \/ / \ \/ / | ____| | | | | | |___| | | | \ \ / / | | | ____|
\ / \ / | |_______ | |________ | \_______ \ / | | \ \/ / | | | |________
\__/ \__/ |_____________| |_____________| \_____________| \_______________/ |__| \__/ |__| |______________|
______________ _______________
|_____ _____| / \
| | / ___ \
| | | |___| |
| | \ /
|__| \_______________/
______________ _______ ______________ ______________ ____ _______________
| _ \ / _ \ |_____ _____| |_____ _____| | | | _________|
| |_| | / / \ \ | | | | | | | |____
| _________/ / /___\ \ | | | | | | | | _____
| _ \ / _______ \ | | | | | | | ____| |_____|
| |_| | / / \ \ | | | | | |________ | |_________
|______________/ /___/ \___\ |__| |__| |_____________| |_______________|
______________ ____ ____ ____ ___________
/ ____ \ | | | | | | | _ \
/ | |____| | | | | | | | |_| |
\ |__________ | |________| | | | | _______/
\____________ \ | ________ | | | | |
_____ | | | | | | | | | |
\ \_______| | | | | | | | | |
\_______________/ |____| |____| |____| |___|
""")
def createBoard(x,y):
board = []
for i in range(y):
row = []
for j in range(x):
row.append("O")
board.append(row)
return board
def printBoard(board):
numbers = []
for i in range(1,len(board[0])+ 1 ):
numbers.append(str(i))
print(" ", " ".join(numbers))
counter = 1
for row in board:
print(str(counter)," ".join(row))
counter +=1
name_spieler = input("Wie heisst du?: ")
print("Ok, los gehts", name_spieler, "!")
board = createBoard(9, 9)
printBoard(board)
def random_reihe(board):
return randint(0, len(board) - 1)
def random_kolone(board):
return randint(0, len(board[0]) - 1)
schiff_reihe = random_reihe(board)
schiff_kolone = random_kolone(board)
#user wird zum raten gefragt
def main():
Punkte = 0
for turn in range(40):
rate_reihe = None
rate_kolone = None
while rate_reihe is None:
try:
rate_reihe = int(input("Rate die Reihe (1-9): "))-1
if rate_reihe in range(len(board)):
pass
except ValueError:
print("Bitte eine Nummer eingeben: ")
while rate_kolone is None:
try:
rate_kolone = int(input("Rate die Kolone (1-9): "))-1
if rate_kolone not in range(len(board)):
pass
except ValueError:
print("Bitte eine Nummer eingeben: ")
#Wenn Spieler das richtige Feld getroffen hat
if schiff_reihe == rate_reihe and schiff_kolone == schiff_kolone:
board[rate_kolone][rate_reihe] = "X"
print("Glückwunsch, du hast das Battleship gesenkt! ")
if turn == 1:
Punkte += 3
print("Du hast", Punkte, "Punkte")
elif turn == 2:
Punkte += 2
print("Du hast", Punkte, "Punkte")
else:
Punkte += 1
print("Du hast", Punkte , "Punkte")
restart = input("Willst du nochmal spielen ?: ")
if restart == "ja" or "Ja":
main()
printBoard(board)
else:
if __name__ == '__main__':
main()
print("Du hattest", Punkte, "Punkte")
print("Game Over")
# den spieler warnen falls er ausserhalb des Spielfeldes geraten hat
else:
if (rate_reihe < 0 or rate_reihe > 10 ) or (rate_kolone < 0 or rate_kolone > 10):
print("Oops, das ist leider nicht im Spielfeld.")
# warnen falls gleich geraten wurde wie vorher
elif board[rate_kolone][rate_reihe] == "*":
print("Da hast du schonmal geraten.")
#Wenn die Rate falsch ist, dann das Feld mit * markieren
else:
print("Du hast nicht getrofen!")
board[rate_kolone][rate_reihe] = "*"
#Turn und board nochmals hier ausdrucken
print("Runde ", str(turn + 1 ), "von 40.")
printBoard(board)
#Wenn der Spieler es 40 mal versucht hat und nicht getroffen hat, das Spiel beenden
if __name__ == '__main__':
main()
print("Du hattest", Punkte, "Punkte")
print("Game Over")
Code: Alles auswählen
from random import randint
class Ship:
size = randint(1, 5)
position = []
ships = 4
def __init__(self, position:list, size = randint(1, 5), ships = 4):
self.size = size
self.position = position
def x(self):
return self.position[0]
def y(self):
return self.position[1]
def getSize(self):
return self.size
def num_Ships(self):
return self.ships