def setUp(self): self.workers = [ Worker("p1", 1), Worker("p1", 2), Worker("p2", 1), Worker("p2", 2) ]
def setUp(self): """Mock-up context for testing the rulechecker. 4 workers for 2 players, each placed along the diagonal (i.e. (0, 0), (1, 1), etc) To the east of each worker, a building is built with height equal to the worker's number in the total workers (i.e. Worker 2 builds a 2-floor building) Example: 0W | 1 | 0 | 0 | 0 | 0 0 | 0W | 2 | 0 | 0 | 0 0 | 0 | 0W | 3 | 0 | 0 0 | 0 | 0 | 0W | 4 | 0 0 | 0 | 0 | 0 | 0 | 0 0 | 0 | 0 | 0 | 0 | 0 """ self.workers = [ Worker("player1", 1), Worker("player1", 2), Worker("player2", 1), Worker("player2", 2) ] self.board = Board() for i, worker in enumerate(self.workers): self.board.place_worker(worker, (i, i)) for _ in range(i): self.board.build_floor(worker, Direction.EAST)
def setUp(self): p1id = uuid.uuid4() p2id = uuid.uuid4() self.workers = [Worker(p1id, 1), Worker(p1id, 2), Worker(p2id, 1), Worker(p2id, 2)]
def test_create_worker(self): """Test worker creation""" worker1 = Worker("player1", 1) self.assertEqual(worker1.player, "player1") self.assertEqual(worker1.number, 1) worker2 = Worker("player2", 2) self.assertEqual(worker2.player, "player2") self.assertEqual(worker2.number, 2)
def test_worker_bounds(self): """Test worker creation with out of bounds number.""" with self.assertRaises(ValueError) as context: Worker("player1", 3) self.assertTrue( "Worker number out of range!" in str(context.exception)) with self.assertRaises(ValueError) as context: Worker("player1", 0) self.assertTrue( "Worker number out of range!" in str(context.exception))
def board_to_board(self, board_arr): """ Converts a 2d Board cell array to a Board object :param [[Cell, ...], ...] board_arr: where Cell is either a Height (0, 1, 2, 3, 4) or a BuildingWorker, a Height followed by a Worker :rtype Board: a suitable board """ new_board = [[0 for col in range(Board.BOARD_SIZE)] for row in range(Board.BOARD_SIZE)] workers = {} for (row, rows) in enumerate(board_arr): for col in range(len(rows)): cur_cell = board_arr[row][col] if isinstance(cur_cell, int): new_board[row][col] = cur_cell elif cur_cell: new_board[row][col] = int(cur_cell[0]) worker_str = cur_cell[1:] worker_num = int(worker_str[-1:]) player_name = worker_str[:-1] cur_worker = Worker(self.name_to_uuid(player_name), worker_num) if cur_worker: workers[cur_worker] = (row, col) cur_board = Board(new_board, workers) return cur_board
def test_hash(self): """Test worker hashing using dictionary""" worker_dict = { Worker("player1", 1): 1, Worker("player1", 2): 2, Worker("player2", 1): 3, Worker("player2", 2): 4, Worker("player1", 1): 5 } self.assertEqual(worker_dict[Worker("player1", 1)], 5) self.assertEqual(worker_dict[Worker("player1", 2)], 2) self.assertEqual(worker_dict[Worker("player2", 1)], 3) self.assertEqual(worker_dict[Worker("player2", 2)], 4)
def setUp(self): """Mock-up context for testing the board. 4 workers for 2 players, each placed along the diagonal (i.e. (0, 0), (1, 1), etc) """ self.ids = [uuid.uuid4(), uuid.uuid4()] self.player_names = ["player1", "player2"] self.uuids_to_name = {} for player_id, player_name in zip(self.ids, self.player_names): self.uuids_to_name[player_id] = player_name self.workers = [ Worker(self.ids[0], 1), Worker(self.ids[0], 2), Worker(self.ids[1], 1), Worker(self.ids[1], 2) ]
def setUp(self): """Mock-up context for testing the observer""" self.ids = [uuid.uuid4(), uuid.uuid4()] self.player_names = ["player1", "player2"] self.uuids_to_name = { self.ids[0]: self.player_names[0], self.ids[1]: self.player_names[1] } for player_id, player_name in zip(self.ids, self.player_names): self.uuids_to_name[player_id] = player_name self.workers = [ Worker(self.ids[0], 1), Worker(self.ids[0], 2), Worker(self.ids[1], 1), Worker(self.ids[1], 2) ] self.observer = Observer()
def place_worker(self, cur_board): """Worker Placement. :param Board cur_board: a copy of the current board :rtype tuple (Worker, (row, col)) placement: the placement """ new_worker = Worker(self, len(self.workers) + 1) self.workers.append(new_worker) placement = self.strategy.plan_placement(new_worker, cur_board) return new_worker, placement
def test_can_place_worker_on_worker(self): """Case for placing a worker onto another worker.""" board = Board( workers={ self.workers[0]: (0, 0), self.workers[1]: (0, 1), self.workers[2]: (0, 2) }) self.assertFalse( rulechecker.can_place_worker(board, Worker("player2", 2), (0, 1)))
def worker_to_worker(self, worker): """ Converts a string representation of a Worker to a worker. e.g. "potato2" would be a Worker of player "potato" and number 2 :param WorkerMsg worker: a series of lowercase letters followed by either 1 or 2 :rtype Worker worker: the Worker """ player, number = worker[:-1], int(worker[-1:]) player_uuid = self.name_to_uuid(player) return Worker(player_uuid, number)
def test_can_place_five_workers(self): """Case for placing more than 4 workers.""" board = Board( [[0, 1, 2, 3, 4, 0]], { self.workers[0]: (0, 0), self.workers[1]: (0, 1), self.workers[2]: (0, 2), self.workers[3]: (0, 3) }) self.assertFalse( rulechecker.can_place_worker(board, Worker("player3", 2), (0, 5)))
def test_place_more_than_4_workers(self): """Case for placing more than 4 workers.""" board = Board( [[0, 1, 2, 3, 4, 0]], { self.workers[0]: (0, 0), self.workers[1]: (0, 1), self.workers[2]: (0, 2), self.workers[3]: (0, 3) }) self.assertEqual(len(board.workers), 4) board.place_worker(Worker("player3", 2), (0, 5)) self.assertEqual(len(board.workers), 5)
def place_worker(self, cur_board): """ask the client for a placement given the current placements :param Board cur_board: a copy of the current board :rtype tuple (Worker, (row, col)) placement: the placement """ self._worker_count += 1 worker_placements = cur_board.dump_workers_as_json(self._uuid_to_name) self._send_json(worker_placements) place = self._recv_json() if not validate_json(PLACE, place): raise ValueError("place didn't validate: " + str(place)) worker = Worker(self._player_id, self._worker_count) return (worker, tuple(place))
def test_equality(self): """Test equality between workers""" self.assertEqual(Worker("player1", 1), Worker("player1", 1))
def test_not_equality(self): """Test not equality between workers""" self.assertNotEqual(Worker("player2", 1), Worker("player1", 1)) self.assertNotEqual(Worker("player2", 2), Worker("player2", 1)) self.assertNotEqual(Worker("player2", 2), 4)