Exemplo n.º 1
0
    def test_robot_behaviour(self):

        for entry in TestRobot.test_behaviour_table:
            if entry["goal"] is None and entry["solvable"]:
                goal = (len(entry["terrain"]) - 1, len(entry["terrain"]) - 1)
            elif entry["goal"] is None:
                goal = (0, 0)
            else:
                goal = entry["goal"]

            battleground = Battleground(terrain=entry["terrain"])
            robot = Robot(battleground,
                          search=Robot.SEARCH_DFS,
                          goal=entry["goal"])
            self.assertEqual(robot.run(), entry["solvable"])
            self.assertEqual(goal, (robot.x, robot.y))

            battleground = Battleground(terrain=entry["terrain"])
            robot = Robot(battleground,
                          search=Robot.SEARCH_FOLLOW_LEFT,
                          goal=entry["goal"])
            self.assertEqual(robot.run(), entry["solvable"])
            self.assertEqual(robot.moves(), entry["moves_left"])
            self.assertEqual(goal, (robot.x, robot.y))

            battleground = Battleground(terrain=entry["terrain"])
            robot = Robot(battleground,
                          search=Robot.SEARCH_FOLLOW_RIGHT,
                          goal=entry["goal"])
            self.assertEqual(robot.run(), entry["solvable"])
            self.assertEqual(robot.moves(), entry["moves_right"])
            self.assertEqual(goal, (robot.x, robot.y))
Exemplo n.º 2
0
    def test_robot_output(self):

        for entry in TestRobot.test_output_table:
            with redirect_stdout() as file:
                battleground = Battleground(terrain=entry["terrain"])
                robot = Robot(battleground)
                print(robot)
            with open(file, 'r') as f:
                self.assertEqual(f.read().strip(), entry["repr"].strip())

            with redirect_stdout() as file:
                battleground = Battleground(terrain=entry["terrain"])
                robot = Robot(battleground, display=Robot.DISPLAY_COLOR)
                robot.render()
            with open(file, 'r') as f:
                self.assertEqual(f.read().strip(), entry["repr"].strip())
Exemplo n.º 3
0
 def start_battle_thread(self, generals):
     battleground = Battleground(BG_WIDTH, BG_HEIGHT)
     battleground.generals = generals
     scenario_pos = []
     for g in generals:
         scenario_pos.append((g.x, g.y))
         g.change_battleground(battleground, 3 if g.side == 0 else 56, g.y)
     battle = BattleWindow(battleground, 0)
     winner = battle.loop()
     for i in [0, 1]:
         generals[i].change_battleground(self.bg, *(scenario_pos[i]))
         if not generals[i].alive:
             generals[i].die()
             generals[i].start_scenario()
             self.message(
                 generals[(i + 1) % 2].name + " defeated " + generals[i].name + " on a battle.",
                 generals[(i + 1) % 2].color,
             )
Exemplo n.º 4
0
 def start_battle_thread(self, generals):
     battleground = Battleground(BG_WIDTH, BG_HEIGHT)
     battleground.generals = generals
     scenario_pos = []
     for g in generals:
         scenario_pos.append((g.x, g.y))
         g.change_battleground(battleground, 3 if g.side == 0 else 56, g.y)
     battle = BattleWindow(battleground, 0)
     winner = battle.loop()
     for i in [0, 1]:
         generals[i].change_battleground(self.bg, *(scenario_pos[i]))
         if not generals[i].alive:
             generals[i].die()
             generals[i].start_scenario()
             self.message(
                 generals[(i + 1) % 2].name + " defeated " +
                 generals[i].name + " on a battle.",
                 generals[(i + 1) % 2].color)
Exemplo n.º 5
0
    def test_initialization(self):
        for entry in TestBattleground.test_init_table:
            exception = None
            try:
                if entry["from"] == "view":
                    battleground = Battleground(view=entry["view"])
                    self.assertEqual(battleground.fog, entry["fog"])
                    self.assertEqual(battleground.terrain, entry["terrain"])
                elif entry["from"] == "fog+terrain":
                    battleground = Battleground(terrain=entry["terrain"],
                                                fog=entry["fog"])
                    self.assertEqual(battleground.get_view(), entry["view"])
                elif entry["from"] == "dimensions":
                    battleground = Battleground(m=entry["m"], n=entry["n"])
                    view = battleground.get_view()
                    self.assertEqual(len(view), entry["m"])
                    for column in view:
                        self.assertEqual(len(column), entry["n"])
                    if entry.get("fog") is not None:
                        self.assertEqual(battleground.fog, entry["fog"])
                elif entry["from"] == "nothing":
                    battleground = Battleground()
                else:
                    raise RuntimeError("invalid test case")

                if entry.get("repr") is not None:
                    with redirect_stdout() as file:
                        print(battleground)
                    with open(file, 'r') as f:
                        self.assertEqual(f.read().strip(),
                                         entry["repr"].strip())

            except Exception as e:
                if e.__class__ != entry.get("exception").__class__:
                    raise e
                exception = e
            finally:
                self.assertEqual(
                    entry.get("exception").__class__, exception.__class__)
Exemplo n.º 6
0
 def run(cls):
     pokemon_output = cls._get_pokemon_output()
     protagonist = cls._get_protagonist(pokemon_output)
     antagonist = cls._get_antagonist(pokemon_output)
     battleground = Battleground(protagonist, antagonist)
     battleground.battle()
Exemplo n.º 7
0
                        libtcod.dark_blue, libtcod.sky, libtcod.black)
      line += 2

  def render_tactics(self, i):
    bar_offset_x = 3
    line = 7 + len(self.bg.generals[i].skills)*2
    for s in range(0, len(self.bg.generals[i].tactics)):
      libtcod.console_set_default_foreground(self.con_panels[i],
        libtcod.red if self.bg.generals[i].tactics[s] == self.bg.generals[i].selected_tactic else libtcod.white)
      libtcod.console_print(self.con_panels[i], bar_offset_x, line, KEYMAP_TACTICS[s] + ": " + self.bg.generals[i].tactic_quotes[s])
      line += 2
    return line

from factions import doto
if __name__=="__main__":
  bg = Battleground(BG_WIDTH, BG_HEIGHT)
  bg.generals = [doto.Pock(bg, 0, 3, 21), doto.Pock(bg, 1, 56, 21)]
  for i in [0,1]:
    bg.reserves[i] = [doto.Rubock(bg, i), doto.Bloodrotter(bg, i), doto.Ox(bg, i)]
  for i in [0,1]:
    bg.generals[i].start_scenario()
    for g in bg.reserves[i]:
      g.start_scenario()

  if len(sys.argv) == 4: 
    battle = BattleWindow(bg, int(sys.argv[1]), sys.argv[2], int(sys.argv[3]))
  elif len(sys.argv) == 2:
    battle = BattleWindow(bg, int(sys.argv[1]))
  else:
    battle = BattleWindow(bg, 0)
  battle.loop()
Exemplo n.º 8
0
                        help="randomly generate terrain",
                        action="store_true")
parser.add_argument("-o",
                    "--output",
                    help="which output method should be used",
                    action="store",
                    choices=display_choices,
                    default="curses_color")
parser.add_argument("-s",
                    "--search",
                    help="which search to perform",
                    action="store",
                    choices=search_choices,
                    default="follow_left")

args = parser.parse_args()
search = Robot.__dict__["SEARCH_" + args.search.upper()]
output = Robot.__dict__["DISPLAY_" + args.output.upper()]

if args.random:
    ground = Battleground(m=8, n=9, hill_rates=[0.1, 0.03, 0.03])
else:
    if args.file is not None:
        view = battleground_from_file(args.file)
    ground = Battleground(view=view)

robot = Robot(ground, search=search, display=output)
if not robot.run():
    print("Terrain unsolvable")
print("Moves: " + str(robot.moves()))
Exemplo n.º 9
0
        line = 7 + len(self.bg.generals[i].skills) * 2
        for s in range(0, len(self.bg.generals[i].tactics)):
            fg_color = libtcod.red if self.bg.generals[i].tactics[
                s] == self.bg.generals[i].selected_tactic else libtcod.white
            self.con_panels[i].print(bar_offset_x,
                                     line,
                                     KEYMAP_TACTICS[s] + ": " +
                                     self.bg.generals[i].tactic_quotes[s],
                                     fg=fg_color)
            line += 2
        return line


from factions import doto
if __name__ == "__main__":
    bg = Battleground(BG_WIDTH, BG_HEIGHT)
    bg.generals = [doto.Pock(bg, 0, 3, 21), doto.Pock(bg, 1, 56, 21)]
    for i in [0, 1]:
        bg.reserves[i] = [
            doto.Rubock(bg, i),
            doto.Bloodrotter(bg, i),
            doto.Ox(bg, i)
        ]
    for i in [0, 1]:
        bg.generals[i].start_scenario()
        for g in bg.reserves[i]:
            g.start_scenario()

    if len(sys.argv) == 4:
        battle = BattleWindow(bg, int(sys.argv[1]), sys.argv[2],
                              int(sys.argv[3]))
Exemplo n.º 10
0
                continue
            enemy = g.enemy_reachable(diagonals=True)
            if enemy is not None and enemy.side != NEUTRAL_SIDE:
                if enemy in self.bg.fortresses:
                    if enemy.guests:
                        e = enemy.guests[-1]
                        self.start_battle([g, e])
                        if not e.alive:
                            enemy.unhost(e)
                else:
                    self.start_battle([g, enemy])
            else:
                t = self.get_next_tile(g)
                if t:
                    entity = self.bg.tiles[t].entity
                    if entity in self.bg.fortresses:
                        entity.host(g)
                    elif g.can_move(t[0] - g.x, t[1] - g.y):
                        g.move(t[0] - g.x, t[1] - g.y)
                    else:
                        g.target = g.home


if __name__ == "__main__":
    battleground = Battleground(BG_WIDTH, BG_HEIGHT, "map.txt")
    factions = []
    factions.append(Mechanics(battleground, 0))
    factions.append(Oracles(battleground, 1))
    scenario = Scenario(battleground, 0, factions)
    scenario.loop()
Exemplo n.º 11
0
game_count = 10

try:
    if (len(sys.argv) == 4):
        gladiator_count = int(sys.argv[1])
        arena_size = int(sys.argv[2])
        game_count = int(sys.argv[3])
except Exception as e:
    print(e)
    print("Invalied arguments. Default parameters will be set.")
    sleep(2)

ui = UI(space_length)

for i in range(game_count):
    battleground = Battleground(arena_size, arena_size)

    gladiators = []

    for i in range(gladiator_count):
        gladiators.append(Gladiator(battleground,ascii_uppercase[i],random.randint(1,3),random.randint(1,5),100))

    while True:
        os.system("cls")

        for gladiator in gladiators:
            if gladiator in battleground.gladiators:
                if len(battleground.gladiators) > 1:
                    gladiator.play()

        ui.show_matrix(battleground.get_matrix())
Exemplo n.º 12
0
 def setUp(self):
     self.battleground = Battleground(DummyCreature(), DummyCreature())
Exemplo n.º 13
0
class TestBattleGround(TestCase):
    def setUp(self):
        self.battleground = Battleground(DummyCreature(), DummyCreature())

    def _kill_antagonist(self):
        self.battleground._antagonist.is_fainted = True

    def test__calculate_damage(self):
        attacker, defender, effectiveness_multiplier = Mock(), Mock(), Mock()
        attacker.attack = 8
        defender.defense = 2
        effectiveness_multiplier.value = 0.5
        expected_value = Battleground.BASE_DAMAGE * 2
        result = self.battleground._calculate_damage(attacker, defender,
                                                     effectiveness_multiplier)
        self.assertEqual(expected_value, result)

    def test__get_effectiveness_multiplyer(self):
        bigger, lower = Mock(), Mock()
        bigger.affinity = 2
        lower.affinity = 1
        self.assertEqual(
            2,
            self.battleground._get_effectiveness_multiplyer(bigger,
                                                            lower).value)
        self.assertEqual(
            0.5,
            self.battleground._get_effectiveness_multiplyer(lower,
                                                            bigger).value)
        self.assertEqual(
            1,
            self.battleground._get_effectiveness_multiplyer(bigger,
                                                            bigger).value)

    def test__play_a_turn(self):
        fighter1, fighter2 = DummyCreature("a"), DummyCreature("b")
        self.battleground._get_effectiveness_multiplyer = Mock()
        self.battleground._calculate_damage = Mock(return_value=10)
        self.battleground._fighters = [fighter1, fighter2]
        self.battleground._play_a_turn()
        self.battleground._get_effectiveness_multiplyer.assert_called_once()
        self.battleground._calculate_damage.assert_called_once()
        self.assertEqual(90, fighter2.current_health)
        self.assertEqual([fighter2, fighter1], self.battleground._fighters)

    def test_battle(self):
        self.battleground._logger.info = Mock()
        self.battleground._play_a_turn = self._kill_antagonist
        self.battleground._announce_result = Mock()
        self.battleground.battle()
        self.battleground._announce_result.assert_called_once()

    def test__announce_result(self):
        self.battleground._logger.info = Mock()
        self.battleground._announce_result()
        self.battleground._logger.info.assert_called_with("Player won!")
        self.battleground._fighters.append(self.battleground._fighters.pop(0))
        self.battleground._announce_result()
        self.battleground._logger.info.assert_called_with("Opponent won!")