def test_countIntersections(self): comp = ComputerPlayer() ships = [Battleship(4, 0, 0, "vertical")] intersections = comp.countIntersections(0, 0, 1, 0, ships) assert (intersections == 0) intersections = comp.countIntersections(0, 0, 0, 1, ships) assert (intersections == 1)
def test_makeMove(self): comp = ComputerPlayer() table = Table([]) table.table[1][1] = "O" table.table[2][2] = "~" table.table[3][3] = "O" results = comp.makeMove(table.table) assert (results not in [[1, 1], [2, 2], [3, 3]])
def test_fits(self): comp = ComputerPlayer() result = comp.fits(4) if result[2] == "vertical": assert (result[1] + 4 < 8) elif result[2] == "horizontal": assert (result[0] + 4 < 8) else: assert (result[0] + 4 < 8) assert (result[1] + 4 < 8)
def test_findAttackCoordinates(self): comp = ComputerPlayer() table = Table([]) table.table[1][1] = "O" table.table[2][2] = "~" table.table[3][3] = "O" results = comp._findAttackCoordinates(table.table, 1, 1) assert (results == [[2, 1], [3, 1], [4, 1], [5, 1], [1, 2], [1, 3], [1, 4], [1, 5]]) table.table[1][2] = "O" results = comp._findAttackCoordinates(table.table, 1, 1) assert (results == [[3, 1], [4, 1], [5, 1], [6, 1]]) table.table[1][3] = "~" results = comp._findAttackCoordinates(table.table, 1, 1) assert (results == [[0, 1]])
def __init__(self): self._atnc = { 'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4, 'F': 5, 'G': 6, 'H': 7 } self._ui = GUIui(Table([])) self._player = Player() self._computer = ComputerPlayer() plShips = self._ui.setupPlayer() for ship in plShips: self._player.addShip(ship[3], ship[0], ship[1], ship[2]) self._playerTable = Table(self._player.ships) self._computerTable = Table([])
def __init__(self): ui = UI() computerPlayer = ComputerPlayer() player = Player() ships = ui.setupPlayer() for ship in ships: player.addShip(ship[3], ship[0], ship[1], ship[2]) playerTable = Table(player.ships) computerTable = Table([]) while True: ui.printTables(playerTable, computerTable) hMove = ui.readMove(playerTable, computerTable) if hMove != []: computerTable.applyMove(hMove, computerPlayer.ships) if computerPlayer.hasLost: ui.printTables(playerTable, computerTable) ui.endGame("PLAYER") return playerTable.applyMove(computerPlayer.makeMove(playerTable.table), player.ships) if player.hasLost: ui.printTables(playerTable, computerTable) ui.endGame("COMPUTER") return
class controller: def __init__(self): self._atnc = { 'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4, 'F': 5, 'G': 6, 'H': 7 } self._ui = GUIui(Table([])) self._player = Player() self._computer = ComputerPlayer() plShips = self._ui.setupPlayer() for ship in plShips: self._player.addShip(ship[3], ship[0], ship[1], ship[2]) self._playerTable = Table(self._player.ships) self._computerTable = Table([]) # returns the player table and the computer table @property def tables(self): return [self._playerTable, self._computerTable] # applies the input read from the Text widget in mainGame module if it is valid def applyInput(self, input): if self._ui.checkCoordinates(input): if input[0] in "12345678": y = int(input[0]) - 1 x = self._atnc[input[1].upper()] else: y = int(input[1]) - 1 x = self._atnc[input[0].upper()] self._computerTable.applyMove([x, y], self._computer.ships) if self._computer.hasLost: return [self._playerTable, self._computerTable, "player"] self._playerTable.applyMove( self._computer.makeMove(self._playerTable.table), self._player.ships) if self._player.hasLost: return [self._playerTable, self._computerTable, "computer"] return [self._playerTable, self._computerTable, "#rezist"] # calls the endGame method from the GUIui def endGame(self, winner): self._ui.endGame(winner)
def test_setupShips(self): comp = ComputerPlayer() assert (len(comp.ships) == 3)