Ejemplo n.º 1
0
void action_one_greedy (struct king *k, struct grid *g, struct flag_grid *fg) {
  int i, j; 
  int i_best, j_best;
  float v_best = -1.0;
  for (i=0; i<g->width; ++i) {
    for (j=0; j<g->height; ++j) {
      if (fg->flag[i][j]) 
        remove_flag(g, fg, i, j, FLAG_POWER);

      // estimate the value of the grid point (i,j)
      int army = g->tiles[i][j].units[k->pl][citizen];
      int enemy = 0;
      int p;
      for (p=0; p<MAX_PLAYER; ++p) { 
        if (p!= k->pl) 
          enemy = enemy + g->tiles[i][j].units[p][citizen];
      }
      float v = (float) k->value[i][j] * (5.0*enemy - army) * pow(army, 0.5); 

      if (v > v_best && v > 5000) {v_best = v; i_best = i; j_best = j;}
    }
  }
  if (v_best > 0)
    add_flag(g, fg, i_best, j_best, FLAG_POWER);
}
Ejemplo n.º 2
0
void action_noble (struct king *k, struct grid *g, struct flag_grid *fg) {
  int i, j; 
  /* number of flags */
  int locval_len = 5;
  struct loc loc[MAX_PRIORITY];
  int val[MAX_PRIORITY];
  init_locval(loc, val, locval_len);

  for (i=0; i<g->width; ++i) {
    for (j=0; j<g->height; ++j) {
      if (fg->flag[i][j]) 
        remove_flag(g, fg, i, j, FLAG_POWER);

      // estimate the value of the grid point (i,j)
      int army = g->tiles[i][j].units[k->pl][citizen];
      int enemy = 0;
      int p;
      for (p=0; p<MAX_PLAYER; ++p) { 
        if (p!= k->pl) 
          enemy = enemy + g->tiles[i][j].units[p][citizen];
      }
      float v = (float) k->value[i][j] * (MAX_POP - (enemy - army)) * pow(army, 0.5); 

      if (enemy > army && v > 7000) {
        //add_flag(g, fg, i, j, FLAG_POWER);
        struct loc lx = {i,j};
        insert_locval(loc, val, locval_len, lx, (int)v);
      }
    }
  }
  for (i=0; i<locval_len && val[i]>0; ++i){
    add_flag(g, fg, loc[i].i, loc[i].j, FLAG_POWER);
  }
}
Ejemplo n.º 3
0
void action_persistent_greedy (struct king *k, struct grid *g, struct flag_grid *fg) {
  int i, j; 
  for (i=0; i<g->width; ++i) {
    for (j=0; j<g->height; ++j) {
      // estimate the value of the grid point (i,j)
      int army = g->tiles[i][j].units[k->pl][citizen];
      int enemy = 0;
      int p;
      for (p=0; p<MAX_PLAYER; ++p) { 
        if (p!= k->pl) 
          enemy = enemy + g->tiles[i][j].units[p][citizen];
      }
      float v1 = (float) k->value[i][j] * (2.5*enemy - army) * pow(army, 0.7); 
      // weak opportunist
      float v2 = (float) k->value[i][j] * (MAX_POP - (enemy - army)) * pow(army, 0.7) * 0.5; 
      if (enemy <= army) v2 = -10000;
      float v = MAX(v1,v2);

      if (fg->flag[i][j] == FLAG_ON) {
        if (v < 1000) 
          remove_flag(g, fg, i, j, FLAG_POWER);
      }
      else {
        if (v > 9000) 
          add_flag(g, fg, i, j, FLAG_POWER);
      }
    }
  }
}
/* 优化一:向数组中添加标志符#来分隔所有的字符,将奇数长度的回文串和偶数长度的回文串统一起来,不过由于其中多次用到了内存动态分配,可能略显麻烦 */
void longest_pal_substr2(char *str, int size, char *substr)
{
    char *newstr = (char*)calloc(size*2+1, sizeof(char));
    strncpy(newstr, str, size);
    add_flag(newstr, size);

    int i = 0, mid = 0, radius = 0;
    int len = size*2+1;
    int max_radius = 0, max_mid = 0;
    while(i < len) {
        mid = i;
        radius = 1;
        while(mid-radius > 0 &&
              mid+radius < len &&
              newstr[mid-radius] == newstr[mid+radius])
            radius++;
        radius--;
        if(radius > max_radius) {
            max_mid = mid;
            max_radius = radius;
        }
        i++;
    }

    int sub_len = 2*max_radius+1;
    char *new_substr = (char*)calloc(sub_len, sizeof(char));
    strncpy(new_substr, newstr+max_mid-max_radius, sub_len);
    remove_flag(new_substr, sub_len);
    strncpy(substr, new_substr, max_radius);
    
    free(new_substr);
    free(newstr);
}
Ejemplo n.º 5
0
bool mut_lose(int mut_idx)
{
	variant v;
	if (mut_idx < 0 || mut_idx >= MAX_MUTATIONS) return FALSE;
	if (!mut_present(mut_idx)) return FALSE;
	if (mut_locked(mut_idx)) return FALSE;
	
	var_init(&v);
	remove_flag(p_ptr->muta, mut_idx);
	(_mutations[mut_idx].spell.fn)(SPELL_LOSE_MUT, &v);
	var_clear(&v);

	_mut_refresh();
	return TRUE;
}
/* 优化二:使用Manacher's Algorithm */
void longest_pal_substr3(char *str, int size, char *substr)
{
    int newlen = 2*size+1;
    char *newstr = (char*)calloc(newlen, sizeof(char));
    int *P = (int*)calloc(newlen, sizeof(int));
    strncpy(newstr, str, size);
    add_flag(newstr, size);

    int i = 0, i_mirror = 0;
    int center = 0, right = 0;
    for(i = 0; i < newlen; i++) {
        i_mirror = 2*center-i;

        P[i] = (right>i) ? min(right-i, P[i_mirror]) : 0;

        while(newstr[i+1+P[i]] == newstr[i-1-P[i]])
            P[i]++;

        if(i+P[i] > right) {
            center = i;
            right = i+P[i];
        }
    }

    int max_len = 0;
    int center_index = 0;
    for(i = 0; i < newlen; i++) {
        if(P[i] > max_len) {
            max_len = P[i];
            center_index = i;
        }
    }

    //printf("center: %d\tmax_len: %d\n", center_index, max_len);
    int sub_len = 2*max_len+1;
    char *new_substr = (char*)calloc(sub_len, sizeof(char));
    strncpy(new_substr, newstr+center_index-max_len, sub_len);
    //printf("%s\n", new_substr);
    remove_flag(new_substr, sub_len);
    strncpy(substr, new_substr, max_len);

    free(P);
    free(newstr);
}
Ejemplo n.º 7
0
void action_opportunist (struct king *k, struct grid *g, struct flag_grid *fg) {
  int i, j; 
  for (i=0; i<g->width; ++i) {
    for (j=0; j<g->height; ++j) {
      if (fg->flag[i][j]) 
        remove_flag(g, fg, i, j, FLAG_POWER);

      // estimate the value of the grid point (i,j)
      int army = g->tiles[i][j].units[k->pl][citizen];
      int enemy = 0;
      int p;
      for (p=0; p<MAX_PLAYER; ++p) { 
        if (p!= k->pl) 
          enemy = enemy + g->tiles[i][j].units[p][citizen];
      }
      float v = (float) k->value[i][j] * (MAX_POP - (enemy - army)) * pow(army, 0.5); 

      if (enemy > army && v > 7000) 
        add_flag(g, fg, i, j, FLAG_POWER);
    }
  }
}
Ejemplo n.º 8
0
static enum parser_error parse_r_mf(struct parser *p) {
	struct monster_race *r = parser_priv(p);
	char *flags;
	char *s;

