Skip to content

Instantly share code, notes, and snippets.

@mustooch
Created September 23, 2019 19:37
Show Gist options
  • Save mustooch/24451fb37e8d346a389657d8a380c9c4 to your computer and use it in GitHub Desktop.
Save mustooch/24451fb37e8d346a389657d8a380c9c4 to your computer and use it in GitHub Desktop.
################################################################################
## python battle ship program ##################################################
################################################################################
import random
class Grid:
def __init__(self, height, width):
self.height = height
self.width = width
self.sign = "X"
def make_grid(self):
self.grid = []
for y in range(self.height):
self.grid.append([])
for x in range(self.width):
self.grid[y].append(0)
def show_grid(self):
print(" 1 2 3 4 5 6") #to arrange
print(" -----------")
for i in range(self.height):
print("{}|".format(i+1), end=" ")
for j in range(self.width):
print(str(self.grid[i][j]), end=" ")
print()
def place_boat_on_grid(self, boat):
boatX = boat[0]-1
boatY = boat[1]-1
self.grid[boatY][boatX] = 1
class Player:
def __init__(self, boat1, boat2):
#to arrange
self.boat1 = boat1
self.boat2 = boat2
def place_boats(self, grid):
grid.place_boat_on_grid(self.boat1)
grid.place_boat_on_grid(self.boat2)
def Game():
print("Init grid:\n")
grid1 = Grid(6,6)
grid1.make_grid()
grid1.show_grid()
print("\n")
print("Grid after player places boats:\n")
player1 = Player( (2,4), (5,6) )
player1.place_boats(grid1)
grid1.show_grid()
Game()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment