def test_djn_f_not_zero(): warrior = Warrior(processes=[0]) core = Core(data=[ DJN('B', '$', 2, '$', 0), DAT('F', '$', 0, '$', 1), DAT('F', '$', 0, '$', 1) ]) game = Round(core=core, warriors=[warrior], init_warriors=False) game.simulation_step() assert warrior.processes() == [2]
def test_mod_zero(): warrior = Warrior(processes=[0]) core = Core(data=[ MOD('I', '$', 1, '$', 2), DAT('F', '$', 0, '$', 0), DAT('F', '$', 0, '$', 0) ]) game = Round(core=core, warriors=[warrior], init_warriors=False) game.simulation_step() assert len(warrior.processes()) == 0
def test_seq_i_no(): warrior = Warrior(processes=[0]) core = Core(data=[ SEQ('I', '$', 1, '$', 2), JMP('F', '$', 2, '$', 4), DAT('F', '$', 2, '$', 4) ]) game = Round(core=core, warriors=[warrior], init_warriors=False) game.simulation_step() assert warrior.processes()[0] == 1
def test_spl_typical(): warrior = Warrior(processes=[0]) core = Core(data=[ SPL('F', '$', 2, '$', 2), DAT('F', '$', 0, '$', 0), DAT('F', '$', 0, '$', 0) ]) game = Round(core=core, warriors=[warrior], init_warriors=False) game.simulation_step() assert warrior.processes() == [1, 2]
def test_post_decrement_a(): warrior = Warrior(processes=[0]) core = Core(data=[MOV('I', '}', 1, '$', 1), DAT('F', '$', 0, '$', 0)]) game = Round(core=core, warriors=[warrior], init_warriors=False) game.simulation_step() assert core[1].a_value() == 0 assert core[1].b_value() == 0
def test_sub_typical(): warrior = Warrior(processes=[0]) core = Core( data=[SUB('AB', '#', '3', '$', '1'), DAT('F', '$', 1, '$', -5)]) game = Round(core=core, warriors=[warrior], init_warriors=False) game.simulation_step() assert game.core()[1].b_value() == -8
def test_sub_x(): warrior = Warrior(processes=[0]) core = Core(data=[SUB('X', '#', '3', '$', '1'), DAT('F', '$', 2, '$', -5)]) game = Round(core=core, warriors=[warrior], init_warriors=False) game.simulation_step() instruction = game.core()[1] assert instruction.a_value() == 1 assert instruction.b_value() == -8
def test_core_add_cycle_begin_two_times(): warrior = Warrior(processes=[1]) core = Core(data=[ DAT('F', '$', 1, '$', 5), ADD('AB', '#', '3', '$', '-5'), DAT('F', '$', 1, '$', -5) ]) game = Round(core=core, warriors=[warrior], init_warriors=False) game.simulation_step() assert game.core()[2].b_value() == -2
def test_jmp_typical(): warrior = Warrior(processes=[2]) core = Core(data=[ DAT('F', '$', 1, '$', -5), DAT('F', '$', 1, '$', -5), JMP('B', '$', -1, '$', 0) ]) game = Round(core=core, warriors=[warrior], init_warriors=False) game.simulation_step() assert game.warriors()[0].processes()[0] == 1
def test_mov_x(): warrior = Warrior(processes=[0]) core = Core(data=[ MOV('X', '$', '1', '$', '2'), DAT('F', '$', 1, '$', 7), DAT('F', '$', 0, '$', 0) ]) game = Round(core=core, warriors=[warrior], init_warriors=False) game.simulation_step() assert game.core()[2].a_value() == 7 assert game.core()[2].b_value() == 1
def parse_warrior(file_handle): """ Parses warrior instructions from file_handle :param file_handle: file_handle stream :return: warrior object """ instructions = [] name = None for line_number, line in enumerate(file_handle): line = line.strip() if line: if line.startswith(';'): # It is a comment # Try to parse it as name comment parsed_name = try_parse_name(line) if parsed_name: # Set warrior name name = parsed_name else: # It isn't a comment and should be parsed as instruction instruction = parse_instruction(line, line_number) instructions.append(instruction) return Warrior(instructions, name=name)
def test_play_game_ok(): warrior_a = Warrior([MOV("I", "#", 1, "}", 0)]) warrior_b = Warrior([MOV("I", "#", 1, "}", 0)]) game = Game([warrior_a, warrior_b], max_cycles=20) game.play()
def test_game_results(): warrior_a = Warrior([MOV("I", "#", 1, "}", 0)]) warrior_b = Warrior([MOV("I", "#", 1, "}", 0)]) game = Game([warrior_a, warrior_b], max_cycles=20) game.get_results_string()
def main(iterations=1): running_count = 0 running_warrior1_wins = 0 running_warrior2_wins = 0 running_warrior1_game_wins = 0 running_warrior2_game_wins = 0 for i in range(0, iterations): warrior1 = Warrior() warrior2 = Warrior() deck = Deck() deck.shuffle() deck.shuffle() #################### # Deal out hands #################### i = 0 for card in deck.deck: if i % 2 == 0: warrior1.add_cards([card]) else: warrior2.add_cards([card]) i += 1 #################### # Play the game #################### count = 0 warrior1_wins = 0 warrior2_wins = 0 warrior1_game_wins = 0 warrior2_game_wins = 0 while True: if int(warrior1.get_queue_size()) == 0: print("WINNER Warrior 2") warrior2_game_wins += 1 break if int(warrior2.get_queue_size()) == 0: print("WINNER Warrior 1") warrior1_game_wins += 1 break cards = None war1_card = warrior1.get_card() # print(war1_card) war2_card = warrior2.get_card() cards = [war1_card, war2_card] # print(war2_card) print( f"warrior1 [{war1_card.name}-{war1_card.suit}-{war1_card.power}]" f" warrior2 [{war2_card.name}-{war2_card.suit}-{war2_card.power}]" ) if war1_card.power > war2_card.power: print("Warrior 1 sweeps") warrior1_wins += 1 warrior1.add_cards(cards) elif war1_card.power < war2_card.power: print("Warrior 2 sweeps") warrior2_wins += 1 warrior2.add_cards(cards) else: print("WAR") while True: warror1_len = int(warrior1.get_queue_size()) warrior2_len = int(warrior2.get_queue_size()) min_len = min(warror1_len, warrior2_len) if min_len == 0: if warror1_len == 0: warrior2.add_cards(cards) break if warrior2_len == 0: warrior1.add_cards(cards) break print(min_len) if min_len > 3: r = 3 elif min_len == 3: r = 2 elif min_len == 2: r = 1 else: r = 0 for i in range(0, r): cards.append(warrior1.get_card()) cards.append(warrior2.get_card()) war1_war = warrior1.get_card() cards.append(war1_war) war2_war = warrior2.get_card() cards.append(war2_war) if war1_war.power > war2_war.power: print("Warrior 1 sweeps") warrior1_wins += 1 warrior1.add_cards(cards) break elif war1_war.power < war2_war.power: print("Warrior 2 sweeps") warrior2_wins += 1 warrior2.add_cards(cards) break else: print("WAR AGAIN") count += 1 print( f"warrior1 hand: {warrior1.get_queue_size()} warrior2 hand: {warrior2.get_queue_size()}" ) if int(warrior1.get_queue_size()) + int( warrior2.get_queue_size()) != 52: print("Invalid deck size") break warrior1_list = warrior1.queue_to_list() warrior2_list = warrior2.queue_to_list() warrior1_list.sort(key=lambda x: x.power, reverse=True) warrior2_list.sort(key=lambda x: x.power, reverse=True) if len(warrior1_list) != 0: for item in warrior1_list: print(item) if len(warrior2_list) != 0: for item in warrior1_list: print(item) print("------------------------------------------------") print(f"Iterations: {count}") print(f"Warrior1 Wins: {warrior1_wins}") print(f"Warrior2 Wins: {warrior2_wins}") print("------------------------------------------------") running_count += count running_warrior1_wins += warrior1_wins running_warrior2_wins += warrior2_wins running_warrior1_game_wins += warrior1_game_wins running_warrior2_game_wins += warrior2_game_wins print("*********************************************************") print(f"Total iterations: {running_count}") print(f"Total Warrior1 Game Wins: {running_warrior1_game_wins}") print(f"Total Warrior2 Game Wins: {running_warrior2_game_wins}") print(f"Total Warrior1 Wins: {running_warrior1_wins}") print(f"Total Warrior2 Wins: {running_warrior2_wins}") print("*********************************************************")
def test_round_without_gui(): warrior_a = Warrior([MOV("I", "#", 1, "}", 0)]) warrior_b = Warrior([MOV("I", "#", 1, "}", 0)]) round_obj = Round([warrior_a, warrior_b]) round_obj.play()
def test_core_postincrement_b(): warrior = Warrior(processes=[0]) core = Core(data=[DAT('F', '>', 1, '$', 1), DAT('F', '$', 0, '$', 0)]) game = Round(core=core, warriors=[warrior], init_warriors=False) game.simulation_step() assert game.core()[1].b_value() == 1
def test_has_alive_warriors_false(): warrior_a = Warrior(processes=[]) warrior_b = Warrior(processes=[]) game = Round([warrior_a, warrior_b], core_size=2) assert game.get_alive_warriors()