def marble_move(self, row, col) -> 'Game': n = self.__board.get_block_size() marble_placement = Placement(Position(row // n, col // n), Position(row % n, col % n)) new_board = self.__board.play_marble(marble_placement, self.__player_num) return Game(new_board, self.__win_length, self.__player_num, self.__stage, self.__num_players, self.__chosen_tile).next_stage()
def __init__(self): """The class constructor. Args: self (Actor): an instance of Actor. """ self._text = "" self._position = Position(0, 0) self._velocity = Position(0, 0)
def load(self): game = world.Game.get_instance() spare_battery1 = SpareBattery(4, 0, 3) spare_battery2 = SpareBattery(4, 3, 3) spare_battery3 = SpareBattery(0, 1, 2) robot1 = Robot(2, 4, 9) game.create_grid(Position(0, 0), Position(500, 500), 100) game.add_game_object(spare_battery1) game.add_game_object(spare_battery2) game.add_game_object(spare_battery3) game.add_game_object(robot1)
def __init__(self): pygame.init() self.level = Level1() self.size = self.width, self.height = (500, 500) self.screen = pygame.display.set_mode(self.size) self.done = False self.game_objects: List[GameObject] = [] self.start_game_objects = [] self.grid = self.from_position, self.to_position, self.field_size = ( Position(0, 0), Position(*self.size), 100) Game.instance = self
def get_marble_placement(self, analyser: BoardAnalyser) -> Placement: move_instruction = f"""Player {self.__colour}: Enter a string of length 4 - For example '0112' - 'rowcolrowcol' First couple of characters: The block on which to place the marble Second couple of characters: The position to place the marble """ move_string = get_string_of_length(4, move_instruction) return Placement( block_pos=Position(int(move_string[0]), int(move_string[1])), marble_pos=Position(int(move_string[2]), int(move_string[3])) )
def test_rotate_block(self): o = None tests = [{ 'rot': Rotation(Position(0, 0), True), 'new': Block.from_int_array([[1, 1, o], [1, o, o], [o, o, o]]) }, { 'rot': Rotation(Position(0, 1), False), 'new': Block.from_int_array([[o, o, o], [o, 1, o], [o, 0, 1]]) }] for test in tests: new_board = self.test_board.rotate_block(test['rot']) self.assertEqual(new_board.get_block(test['rot'].get_block_pos()), test['new'])
def test_play_marble(self): o = None tests = [{ 'move': Placement(Position(0, 0), Position(0, 0)), 'new': Block.from_int_array([[1, o, o], [1, o, o], [1, 1, o]]), 'colour': 1 }, { 'move': Placement(Position(0, 1), Position(2, 1)), 'new': Block.from_int_array([[o, o, o], [0, 1, o], [1, 0, o]]), 'colour': 0 }] for test in tests: new_board = self.test_board.play_marble(test['move'], test['colour']) self.assertEqual(new_board.get_block(test['move'].get_block_pos()), test['new'])
def from_config(config_path: str) -> 'Game': config = read_config(config_path) win_length, block_size, board_size, num_players = \ config['game']['win_length'], \ config['game']['block_size'], \ config['game']['board_size'], \ config['game']['players'] board = Board.blank(board_size, block_size) return Game(board, win_length, 0, 0, num_players, Position(0, 0))
def __init__(self, word): """ Class constructor. Calls the super class init function (from Actor) Args: self (Word): An instance of Word word (string): a random word from the word bank """ super().__init__() self.set_text(word) self.set_position( Position(randint(0, constants.MAX_X), randint(2, constants.MAX_Y - 1))) self.set_velocity(Position(1, 0)) # TODO: the score for each word should be based on the length of the word and # its velocity self._points = 0
def __init__(self): """ Class constructor. """ super().__init__() self._buffer = [] self._display_buffer = '-Buffer: ' self._text_buffer = '' self.set_position(Position(0, 20)) self.set_text(self._display_buffer)
def __eq__(self, other): if type(self) != type(other): return False if self.get_size() != other.get_size(): return False for row in range(self.get_size()): for col in range(self.get_size()): pos = Position(row, col) if self.get_notch(pos).get_colour() != other.get_notch(pos).get_colour(): return False return True
def get_rotation(self, analyser: BoardAnalyser) -> Rotation: rotation_instruction = """ Enter a string of length 3 - For example '001' - rotates top left block anticlockwise First couple of characters: The block to rotate Final character: 0 - anticlockwise, 1 clockwise """ move_string = get_string_of_length(3, rotation_instruction) return Rotation( block_pos=Position(int(move_string[0]), int(move_string[1])), clockwise=bool(int(move_string[2])) )
def test_move_error_for_non_extant_block(self): rotation_tests = [ Rotation(Position(100, 100), True), Rotation(Position(-1, 0), False), Rotation(Position(2, 1), True) ] placement_tests = [ Placement(Position(100, 100), Position(2, 2)), Placement(Position(-1, 0), Position(2, 2)), Placement(Position(2, 1), Position(2, 2)) ] for test in rotation_tests: with self.assertRaises(MoveError): self.test_board.rotate_block(test) for test in placement_tests: with self.assertRaises(MoveError): self.test_board.play_marble(test, 1)
def test_get_notch(self): tests = [ { 'pos': Position(0, 0), 'colour': None }, { 'pos': Position(1, 0), 'colour': 1 }, { 'pos': Position(1, 2), 'colour': 1 }, { 'pos': Position(2, 2), 'colour': 0 }, ] for test in tests: self.assertEqual( self.test_block.get_notch(test['pos']).get_colour(), test['colour'])
def test_move_error_for_non_extant_notch(self): placement_tests = [ Placement(Position(1, 1), Position(3, 2)), Placement(Position(0, 0), Position(-1, 2)), Placement(Position(0, 1), Position(2, -2)) ] for test in placement_tests: with self.assertRaises(MoveError): self.test_board.play_marble(test, 1)
def move_next(self): """Moves the actor to its next position according to its velocity. Will wrap the position from one side of the screen to the other when it reaches the boundary in either direction. Args: self (Actor): an instance of Actor. """ x1 = self._position.get_x() y1 = self._position.get_y() x2 = self._velocity.get_x() y2 = self._velocity.get_y() x = 1 + (x1 + x2 - 1) % (constants.MAX_X - 1) y = 1 + (y1 + y2 - 1) % (constants.MAX_Y - 1) position = Position(x, y) self._position = position
def get_middle_position_of_field(self, x, y): from_x, from_y = self.from_position.as_tuple() middle_x = from_x + x * self.field_size + self.field_size / 2 middle_y = from_y + y * self.field_size + self.field_size / 2 return Position(middle_x, middle_y)
def test_is_over(self): win_tests = [ { 'placement': Placement(Position(0, 0), Position(2, 0)), 'colour': 1, 'length': 5, 'win': True }, { 'placement': Placement(Position(0, 0), Position(2, 1)), 'colour': 1, 'length': 5, 'win': True }, { 'placement': Placement(Position(0, 1), Position(2, 0)), 'colour': 1, 'length': 5, 'win': True }, { 'placement': Placement(Position(1, 1), Position(2, 0)), 'colour': 1, 'length': 5, 'win': True }, { 'placement': Placement(Position(0, 0), Position(2, 0)), 'colour': 1, 'length': 6, 'win': False }, { 'placement': Placement(Position(0, 0), Position(2, 1)), 'colour': 1, 'length': 6, 'win': False }, { 'placement': Placement(Position(0, 1), Position(2, 0)), 'colour': 1, 'length': 6, 'win': False }, { 'placement': Placement(Position(1, 1), Position(2, 0)), 'colour': 1, 'length': 6, 'win': False }, { 'placement': Placement(Position(1, 1), Position(0, 0)), 'colour': 1, 'length': 5, 'win': False }, { 'placement': Placement(Position(1, 1), Position(1, 2)), 'colour': 1, 'length': 5, 'win': False }, { 'placement': Placement(Position(1, 1), Position(1, 2)), 'colour': 0, 'length': 5, 'win': False }, { 'placement': Placement(Position(0, 0), Position(2, 0)), 'colour': 0, 'length': 5, 'win': False }, ] for i, test in enumerate(win_tests): board = self.test_board.play_marble(test['placement'], test['colour']) self.assertEqual( BoardAnalyser(board).is_over([0, 1], test['length']), test['win'], "\n" + str(board) + f"\nTest {i}")
def get_empty_positions(self) -> List[Position]: return [Position(i, j) for i in range(self.get_size()) for j in range(self.get_size()) if self.__notches[i][j].is_empty()]
def test_get_empty_positions(self): self.assertEqual( self.test_block.get_empty_positions(), [Position(0, 0), Position(0, 1), Position(0, 2)])