Ejemplo n.º 1
0
int main_menu(SDL_Renderer *renderer) {
  int should_quit = 0;
  Code code = CODE_OK;
  Menu menu;
  CommandTable command_table;
  char title[MAXIMUM_STRING_SIZE];
  char *options[] = {"Play", "Top Scores", "Info", "Quit"};
  sprintf(title, "%s %s", "Walls of Doom", WALLS_OF_DOOM_VERSION);
  menu.title = title;
  menu.options = options;
  menu.option_count = 4;
  menu.selected_option = 0;
  initialize_command_table(&command_table);
  while (!should_quit) {
    write_menu(&menu, renderer);
    read_commands(&command_table);
    if (test_command_table(&command_table, COMMAND_UP, REPETITION_DELAY)) {
      if (menu.selected_option > 0) {
        menu.selected_option--;
      } else {
        menu.selected_option = menu.option_count - 1;
      }
    } else if (test_command_table(&command_table, COMMAND_DOWN, REPETITION_DELAY)) {
      if (menu.selected_option + 1 < menu.option_count) {
        menu.selected_option++;
      } else {
        menu.selected_option = 0;
      }
    } else if (test_command_table(&command_table, COMMAND_ENTER, REPETITION_DELAY) ||
               test_command_table(&command_table, COMMAND_CENTER, REPETITION_DELAY)) {
      if (menu.selected_option == 0) {
        code = game(renderer, &command_table);
      } else if (menu.selected_option == 1) {
        code = top_scores(renderer, &command_table);
      } else if (menu.selected_option == 2) {
        code = info(renderer, &command_table);
      } else if (menu.selected_option == 3) {
        should_quit = 1;
      }
      /* If it is not defined whether or not we should quit, check the code. */
      if (should_quit == 0) {
        should_quit = is_termination_code(code);
      }
    }
    /* Quit if the user selected the Quit option or closed the window. */
    should_quit = should_quit || test_command_table(&command_table, COMMAND_QUIT, REPETITION_DELAY);
  }
  return 0;
}
Ejemplo n.º 2
0
void World::mutate(double ratio_to_reproduce){

    std::vector<std::string> top_brains;
    std::string robot_brain;
    std::vector<std::vector<unsigned int> > top_ancestors;
    std::vector<unsigned int> tmp_ancestors;
    std::vector<unsigned int> robot_uuids_to_remove;
    int tmp_score, num_to_keep, num_to_birth, i, rand_robot_index;
    unsigned int uuid;

    num_to_keep = robots.size() * ratio_to_reproduce;
    num_to_birth = robots.size() - num_to_keep;
    std::vector<Robot*> top_bots(num_to_keep, NULL);
    std::vector<int> top_scores (num_to_keep, -1);

    printf("top_bots.size(): %d", top_bots.size());

    //Get robots that need to reproduce
    for( auto it = robot_vec.begin(); it != robot_vec.end(); it++ ){
        tmp_score = (*it)->get_score();
        for( int i=0; i<num_to_keep; i++ ){
            printf("tmp_score: %d\n", tmp_score);
            printf("top_score: %d\n", top_scores[i]);
            if( tmp_score > top_scores[i] ){
                printf("top bot getting changed\n");
                top_bots[i] = (*it);
                top_scores[i] = tmp_score;
                //Don't want the top robot to dominate!!!!
                break;
            }
        }
    }

    //Find and remove robots that didn't get to reproduce
    for( auto it = robot_vec.begin(); it != robot_vec.end(); it++ ){
        //If the robot didn't make the cut
        if(find(top_bots.begin(), top_bots.end(), *it) == top_bots.end()){
            //Get its uuid
            printf("Deleting robot pointer: %p\n", *it);
            uuid = robot_uuid_lookup[*it];
            printf("Deleting robot uuid: %d\n\n", uuid);

            robot_uuids_to_remove.push_back(uuid);
        }
    }

    for( auto it = robot_uuids_to_remove.begin(); it != robot_uuids_to_remove.end(); it++ ){
        remove_robot(*it);
    }

    printf("Num bots remaining: %d", robot_vec.size());

    //Reset scores of top bots
    for( auto it = top_bots.begin(); it!=top_bots.end(); it++ ){
        (*it)->reset_score();
    }

    //Serialize the victor brains
    for( i=0; i<top_bots.size(); i++ ){
        top_brains.push_back(top_bots[i]->get_nnet()->serialize());
    }

    printf("top_brains.size(): %d", top_brains.size());
    //Get the ancestors
    for( i=0; i<top_bots.size(); i++ ){
        tmp_ancestors = top_bots[i]->get_ancestors();
        tmp_ancestors.push_back(robot_uuid_lookup[top_bots[i]]);
        top_ancestors.push_back(tmp_ancestors);
    }

    


    
    //Reproduce the victors
    for( i=0; i<num_to_birth; i++){
        //Select random robot;
        rand_robot_index = rand()%top_brains.size();

        //To SPEED THIS UP, I could only store the brains of the top bots or memoize them
        add_robot(0,0,top_brains[rand_robot_index], top_ancestors[rand_robot_index]);
    }

    
    
    
    

}