Skip to content

Instantly share code, notes, and snippets.

@Jeklah
Last active July 24, 2021 22:31
Show Gist options
  • Save Jeklah/f316c57f5f361e2a122f17c2026da723 to your computer and use it in GitHub Desktop.
Save Jeklah/f316c57f5f361e2a122f17c2026da723 to your computer and use it in GitHub Desktop.
Classes for battleships and players in the game.
"""Game Board class for battleships."""
class GameBoard:
"""Game board class to represent the game area for where the
ships can be placed in the battleships game."""
def __init__(self, shipDrawer):
self.drawBoard(10)
def drawBoard(area):
for _ in range(area):
print('~~~~~~~~~~')
"""Player class for battleships."""
import Ship
class Player:
"""Player class to represent the player in the current game."""
def __init__(self, playerID, name, shipsDestroyed, winner = False, shipCount = 5):
self.name = name
self.ships = []
self.shipsDestroyed = 0
self.winner = winner
self.playerID = playerID
self.shipCount = shipCount
def shipList(self):
for _ in range(self.shipCount):
self.ships.append(Ship.shipID)
def shoot(self, shot_posX, shot_posY, gameBoardID):
if shot_posX == gameBoardID.shipPlaced and shot_posY == gameBoardID.shipPlaced:
print(f'{self.name} has hit a ship!')
else:
print(f'{self.name} misses!')
def destroyedShip(self, oppnonentID):
self.shipsDestroyed += 1
if self.shipsDestroyed == 1:
print(f'{self.name} has destroyed their first ship!')
elif self.shipsDestroyed >= 3:
print(f'{self.name} has destroyed {self.shipsDestroyed}! They have nearly won!!')
print(f'{oppnonentID.name} hurry up and destroy some of their ships!')
def win(self, opponentID):
print(f'{self.name} wins the game!!')
print(f'{self.name} has beaten {opponentID.name}!')
class Ship:
"""Battleship class for the ships in the game."""
def __init__(self, hitpoints, posX, posY, name, alive=True):
self.hitpoints = hitpoints
self.posX = posX
self.posY = posY
self.alive = alive
self.name = name
def shipHit(self):
self.hitpoints = self.hitpoints - 1
if self.hitpoints <= 0:
print(f'{self.name} has been destroyed!')
self.alive = False
else:
print(f'{self.name} has been hit! {self.name} has {self.hitpoints} left')
def shipDestroyed(self, playerID):
print(f'{self.name} sinks into the ocean slowly...')
if playerID.shipsDestroyed >= 1:
print(f'{playerID.name} has destroyed another ship!')
else:
print(f'{playerID.name} has destroyed a ship!')
"""Ship Draw class to draw the ships and remove them as the game goes on."""
import Ship
import Player
class ShipDraw:
"""Class to place and remove ships as the game starts and goes on."""
def startGame(player1=Player.playerID, player2=Player.playerID, gameboard):
for ship in player1.ships:
if gameBoard.posX == ship.posX and gameBoard.posY == ship.posY:
print('*')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment