# Buggy code
def main():
    """Add all available states to our scene manager (tools.Control)
    and start the game using the pygame interface.

    :rtype: None
    :returns: None

    """
    import pygame
    from .control import PygameControl

    prepare.init()
    control = PygameControl(prepare.ORIGINAL_CAPTION)
    control.auto_state_discovery()

    # background state is used to prevent other states from
    # being required to track dirty screen areas.  for example,
    # in the start state, there is a menu on a blank background,
    # since menus do not clean up dirty areas, the blank,
    # "Background state" will do that.  The alternative is creating
    # a system for states to clean up their dirty screen areas.
    control.push_state("BackgroundState")

    # basically the main menu
    control.push_state("StartState")

    # Show the splash screen if it is enabled in the game configuration
    if prepare.CONFIG.splash == "1":
        control.push_state("SplashState")
        control.push_state("FadeInTransition")

    # block of code useful for testing
    if 0:
        import random
        from core.components.event.actions.player import Player
        from core.components.technique import Technique

        # TODO: fix this player/player1 issue
        control.player1 = prepare.player1

        add_monster = partial(adapter("add_monster"))
        Player().add_monster(control, add_monster("Bigfin", 10))
        Player().add_monster(control, add_monster("Dollfin", 10))
        Player().add_monster(control, add_monster("Rockitten", 10))
        Player().add_monster(control, add_monster("Nut", 10))
        Player().add_monster(control, add_monster("Sumobug", 10))

        add_item = partial(adapter("add_item"))
        Player().add_item(control, add_item("Potion", 1))
        Player().add_item(control, add_item("Super Potion", 1))
        Player().add_item(control, add_item("Capture Device", 1))

        for monster in control.player1.monsters:
            monster.hp = 100
            monster.current_hp = 1
            # monster.current_hp = random.randint(1, 2)
            monster.apply_status(Technique("Poison"))

        # control.push_state("MonsterMenuState")

        from core.components.event.actions.combat import Combat

        start_battle = partial(adapter("random_encounter"))
        Combat().random_encounter(control, start_battle(1))

    control.main()
    pygame.quit()


# Traceback (KeyError)
Traceback (most recent call last):
File "./tuxemon.py", line 60, in <module>
main()
File "/tmp/Tuxemon/tuxemon/core/main.py", line 109, in main
control.main()
File "/tmp/Tuxemon/tuxemon/core/control.py", line 512, in main
self.main_loop()
File "/tmp/Tuxemon/tuxemon/core/control.py", line 573, in main_loop
self.update(time_delta)
File "/tmp/Tuxemon/tuxemon/core/control.py", line 203, in update
state.update(dt)
File "/tmp/Tuxemon/tuxemon/core/states/combat/combat.py", line 156, in update
self.update_phase()
File "/tmp/Tuxemon/tuxemon/core/states/combat/combat.py", line 328, in update_phase
self.handle_action_queue()
File "/tmp/Tuxemon/tuxemon/core/states/combat/combat.py", line 338, in handle_action_queue
self.check_party_hp()
File "/tmp/Tuxemon/tuxemon/core/states/combat/combat.py", line 614, in check_party_hp
self.faint_monster(monster)
File "/tmp/Tuxemon/tuxemon/core/states/combat/combat.py", line 581, in faint_monster
awarded_exp = monster.total_experience/monster.level/len(self._damage_map[monster])
KeyError: <core.components.monster.Monster object at 0x7fb24236ddd0>

# Fixed code
def main():
    """Add all available states to our scene manager (tools.Control)
    and start the game using the pygame interface.

    :rtype: None
    :returns: None

    """
    import pygame
    from .control import PygameControl

    prepare.init()
    control = PygameControl(prepare.ORIGINAL_CAPTION)
    control.auto_state_discovery()

    # background state is used to prevent other states from
    # being required to track dirty screen areas.  for example,
    # in the start state, there is a menu on a blank background,
    # since menus do not clean up dirty areas, the blank,
    # "Background state" will do that.  The alternative is creating
    # a system for states to clean up their dirty screen areas.
    control.push_state("BackgroundState")

    # basically the main menu
    control.push_state("StartState")

    # Show the splash screen if it is enabled in the game configuration
    if prepare.CONFIG.splash == "1":
        control.push_state("SplashState")
        control.push_state("FadeInTransition")

    # block of code useful for testing
    if 0:
        import random
        from core.components.event.actions.player import Player
        from core.components.technique import Technique

        # TODO: fix this player/player1 issue
        control.player1 = prepare.player1

        add_monster = partial(adapter("add_monster"))
        Player().add_monster(control, add_monster("Bigfin", 10))
        Player().add_monster(control, add_monster("Dollfin", 10))
        Player().add_monster(control, add_monster("Rockitten", 10))
        Player().add_monster(control, add_monster("Nut", 10))
        Player().add_monster(control, add_monster("Sumobug", 10))

        add_item = partial(adapter("add_item"))
        Player().add_item(control, add_item("Potion", 1))
        Player().add_item(control, add_item("Super Potion", 1))
        Player().add_item(control, add_item("Capture Device", 1))

        for monster in control.player1.monsters:
            monster.hp = 100
            monster.current_hp = 1
            # monster.current_hp = random.randint(1, 2)
            monster.apply_status(Technique("status_poison"))

        # control.push_state("MonsterMenuState")

        from core.components.event.actions.combat import Combat

        start_battle = partial(adapter("random_encounter"))
        Combat().random_encounter(control, start_battle(1))

    control.main()
    pygame.quit()