def test_fills(self):
        """create a game with some tunnels and chambers already in"""
        gamestate = makeNewGame()
        action = ['dig_tunnel', 'chamber1', None]
        gamestate = doAction(gamestate, action)
        action = ['dig_chamber', 'chamber1']
        gamestate = doAction(gamestate, action)
        action = ['dig_tunnel', 'chamber2', None]
        gamestate = doAction(gamestate, action)
        """do the fills"""
        action = ['fill_tunnel', 'chamber2']
        gamestate = doAction(gamestate, action)
        self.assertEqual(False,
                         gamestate['chambers']['chamber2']['tunnels']['exit'])

        action = ['fill_chamber', 'chamber2']
        gamestate = doAction(gamestate, action)
        self.assertEqual(2, len(gamestate['chambers'].keys()))
        """game state shouldn't change if I remove a nonexistent tunnel"""
        action = ['fill_tunnel', 'chamber3']
        with self.assertRaises(ValueError):
            doAction(gamestate, action)
        """game state shouldn't change if I remove a nonexistent chamber"""
        action = ['fill_chamber', 'chamber3']
        with self.assertRaises(ValueError):
            doAction(gamestate, action)
Example #2
0
 def testWin(self):
     startergraph = makeNewGame()
     starterboard = {'graph': startergraph, 'queen_at_head': True}
     """shouldn't be able to win at the start"""
     self.assertEqual(win_state_llist(starterboard), False)
     action = ['dig_chamber', None]
     startergraph = doAction(startergraph, action)
     action = ['dig_tunnel', 'chamber1', None]
     startergraph = doAction(startergraph, action)
     for i in range(2, 11):
         action = ['dig_chamber', 'chamber' + str(i - 1)]
         startergraph = doAction(startergraph, action)
         action = ['dig_tunnel', 'chamber' + str(i), None]
         startergraph = doAction(startergraph, action)
     """Even with 10 chambers, this shouldn't work because they have no food"""
     longboard = starterboard = {'graph': startergraph, 'queen_at_head': True}
     self.assertEqual(win_state_llist(longboard), False)
     for i in range(1, 11):
         chamber = 'chamber' + str(i)
         startergraph['food'][chamber]['crumb'] = 3
     """Now we should be able to win"""
     winboard = {'graph': startergraph, 'queen_at_head': True}
     self.assertEqual(win_state_llist(winboard), True)
     """but if we make one of the chambers under attack, it will be impossible again"""
     startergraph['under_attack']['chamber1'] = True
     winboard = {'graph': startergraph, 'queen_at_head': True}
     self.assertEqual(win_state_llist(winboard), False)
Example #3
0
 def testLose(self):
     startergraph = makeNewGame()
     starterboard = {'graph': startergraph, 'queen_at_head': True}
     losingboard = {'graph': startergraph, 'queen_at_head': False}
     """shouldn't be able to lose at the start"""
     self.assertEqual(lose_state_llist(starterboard), False)
     """unless the queen gets yeeted somehow"""
     self.assertEqual(lose_state_llist(losingboard), True)
     action = ['dig_chamber', None]
     startergraph = doAction(startergraph, action)
     action = ['dig_tunnel', 'chamber1', None]
     startergraph = doAction(startergraph, action)
     action = ['dig_chamber', 'chamber1']
     startergraph = doAction(startergraph, action)
     """our chambers are connected so this won't cause a lose"""
     tunnelboard = {'graph': startergraph, 'queen_at_head': True}
     self.assertEqual(lose_state_llist(starterboard), False)
     """but now they will!"""
     startergraph['tunnels']['chamber1'][1][0] = None
     losetunnelboard = {'graph': startergraph, 'queen_at_head': True}
     self.assertEqual(lose_state_llist(losetunnelboard), True)
     startergraph['tunnels']['chamber1'][1][0] = 'Head'
     "should work for both top chamber and the connection from c1 to c2"
     action = ['fill_tunnel', 'chamber1']
     losebynoconnection = doAction(startergraph, action)
     self.assertEqual(lose_state_llist({'graph': losebynoconnection, 'queen_at_head': True}), True)
 def test_ants(self):
     gamestate = makeNewGame()
     action = ['spawn_ant']
     gamestate = doAction(gamestate, action)
     self.assertEqual(2, gamestate['chambers']['surface']['num_ants'])
     action = ['move_ant', 'A1', 'chamber1']
     gamestate = doAction(gamestate, action)
     self.assertEqual(1, gamestate['chambers']['chamber1']['num_ants'])
 def test_initialize(self):
     gamestate = makeNewGame()
     keys = ['chambers', 'ants']
     starter_chambers = ['surface', 'chamber1']
     chamber_keys = ['num_ants', 'tunnels', 'food', 'under_attack']
     for key in keys:
         self.assertIn(key, gamestate)
     for chamber in starter_chambers:
         self.assertIn(chamber, gamestate['chambers'])
         for chamber_key in chamber_keys:
             self.assertIn(chamber_key, gamestate['chambers'][chamber])
 def test_inserts(self):
     """test some inserts, make sure they produce intended behavior"""
     gamestate = makeNewGame()
     action = ('dig_tunnel', 'chamber1', None)
     gamestate = doAction(gamestate, action)
     self.assertEqual(True,
                      gamestate['chambers']['chamber1']['tunnels']['exit'])
     action = ('dig_chamber', 'chamber1')
     gamestate = doAction(gamestate, action)
     self.assertIn('chamber2', gamestate['chambers'])
     """game state shouldn't change if I add a chamber connected to a nonexistent one"""
     action = ('dig_chamber', 'chamber4')
     with self.assertRaises(ValueError):
         doAction(gamestate, action)
     """or if I add a tunnel to/from a nonexistent chamber"""
     action = ('dig_tunnel', 'chamber2', 'chamber4')
     with self.assertRaises(ValueError):
         doAction(gamestate, action)
Example #7
0
def new_board(difficulty, player_ids, data_structures):
    """
    Forms the JSON format for the game board state.

    :param difficulty: difficulty of the game
    :param player_ids: list of player ids
    :param data_structures: list of data structures
    :return: game board dict
    """

    # if it is an AVL
    if data_structures[0] == 'AVL':
        graph = avl.avlNew(config.HEIGHT[str(difficulty)],
                           config.POINTS[str(difficulty)]['max'])
        deck = create_card_deck(list(graph['node_points'].keys()),
                                data_structures[0], difficulty,
                                graph['gold_node'])
        cards, deck = distribute_cards(player_ids, deck)
        # real_players = [player for player in player_ids if not player.lower().startswith(config.BOT_NAME_PREFIX)]
        board = {
            'game_id': str(uuid.uuid1()),
            'graph': graph,
            'player_ids': player_ids,
            'player_names': [''],
            'player_points': {str(id): 0
                              for id in player_ids},
            'turn': random.choice(player_ids),
            'deck': deck,
            'cards': cards,
            'difficulty': difficulty,
            'num_players': len(player_ids),
            'curr_data_structure': data_structures[0],
            'end_game': False,
            'time_created': datetime.now().strftime("%d/%m/%Y %H:%M:%S"),
            # Below features are left out for future sprints
            # 'selected_data_structures': data_structures,
            # 'timed_game': False,
            # 'seconds_until_next_ds': 60,
            'online': False,
            'profile_load': False
        }
    # Currently only gives AVL however, this will be for linked list game mode
    else:
        # graph = avl.avlNew(config.HEIGHT[str(difficulty)], config.POINTS[str(difficulty)])
        # this will be replaced when I have the llist_handler class

        graph = makeNewGame()

        board = {
            'game_id': str(uuid.uuid1()),
            'graph': graph,
            'queen_at_head': True,
            'colony_exit': False,
            'colony_entrance': False,
            'total_chambers': 1,
            'total_tunnels': 0,
            'total_food': config.INIT_NUM_FOOD,
            'total_food_types': {
                'crumb': 1,
                'berry': 1,
                'donut': 1,
            },
            'time_tracks': {
                'move/forage': 36,
                'dig_tunnel_track':
                36,  #changing to 36 so that we can make more moves in the demo
                'dig/fill_chamber': 36,
            },
            'total_ants': 1,
            'total_surface_ants': 1,
            'total_under_attack': 0,
            'curr_day': 1,
            'player_ids': player_ids,
            'difficulty': difficulty,
            'num_players': 1,
            'curr_data_structure': data_structures[0],
            'end_game': False,
            'time_created': datetime.now().strftime("%d/%m/%Y %H:%M:%S"),
            'online': False,
            'profile_load': False,
        }

    return board