def test_cell_occupied(self): """ test de la fonction cell_occupied """ robots = Robot_group() Robot(robots, RColors.RED, (0, 0)) Robot(robots, RColors.BLUE, (1, 1)) Robot(robots, RColors.GREEN, (2, 1)) Robot(robots, RColors.YELLOW, (2, 2)) for position in [(0, 0), (1, 1), (2, 1), (2, 2)]: self.assertTrue(robots.cell_occupied(position)) for position in [(1, 0), (1, 2), (2, 5), (10, 10)]: self.assertFalse(robots.cell_occupied(position))
def choix_grille(self, i): """ Lors du choix d'une nouvelle grille, le jeu est réinitialisé et redessiné, les robots et l'objectif sont masqués. """ # pour ouvrir les vieux .txt # name_grid = './test' + str(i + 1) + '.txt' # fd = open(name_grid,'r') # A = Board.load_from_json(fd) if i == 0: A, = Board.load_from_json(GRIDS_PATH + 'grid 6x6.json') elif i == 1: A, = Board.load_from_json(GRIDS_PATH + 'grid 8x8.json') elif i == 2: A, = Board.load_from_json(GRIDS_PATH + 'grid 10x10.json') elif i == 3: A, = Board.load_from_json(GRIDS_PATH + 'grid 12x12.json') elif i == 4: A, = Board.load_from_json(GRIDS_PATH + 'grid 14x14.json') elif i == 5: A, = Board.load_from_json(GRIDS_PATH + 'grid 16x16.json') else: # Pour ouvrir une grille aléatoire classique A = Board.new_classic() self.game.add_board(A) self.number_moves = 0 self.group = Robot_group() self.game = Game(self.game.board, self.group, self.game.goal) self.draw_grid()
def setUp(self) : board, = Board.load_from_json(GRID_FILENAME) robots = Robot_group() r1 = Robot(robots, RColors.RED, (5,0)) r2 = Robot(robots, RColors.BLUE, (11,1)) r3 = Robot(robots, RColors.GREEN, (2,15)) r4 = Robot(robots, RColors.YELLOW, (10,13)) goal = Goal(RColors.RED, (0,7)) self.game = Game(board,robots,goal)
def test_move3(self): """ test de la fonction move avec plusieurs robots""" # test sur la grille classic16x16 classic, = Board.load_from_json(CLASSIC_GRID) # on positionne un robot à chaque coin robots = Robot_group() r1 = Robot(robots, RColors.RED, (0, 0)) r2 = Robot(robots, RColors.BLUE, (0, 7)) r3 = Robot(robots, RColors.GREEN, (7, 0)) r4 = Robot(robots, RColors.YELLOW, (7, 7))
def open_grid(self): """ Ouvre une boîte de dialogue permettant de charger une grille existante sur le disque dur""" filename, filter = QFileDialog.getOpenFileName( self, 'selectionner un fichier contenant une grille', './grids', '*.json') board, = Board.load_from_json(filename) self.game.add_board(board) self.number_moves = 0 self.group = Robot_group() self.game = Game(self.game.board, self.group, self.game.goal) self.unprint_moves_list() self.draw_grid()
def test_str(self): robots = Robot_group() r1 = Robot(robots, RColors.RED, (0, 0)) r2 = Robot(robots, RColors.BLUE, (1, 1)) r3 = Robot(robots, RColors.GREEN, (2, 1)) r4 = Robot(robots, RColors.YELLOW, (2, 2)) self.assertEqual(str(r1), '"R" : [0, 0]') self.assertEqual(str(r2), '"B" : [1, 1]') self.assertEqual(str(r3), '"G" : [2, 1]') self.assertEqual(str(r4), '"Y" : [2, 2]') print(" Test affichage d'un ensemble de robots format json") print(robots)
def test_move1(self): """ test de la fonction move d'un robot sur la grille carrée simple 3x3""" square3 = Board([[9, 1, 3], [8, 0, 2], [12, 4, 6]]) robots = Robot_group() r1 = Robot(robots, RColors.RED, (0, 0)) r1.move(Direction.N, square3) self.assertEqual(r1.position, (0, 0)) r1.move(Direction.W, square3) self.assertEqual(r1.position, (0, 0)) r1.move(Direction.S, square3) self.assertEqual(r1.position, (2, 0)) r1.move(Direction.E, square3) self.assertEqual(r1.position, (2, 2)) r1.move(Direction.N, square3) self.assertEqual(r1.position, (0, 2)) r1.position = (0, 1) r1.move(Direction.S, square3) self.assertEqual(r1.position, (2, 1))
def test_Robot(self): robots = Robot_group() r1 = Robot(robots, RColors.RED, (0, 0)) r2 = Robot(robots, RColors.BLUE, (1, 1)) r3 = Robot(robots, RColors.GREEN, (2, 1)) self.assertTrue(r1.color in robots) self.assertEqual(r1, robots[r1.color]) self.assertTrue(r2.color in robots) self.assertTrue(r3.color in robots) # tentative d'ajout d'un robot de même couleur with self.assertRaises(AssertionError): r4 = Robot(robots, RColors.GREEN, (2, 2)) # tentative d'ajout d'un robot a une position déjà occupée with self.assertRaises(AssertionError): r4 = Robot(robots, RColors.YELLOW, (2, 1)) r4 = Robot(robots, RColors.YELLOW, (2, 2)) self.assertTrue(r4.color in robots)
def test_move2(self): """ test de la fonction move avec plusieurs robots""" # test sur la grille carrée simple 3x3 square3 = Board([[9, 1, 3], [8, 0, 2], [12, 4, 6]]) # on positionne un robot à chaque coin robots = Robot_group() r1 = Robot(robots, RColors.RED, (0, 0)) r2 = Robot(robots, RColors.BLUE, (0, 2)) r3 = Robot(robots, RColors.GREEN, (2, 0)) r4 = Robot(robots, RColors.YELLOW, (2, 2)) r1.move(Direction.S, square3) self.assertEqual(r1.position, (1, 0)) r2.move(Direction.S, square3) self.assertEqual(r2.position, (1, 2)) r2.move(Direction.W, square3) self.assertEqual(r2.position, (1, 1)) r4.move(Direction.N, square3) self.assertEqual(r4.position, (0, 2)) r3.move(Direction.E, square3) self.assertEqual(r3.position, (2, 2))
def choix_nb_robots(self, i): """ Les robots et l'objectif sont placés aléatoirement. L'extension """ self.group = Robot_group() self.game = Game(self.game.board, self.group, self.game.goal) self.nb_robots = i + 1 robots_pos = [0] * self.nb_robots robots_list = [0] * self.nb_robots robots_colors = [i for i in RColors] if self.placement_aleatoire: for i in range(self.nb_robots): x = randint(0, self.game.board.width - 1) y = randint(0, self.game.board.height - 1) while ((x, y) in robots_pos): x = randint(0, self.game.board.width - 1) y = randint(0, self.game.board.height - 1) robots_pos[i] = (x, y) robots_list[i] = Robot(self.game.robots, robots_colors[i], (x, y)) x = randint(0, self.game.board.width - 1) y = randint(0, self.game.board.height - 1) goal = Goal(RColors(randint(1, self.nb_robots)), (x, y)) self.game = Game(self.game.board, self.group, self.game.goal) self.game.add_goal(goal) self.initial_game_state = self.game.get_state() self.draw_robots_and_goal() else: fp = open(GAMES_PATH + DEFAULT_GAME, 'r') self.game = Game.load_from_json(fp) fp.close()
buttonBox = QDialogButtonBox(Qt.Horizontal) buttonBox.addButton(replay_button, QDialogButtonBox.ActionRole) buttonBox.addButton(new_game_button, QDialogButtonBox.ActionRole) buttonBox.addButton(exit_button, QDialogButtonBox.ActionRole) mainLayout.addWidget(self.end_msg) mainLayout.addWidget(buttonBox) self.setLayout(mainLayout) self.setGeometry(250, 250, 0, 50) def replay(self): self.retStatus = 1 self.close() def new_game(self): self.retStatus = 2 self.close() def exit_game(self): self.retStatus = 3 self.close() app = QApplication(sys.argv) group = Robot_group() game = Game.load_from_json(GAMES_PATH + DEFAULT_GAME) fen = MainWindow(game) fen.show() app.exec_()