Skip to content

Instantly share code, notes, and snippets.

@runqvist
Created April 27, 2020 07:30
Show Gist options
  • Save runqvist/c60f22963c482f3ce80b6ed7d7db31b2 to your computer and use it in GitHub Desktop.
Save runqvist/c60f22963c482f3ce80b6ed7d7db31b2 to your computer and use it in GitHub Desktop.
import random
class Ship:
'''all ships are part of the boat class'''
number = 1
def __init__(self, player, length):
'''chooses random location & direction for ship for a given length'''
# print('ship size: ', length, sep='')
self.direction = random.randint(0, 3)
# print('direction:', self.direction)
self.length = length
self.player = player
self.locations = [] # all locations of this ship
while True:
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('location: ',self.locations)
if self.check_ship_is_outside_ocean():
continue
print('inside ocean')
if self.check_ship_is_on_other_ship():
continue
print('not colliding')
# --- ship is correct (not outside ocean, not on other ship)
# unique number for ship
self.number = Ship.number
Ship.number += 1 # preparing Ship.number for the next ship
Game.ships[self.player].append(self) # adding this ship to Game.ships (collection of all ships)
def check_ship_is_outside_ocean(self):
'''returns True if the ship's location is on the ocean'''
for i in range(self.length):
x, y = self.locations[i]
print('testing:', x, y, i)
if x < 0 or y < 0 or x > 9 or y > 9:
# print('SHIP IS NOT LOCATED ON THE OCEAN')
return True
# print('ship is located on the ocean')
return False
def check_ship_is_on_other_ship(self):
'''returns True if ship's location is superposed on another existing ship'''
for i in range(self.length):
x, y = self.locations[i]
# print (x, y)
for other_ship in Game.ships[self.player]:
for other_location in other_ship.locations:
if other_location[0] == x and other_location[1] == y:
return True
return False
def display(shot_list):
'''printing the ocean'''
print()
print(' ', 'a ', 'b ', 'c ', 'd ', 'e ', 'f ', 'g ', 'h ', 'i ', 'j ')
for row in range(10):
print(row, end = ': ')
for column in range(10):
for shot in shot_list:
if shot[0] == column and shot[1] == row:
print('x' if shot[2] else 'o', end=' ')
break # going to line xxx
else: # no shot found for this coordinate
print('.', end=' ')
# xxx
print()
def hit_is_outside_ocean(x, y, minimum = 0, maximum = 9):
if x < minimum or x > maximum or y < minimum or y > maximum:
print('numbers between {} - {} only'.format(minimum, maximum))
return True
else:
return False
class Game:
shot_list = [ [], [] ]
ships = [ [], [] ]
def __init__(self):
for i in range(4,0,-1):
for j in range(5-i):
print('i, j: ',i, j)
Ship(0, i)
Ship(1, i)
self.run()
def run(self):
player = 0
turn = 0
while True:
turn += 1
print()
print('your turn, player {} (turn {})'.format(player, turn))
display(Game.shot_list[player])
print()
x = int(input('x: '))
y = int(input('y: '))
if hit_is_outside_ocean(x, y):
continue
Game.shot_list[player].append( [x,y,False] )
print(Game.shot_list)
# next player
if player == 0:
player = 1
else:
player = 0
print('Game over')
Game()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment