def getSquareCoord(self, x, y): x1, y1, x2, y2, squareSize = self.getBoardCoords() if x1 < x < x1 + squareSize * 10 and y1 < y < y1 + squareSize * 10: return Coord((y - y1) // squareSize, (x - x1) // squareSize, 0) elif x2 < x < x2 + squareSize * 10 and y2 < y < y2 + squareSize * 10: return Coord((y - y2) // squareSize, (x - x2) // squareSize, 1) return None
def paintEvent(self, e): x1, y1, x2, y2, squareSize = self.getBoardCoords() qp = BattleshipPainter() qp.begin(self) qp.setRenderHint(QPainter.Antialiasing) qp.drawGrid(x1, y1, squareSize) qp.drawGrid(x2, y2, squareSize) qp.drawBoats( self.battleship.getPlayerBoards(1)[0].boats, self.boatImages, x1, y1, x2, y2, squareSize) boards = self.battleship.getPlayerBoards(1) for i in range(2): for x in range(self.battleship.boardSize): for y in range(self.battleship.boardSize): coord = Coord(x, y) square = boards[i].getCoord(coord) if square in [Board.SHOT_SUNK, Board.SHOT_HIT]: qp.drawHitShot(self.getSquareRect(coord, i)) elif square == Board.SHOT_MISSED: qp.drawMissedShot(self.getSquareRect(coord, i)) if self.placingBoats and self.currentBoat is not None: qp.drawBoats({0: self.currentBoat}, self.boatImages, x1, y1, x2, y2, squareSize) nbTexts = len(self.texts) font = QFont("Arial", 42) qp.setFont(font) for k in range(max(0, nbTexts - 1), nbTexts): qp.drawText(self.width() / 2 - len(self.texts[k]) * 12, 60 + (nbTexts - k) * 20, self.texts[k]) qp.end()
def getFreeCoord(self): coords = [] for x in range(self.board.shape[0]): for y in range(self.board.shape[1]): if self.get(x, y) == 0: coords.append(Coord(x, y)) rand = random.randint(0, len(coords) - 1) return coords[rand]
def getCoords(self): coords = [self.position] for i in range(self.length - 1): coords.append( Coord( self.position.x + (i + 1) * self.direction.getDirection()[0], self.position.y + (i + 1) * self.direction.getDirection()[1])) return coords
def placeBotBoats(self): self.battleship.nextPlayer() while len(self.battleship.getAvailableBoats().keys()) > 0: self.currentBotBoat = next( iter(self.battleship.getCurrentPlayerBoards() [0].getAvailableBoats().values())) self.currentBotBoat.replace(self.directions[randint(0, 1)], Coord(randint(0, 9), randint(0, 9))) if self.currentBotBoat.isInBoard(self.battleship.boardSize): self.battleship.placeBoat(self.currentBotBoat.id, self.currentBotBoat.position, self.currentBotBoat.direction) self.battleship.nextPlayer()
def __init__(self, id, name, length, coord=Coord(0, 0), direction=Direction(Direction.RIGHT)): self.id = id # int self.name = name # string self.length = length #int self.position = coord # object : the coordonates of the rear of the boat self.direction = direction # object self.squares = self.initSquares( ) # dict, keys : coordonates, values : 0 or 1
from Battleship.Battleship import Battleship from Battleship.Coord import Coord from Battleship.Direction import Direction from Battleship.Board import Board if __name__ == "__main__": battleship = Battleship() assert (battleship.currentPlayer == 1) assert (battleship.placeBoat(0, Coord(4, 2), Direction(Direction.DOWN))) assert (battleship.placeBoat(1, Coord(0, 1), Direction(Direction.RIGHT))) assert (battleship.placeBoat(2, Coord(3, 8), Direction(Direction.DOWN))) assert (battleship.placeBoat(3, Coord(5, 4), Direction(Direction.DOWN))) assert (battleship.placeBoat(4, Coord(2, 1), Direction(Direction.RIGHT))) battleship.nextPlayer() assert (battleship.currentPlayer == 2) assert (battleship.placeBoat(0, Coord(0, 0), Direction(Direction.RIGHT))) assert (battleship.placeBoat(1, Coord(1, 0), Direction(Direction.RIGHT))) assert (battleship.placeBoat(2, Coord(2, 0), Direction(Direction.RIGHT))) assert (battleship.placeBoat(3, Coord(3, 0), Direction(Direction.RIGHT))) assert (battleship.placeBoat(4, Coord(4, 0), Direction(Direction.RIGHT))) battleship.shoot(Coord(0, 2)) assert (battleship.getCurrentPlayerBoards()[1].board[0, 2] == Board.SHOT_HIT) assert (battleship.getOtherPlayerBoards()[0].board[0, 2] == Board.SHOT_HIT) assert (battleship.getOtherPlayerBoards()[0].boats[1].squares[Coord( 0, 2)] == 1)
if 0 > coord.x or coord.x > boardSize - 1 or 0 > coord.y or coord.y > boardSize - 1: return False return True def initSquares(self): squares = {} for coord in self.getCoords(): squares[coord] = 0 return squares def shoot(self, targetCoord): # target : coordonates of the target for square in self.squares: if square == targetCoord: self.squares[square] = 1 def isDestroyed(self): for square in self.squares: if self.squares[square] != 1: return False return True if __name__ == '__main__': boat = Boat(0, 'toto', 4, Coord(0, 0), Direction(Direction.RIGHT)) print(boat.squares) boat.shoot(Coord(0, 0)) boat.shoot(Coord(1, 0)) boat.shoot(Coord(2, 0)) boat.shoot(Coord(3, 0)) print(boat.squares) print(boat.isDestroyed())