def testGetPlaneCells(self): p = Plane((2, 0), 'left') goodCells = [(2, 0), (0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (2, 2), (1, 3), (2, 3), (3, 3)] pCells = p.getPlaneCells() self.assertEqual(len(pCells), 10) for c in goodCells: if c not in pCells: assert False
def testHit(self): self.pl.drawPlane(Plane(Square(0, 2))) self.pl.drawPlane(Plane(Square(7, 4), orientation="down")) self.assertEqual(2, len(self.pl._planes)) x = self.pl.hit(Square(0, 2)) self.assertEqual(len(x), 10) x = self.pl.hit(Square(5, 4)) self.assertEqual(len(x), 1) x = self.pl.hit(Square(0, 0)) self.assertEqual(len(x), 1)
def testDrawing(self): self.pl.drawPlane(Plane(Square(0, 2))) self.assertEqual(1, len(self.pl._planes)) self.pl.cleanPlanes() self.assertEqual(0, len(self.pl._planes)) self.pl.drawPlane(Plane(Square(0, 2))) self.pl.drawPlane(Plane(Square(7, 4), orientation="down")) self.assertEqual(2, len(self.pl._planes)) self.assertEqual(2, self.pl.alivePlanes) self.assertEqual(2, len(self.ai._planes))
def addPlayerPlane(self, cabinPos, orientation): """ Method used to add a human player's plane to his board. :param cabinPos: tuple with 2 int values - the coordinates of the plane cabin in the matrix :param orientation: str - up/down/left/right: the plane orientation """ plane = Plane(cabinPos, orientation) PlaneValidator.validate(plane, self.__playerBoard) planeCells = plane.getPlaneCells() for cell in planeCells: self.__playerBoard[cell[0]][cell[1]] = '#' self.__playerPlanes.append(plane)
def testPlaneValidation(self): from domain.board import Board p = Plane(0, 'up') b = Board(8, 8).getMatrix() with self.assertRaises(PlaneError): PlaneValidator.validate(p, b) p = Plane((2, 0), 'left') self.assertIsNone(PlaneValidator.validate(p, b)) p = Plane((0, -1), 'down') with self.assertRaises(PlaneError): PlaneValidator.validate(p, b) p = Plane((2, 0), 'right') with self.assertRaises(PlaneError): PlaneValidator.validate(p, b)
def placePlane(self): try: colors = [ QtGui.QColor(0, 93, 224), QtGui.QColor(230, 247, 0), QtGui.QColor(0, 214, 60) ] if self._plPlanes < 2: strg = self.cabinSquare_Input.text() cabin = self.checkPos(strg) strg = strg.split() pl = Plane(cabin, orientation=strg[1]) self._game.playerB.drawPlane(pl) col = choice(colors) for bodyPart in pl.getCoords: for sq in bodyPart: item = QtWidgets.QTableWidgetItem() item.setBackground(col) self.playerBoard.setItem(sq.longitude + 1, sq.latitude + 1, item) self._plPlanes += 1 if self._plPlanes == 2: self.startGame_Button.setEnabled(True) else: raise Exception( "You can not have more than 2 planes on the board\n" "if you want to change them press clear board") except Exception as err: self.createDialog(str(err), "Error!", QtWidgets.QMessageBox.Critical)
def playerSetUp(self, nbPlayerPlanes=0): while True: try: cmd = input(">") if cmd == '0' or cmd == 'exit': exit(0) if cmd == 's' and nbPlayerPlanes == 2: break elif cmd == 's': raise ValueError( "You can only start the game after you have 2 planes on the board!" ) if cmd == 'clear': self._game.playerB.cleanPlanes() nbPlayerPlanes = 0 self.printPlBoard() continue cmdL = cmd.split() if len(cmdL) != 2: print("Inccorect command!") else: if nbPlayerPlanes == 2: raise Exception( "You can only have 2 planes on the board. If you want\nto change the plane you can clear the board using 'clear' command." ) self._game.playerB.drawPlane( Plane(cabinPos=self.inputPosToSquare(cmdL[0]), orientation=cmdL[1])) self.printPlBoard() nbPlayerPlanes += 1 except Exception as err: print(err)
def addComputerPlanes(self): """ Method that adds 2 random and valid planes to the computer's board. """ addedPlanes = 0 while addedPlanes < 2: try: cabPos = (random.randint(0, 7), random.randint(0, 7)) orientation = random.choice(["up", "down", "left", "right"]) plane = Plane(cabPos, orientation) PlaneValidator.validate(plane, self.__computerBoard) planeCells = plane.getPlaneCells() for cell in planeCells: self.__computerBoard[cell[0]][cell[1]] = '#' self.__computerPlanes.append(plane) addedPlanes += 1 except PlaneError: pass
def testMarkDestroyedPlane(self): self.srv.resetGame() p = Plane((2, 0), 'left') self.srv.markDestroyedPlane(p, self.srv.getPlayerBoard()) b = self.srv.getPlayerBoard() XCells = 0 for i in range(8): for j in range(8): XCells += 1 if b[i][j] == 'X' else 0 self.assertEqual(XCells, 10)
def genPlanes(board): ''' Generates valid plane's for a given board Input: - plane - Plane - board - Board Output: - planesL - List of valid Planes ''' planesL = [] for i in range(board.height): for j in range(board.lenght): for ori in ["up", "down", "left", "right"]: try: pl = Plane(Square(i, j), orientation=ori) board.validatePlaneCoords(pl) planesL.append(pl) except Exception: pass return planesL
def testGetPlaneOrientation(self): p = Plane((0, 2), 'up') self.assertEqual(p.getPlaneOrientation(), 'up') p = Plane((4, 7), 'right') self.assertEqual(p.getPlaneOrientation(), 'right')
def testGetCabinPosition(self): p = Plane((0, 2), 'up') self.assertEqual(p.getCabinPosition(), (0, 2)) p = Plane((4, 7), 'right') self.assertEqual(p.getCabinPosition(), (4, 7))
def testValidPlane(self): self.assertTrue( self.b.validatePlaneCoords(Plane(cabinPos=Square(0, 2)))) self.b.hit(Square(2, 2)) self.assertFalse( self.b.validatePlaneCoords(Plane(cabinPos=Square(2, 2))))