	if (!r)
		return PARSE_ERROR_MISSING_RECORD_HEADER;
	if (!parser_hasval(p, "flags"))
		return PARSE_ERROR_NONE;
	flags = string_make(parser_getstr(p, "flags"));
	s = strtok(flags, " |");
	while (s) {
		if (remove_flag(r->flags, RF_SIZE, r_info_flags, s)) {
			mem_free(flags);
			quit_fmt("bad mf-flag: %s", s);
			return PARSE_ERROR_INVALID_FLAG;
		}
		s = strtok(NULL, " |");
	}

	mem_free(flags);
	return PARSE_ERROR_NONE;
}
Ejemplo n.º 9
0
void mut_unlock(int mut_idx)
{
	if (mut_idx < 0 || mut_idx >= MAX_MUTATIONS) return;
	if (!mut_locked(mut_idx)) return;
	remove_flag(p_ptr->muta_lock, mut_idx);
}
Ejemplo n.º 10
0
int singleplayer_process_input (struct state *st, struct ui *ui, char c) {
  int cursi = ui->cursor.i;
  int cursj = ui->cursor.j;
  switch (c) {
    case 'q': case 'Q':
      return 1;
    case 'f':
      st->prev_speed = st->speed;
      st->speed = faster(st->speed);
      break;
    case 's':
      st->prev_speed = st->speed;
      st->speed = slower(st->speed);
      break;
    case 'p':
      if (st->speed == sp_pause)
        st->speed = st->prev_speed;
      else {
        st->prev_speed = st->speed;
        st->speed = sp_pause;
      }
      break;
    case 'h': case K_LEFT:
      cursi--;
      break;
    case 'l': case K_RIGHT:
      cursi++;
      break;
    case 'k': case K_UP:
      cursj--;
      if (cursj % 2 == 1)
        cursi++;
      break;
    case 'j': case K_DOWN:
      cursj++;
      if (cursj % 2 == 0)
        cursi--;
      break;
    case ' ':
      if (st->fg[st->controlled].flag[ui->cursor.i][ui->cursor.j] == 0)
        add_flag (&st->grid, &st->fg[st->controlled], ui->cursor.i, ui->cursor.j, FLAG_POWER);
      else
        remove_flag (&st->grid, &st->fg[st->controlled], ui->cursor.i, ui->cursor.j, FLAG_POWER);
      break;
    case 'x':
      remove_flags_with_prob (&st->grid, &st->fg[st->controlled], 1.0);
      break;
    case 'c':
      remove_flags_with_prob (&st->grid, &st->fg[st->controlled], 0.5);
      break;
    case 'r':
    case 'v':
      build (&st->grid, &st->country[st->controlled], st->controlled, ui->cursor.i, ui->cursor.j);
      break;
    
    case ESCAPE:
    case 91:
      break;
  }
  
  adjust_cursor(st, ui, cursi, cursj);

  return 0;
}
Ejemplo n.º 11
0
int main()
{
  init_environment();
  init_data();
  init_display();

  cuss::interface i_editor;
  Window w_editor(0, 0, 80, 24);
  if (!i_editor.load_from_file("cuss/i_terrain.cuss")) {
    debugmsg("Couldn't load cuss/i_terrain.cuss!");
    end_display();
    return 1;
  }

  std::vector<std::string> type_names;
  std::vector<std::string> flags;
  std::vector<std::string> transformations;
  i_editor.ref_data("list_types", &type_names);
  i_editor.ref_data("list_flags", &flags);
  i_editor.ref_data("list_transformations", &transformations);
  i_editor.select("list_types");

  for (int i = 0; i < TER_MAX; i++) {
    if (i >= TERRAIN_POOL.size() || TERRAIN_POOL[i] == NULL) {
      TERRAIN_POOL.push_back( new terrain_type );
      TERRAIN_POOL[i]->name = default_terrain_name( terrain_id(i) );
    }
  }


// Main loop
  bool quit = false;
  do {
    type_names = get_names();
    cuss::element* selected = i_editor.selected();
    int ter_num = i_editor.get_int("list_types");
    terrain_type* current_ter = NULL;

    if (ter_num < TERRAIN_POOL.size()) {
      current_ter = TERRAIN_POOL[ i_editor.get_int("list_types") ];
      flags = get_flag_names(current_ter);
      transformations = get_transformation_names(current_ter);
      i_editor.ref_data("num_move", &(current_ter->move_cost));
      i_editor.ref_data("num_sight", &(current_ter->sight_cost));
      i_editor.ref_data("text_name", &(current_ter->name));
    }

    i_editor.draw(&w_editor);
    long ch = getch();
    if (selected->name == "text_name" &&
        ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == ' ')) {
      current_ter->name += ch;
    } else if (selected->name == "text_name" && is_backspace(ch) &&
               !current_ter->name.empty()) {
      current_ter->name =
        current_ter->name.substr(0, current_ter->name.length() - 1);
    } else if (ch == 's' || ch == 'S') {
      quit = true;
    } else if (ch == 'c' || ch == 'C') {
      set_symbol(current_ter);
    } else if (ch == 'a' || ch == 'A') {
      if (selected->name == "list_types") {
        terrain_type *tmp = new terrain_type;
        tmp->move_cost = 10;
        tmp->name = string_input_popup("Name:");
        set_symbol(tmp);
        TERRAIN_POOL.push_back(tmp);
        i_editor.set_data("list_types", 999);
      } else if (selected->name == "list_flags") {
        add_flag(current_ter);
        i_editor.set_data("list_flags", 0);
      } else if (selected->name == "list_transformations") {
        add_transformation(current_ter);
        i_editor.set_data("list_transformations", 0);
      }
    } else if (ch == 'd' || ch == 'D') {
      if (selected->name == "list_flags") {
        int index = i_editor.get_int("list_flags");
        if (index < flags.size()) {
          remove_flag(current_ter, index);
          i_editor.set_data("list_flags", 0);
        }
      } else if (selected->name == "list_transformations") {
        int index = i_editor.get_int("list_transformations");
        if (index < transformations.size()) {
          remove_transformation(current_ter, index);
          i_editor.set_data("list_transformations", 0);
        }
      }
    } else if (ch == '?') {
      debugmsg("%d of %d", i_editor.get_int("list_transformations"),
                           transformations.size());
      debugmsg("%d (\\n = %d", TERRAIN_POOL[1]->name[0], '\n');
    } else {
      i_editor.handle_action(ch);
    }
  } while (!quit);

  save_data();
  end_display();
  return 0;
}