def get_block(self, block_pos: Position) -> Block: if block_pos.get_row() < 0 or block_pos.get_column() < 0: raise MoveError('use positive notch numbers please') try: return self.__blocks[block_pos.get_row()][block_pos.get_column()] except IndexError: raise MoveError('block does not exist')
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 play(self, marble: Marble, pos: Position): new_notches = deepcopy(self.__notches) if pos.get_row() < 0 or pos.get_column() < 0: raise MoveError('use positive notch numbers please') try: new_notches[pos.get_row()][pos.get_column()] = new_notches[pos.get_row()][pos.get_column()].play(marble) except IndexError: raise MoveError('notch does not exist') return Block(new_notches)
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 __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 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 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_notch(self, pos: Position): return self.get_row(pos.get_row())[pos.get_column()]
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 __update_block(self, block_pos: Position, new_block: Block) -> 'Board': new_blocks = deepcopy(self.__blocks) new_blocks[block_pos.get_row()][block_pos.get_column()] = new_block return Board(new_blocks)
class Actor: """A visible, moveable thing that participates in the game. The responsibility of Actor is to keep track of its appearance, position and velocity in 2d space. Stereotype: Information Holder Attributes: _text (string): The textual representation of the actor. _position (Point): The actor's position in 2d space. _velocity (Point): The actor's speed and direction. """ 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 get_position(self): """Gets the actor's position in 2d space. Args: self (Actor): an instance of Actor. Returns: Point: The actor's position in 2d space. """ return self._position def get_text(self): """Gets the actor's textual representation. Args: self (Actor): an instance of Actor. Returns: string: The actor's textual representation. """ return self._text def get_velocity(self): """Gets the actor's speed and direction. Args: self (Actor): an instance of Actor. Returns: Point: The actor's speed and direction. """ return self._velocity 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 set_position(self, position): """Updates the actor's position to the given one. Args: self (Actor): An instance of Actor. position (Point): The given position. """ self._position = position def set_text(self, text): """Updates the actor's text to the given value. Args: self (Actor): An instance of Actor. text (string): The given value. """ self._text = text def set_velocity(self, velocity): """Updates the actor's velocity to the given one. Args: self (Actor): An instance of Actor. velocity (Point): The given velocity. """ self._velocity = velocity
def test_get_empty_positions(self): self.assertEqual( self.test_block.get_empty_positions(), [Position(0, 0), Position(0, 1), Position(0, 2)])
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)