Created
April 20, 2020 09:13
-
-
Save runqvist/59de81e57c50bf6278575dbda5f693b2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
class Boat: | |
'''all ships are part of the boat class''' | |
def __init__(self, length): | |
'''chooses random location & direction for ship for a given length''' | |
# print('\n', 'ship size: ', length, sep='') | |
self.direction = random.randint(0, 3) | |
# print('direction:', self.direction) | |
self.length = length | |
self.locations = [] # all locations of this ship | |
for i in range(self.length): #iterates through all locations of each individual ship | |
if i == 0: | |
x = random.randint(0, 9) | |
y = random.randint(0, 9) | |
else: | |
if self.direction == 0: # north (the y axis is reversed) | |
y = y - 1 | |
elif self.direction == 1: # east | |
x = x + 1 | |
elif self.direction == 2: # south (the y axis is reversed) | |
y = y + 1 | |
elif self.direction == 3: # west | |
x = x - 1 | |
self.locations.append([x, y]) | |
# print(self.locations) | |
# self.check_if_ship_on_ocean() | |
print(self) | |
Game.boats.append(self) | |
if self.check_if_ship_on_ocean(): | |
self.print_to_ocean() | |
def check_if_ship_on_ocean(self): | |
'''returns True if the ship's location is on the ocean''' | |
for i in range(self.length): | |
x,y = self.locations[i] | |
if x < 0 or y < 0 or x > 9 or y > 9: | |
# print('SHIP IS NOT LOCATED ON THE OCEAN') | |
return False | |
# print('ship is located on the ocean') | |
return True | |
def print_to_ocean(self): | |
'''prints the ship's location on existing ocean''' | |
l = self.locations | |
for i in range(len(l)): | |
x = self.locations[i][0] | |
y = self.locations[i][1] | |
# print(x, y) | |
# if self.check_if_ship_on_ocean(): | |
# Game.ocean[y][x] = 'X' # <----------------------------- | |
# if self.check_if_ship_on_ocean(): | |
# Game.display(self) | |
def __str__ (self): | |
output = '\n' | |
output += 'ship size: ' + str(self.length) | |
output += '\n' + 'direction: ' + str(self.direction) | |
output += '\n' + 'ship is {}on ocean'.format('' if self.check_if_ship_on_ocean() else 'not ') | |
output += '\n' + str(self.locations) | |
return output | |
def check_if_ship_is_on_ship(ship): | |
'''returns True if ship's location is superposed on another existing ship''' | |
pass | |
class Game: | |
'''generating the ocean''' | |
ocean = [] | |
boats = [] | |
def __init__(self): | |
EMPTY = '-' | |
row = [] | |
# generating a row | |
for i in range (10): | |
row.append(EMPTY) | |
# generating the ocean | |
for i in range (10): | |
Game.ocean.append(row) | |
Game.ocean[4][3] = 'Y' | |
print(Game.ocean) | |
# self.generate_ships() | |
self.run() | |
def generate_ships(self): | |
'''generates all ships on ocean''' | |
for i in range(4, 1, -1): # iterates through all 3 different types of ships (4,3,2) | |
for j in range(5 - i): # iterates through all instances of each different ship size | |
Boat(i) | |
def display(self): | |
'''printing the ocean''' | |
Game.ocean[0][0] = 'X' | |
Game.ocean[9][9] = 'X' | |
# print(Game.ocean[1][2]) # <----------------------------- | |
# print(Game.ocean[2][2]) # <----------------------------- | |
# print(Game.ocean) # <----------------------------- | |
print(' ', 'a ', 'b ', 'c ', 'd ', 'e ', 'f ', 'g ', 'h ', 'i ', 'j ') | |
for i in range (10): | |
print (i, Game.ocean[i]) | |
def run(self): | |
while True: | |
self.display() | |
x, y = self.ask() | |
if x is None: | |
break | |
print('Game over') | |
def ask(self): | |
'''asking for coordinates such as a0 or d4, if user wants to exit, ask() returns None''' | |
while True: | |
command = input('>>>') | |
if command.lower() in ['quit', 'exit']: | |
return None, None | |
if len(command) < 2: | |
print('bitte koordinaten eingeben, zb a4') | |
continue | |
x, y = command[0], command[-1] # [-1] ist das letzte zeichen in einem string | |
if x.lower() not in 'abcdefghij' or y not in '0123456789': | |
print('bitte gültige koordinaten eingeben, zb a4 (a bis j und 0 bis 9)') | |
continue | |
x = 'abcdefghij'.find(x.lower()) | |
y = int(y) | |
print('your command (x, y) is', x, y) | |
return x, y | |
# display() | |
# generate_ships() | |
g = Game() | |
# ask() | |
# generate_ships() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment