Beispiel #1
0
def test_edges():
    panel = MapPanel(1, 1, 2, 2)
    frame = GridFrameBuffer(5, 5, init_value=" ")
    panel.add("1", (0, 0))
    panel.add("2", (1, 0))
    panel.add("3", (0, 1))
    panel.add("4", (1, 1))
    panel.redraw(frame)
    exp = ["     ", " 12  ", " 34  ", "     ", "     "]
    exp_frame = GridFrameBuffer.from_string_array(exp)
    assert frame == exp_frame
Beispiel #2
0
def test_edges():
    panel = Panel(1,
                  1,
                  1,
                  1,
                  border=PanelBorder.create("2", "7", "5", "4", "3", "1", "8",
                                            "6"))
    frame = GridFrameBuffer(5, 5, init_value=" ")
    panel.redraw(frame)
    exp = ["123  ", "4 5  ", "678  ", "     ", "     "]
    exp_frame = GridFrameBuffer.from_string_array(exp)
    assert frame == exp_frame
Beispiel #3
0
def test_from_string_array():
    frame1 = GridFrameBuffer(2, 2)
    frame1.set(0, 0, "a")
    frame1.set(1, 0, "b")
    frame1.set(0, 1, "c")
    frame1.set(1, 1, "d")
    frame2 = GridFrameBuffer.from_string_array(["ab", "cd"])
    assert frame1 == frame2
Beispiel #4
0
def test_edges_2():
    panel = Panel(0,
                  0,
                  1,
                  1,
                  border=PanelBorder.create("2", "7", "5", "4", "3", "1", "8",
                                            "6"))
    frame = GridFrameBuffer(5, 5, init_value=" ")
    with pytest.raises(IndexError):
        panel.redraw(frame)
Beispiel #5
0
def test_str_2x2():
    frame = GridFrameBuffer(2, 2)
    frame.set(0, 0, "a")
    frame.set(1, 0, "b")
    frame.set(0, 1, "c")
    frame.set(1, 1, "d")
    assert str(frame) == "ab\ncd"
Beispiel #6
0
    def run_with_local_display(self, seed=None):
        """Will run the game for a user.

        Returns:
        """
        # This import statement is here so pygame is only imported if needed.
        from CYLGame import Display
        game = self.game_class(random.Random(seed))

        charset = Display.CharSet(self.game_class.CHAR_SET,
                                  self.game_class.CHAR_WIDTH,
                                  self.game_class.CHAR_HEIGHT)
        display = Display.PyGameDisplay(*charset.char_size_to_pix(
            (game.SCREEN_WIDTH, game.SCREEN_HEIGHT)),
                                        title=game.GAME_TITLE)

        clock = Display.get_clock()

        frame_buffer = GridFrameBuffer(*charset.pix_size_to_char(
            display.get_size()),
                                       charset=charset)
        frame_updated = True

        game.init_board()
        players = []
        if game.MULTIPLAYER:
            computer_bot_class = game.default_prog_for_computer()
            for _ in range(game.get_number_of_players() - 1):
                players += [game.create_new_player(computer_bot_class())]
        player = game.create_new_player(UserProg())

        game.start_game()

        while game.is_running():
            clock.tick(FPS)
            for key in display.get_keys():
                # TODO: fix
                player.prog.key = key
                player.run_turn(game.random)
                for comp_player in players:
                    comp_player.run_turn(game.random)
                game.do_turn()
                frame_updated = True

            if frame_updated:
                game.draw_screen(frame_buffer)
                display.update(frame_buffer)
                frame_updated = False
Beispiel #7
0
def test_neq_2x2():
    frame1 = GridFrameBuffer(2, 2)
    frame1.set(0, 0, "a")
    frame1.set(1, 0, "b")
    frame1.set(0, 1, "c")
    frame1.set(1, 1, "d")
    frame2 = GridFrameBuffer(2, 2)
    frame2.set(0, 0, "d")
    frame2.set(1, 0, "b")
    frame2.set(0, 1, "c")
    frame2.set(1, 1, "d")
    assert frame1 != frame2
Beispiel #8
0
def test_str_1x1():
    frame = GridFrameBuffer(1, 1)
    frame.set(0, 0, "a")
    assert str(frame) == "a"
Beispiel #9
0
def test_neq_1x1():
    frame1 = GridFrameBuffer(1, 1)
    frame1.set(0, 0, "a")
    frame2 = GridFrameBuffer(1, 1)
    frame2.set(0, 0, "b")
    assert frame1 != frame2
Beispiel #10
0
 def get_frame(self):
     if self.__frame_buffer is None:
         self.__frame_buffer = GridFrameBuffer(self.SCREEN_WIDTH,
                                               self.SCREEN_HEIGHT)
     self.draw_screen(self.__frame_buffer)
     return self.__frame_buffer.dump()
Beispiel #11
0
class GridGame(Game):
    WEBONLY = False
    GRID = True
    SCREEN_WIDTH = 80
    SCREEN_HEIGHT = 25
    CHAR_WIDTH = 8
    CHAR_HEIGHT = 8
    GAME_TITLE = ""
    CHAR_SET = data_file("fonts/terminal8x8_gs_ro.png")
    __frame_buffer = None

    def is_running(self):
        """This is how the game runner knows if the game is over.

        Returns:
            bool: True if the game should still be play. False otherwise
        """
        raise Exception("Not implemented!")

    def create_new_player(
            self,
            prog):  # TODO: add option to create computer, user or bot players.
        """ TODO: write this
        """
        raise Exception("Not Implemented!")

    def do_turn(self):
        """This function should read the new state of all the players and react to them."""
        raise Exception("Not implemented!")

    def draw_screen(self, frame_buffer):
        """WARNING: There MUST NOT be any game logic in this function since it isn't called when simulating the game
                    during the competitions.
        """
        raise Exception("Not implemented!")

    def get_frame(self):
        if self.__frame_buffer is None:
            self.__frame_buffer = GridFrameBuffer(self.SCREEN_WIDTH,
                                                  self.SCREEN_HEIGHT)
        self.draw_screen(self.__frame_buffer)
        return self.__frame_buffer.dump()

    def get_vars(self, player):
        """ TODO: write this
        """
        raise Exception("Not implemented!")

    def get_score(self):
        """This is the game runner gets the ending or mid-game score

        Returns:
            int: The current score.
        """
        raise Exception("Not implemented!")

    @staticmethod
    def default_prog_for_bot(language):
        raise Exception("Not implemented!")

    @staticmethod
    def get_intro():
        raise Exception("Not implemented!")

    @staticmethod
    def get_move_consts():
        return ConstMapping({
            "north": ord("w"),
            "south": ord("s"),
            "west": ord("a"),
            "east": ord("d"),
            "northeast": ord("e"),
            "southeast": ord("c"),
            "northwest": ord("q"),
            "southwest": ord("z")
        })