// Generate a maze (recursively)
//   Generates a maze using the Recursive Backtracker method:
//   https://en.wikipedia.org/wiki/Maze_generation_algorithm#Recursive_backtracker
void generate_maze(int x, int y) {
  maze[x][y] = false;  // clear the spot in current position

  while(can_go(x, y - 2) || can_go(x, y + 2) || can_go(x - 2, y) || can_go(x + 2, y)) {  // While there exists a new position you can go to
    switch(rand()%4) {  // Randomly choose a direction to go to
      case 0:  // Try going up
      if(can_go(x, y - 2)) {  // If you can go up
        maze[x][y-1] = false;  // remove wall above current position
        generate_maze(x, y-2);  // move up two spaces
      }
      break;
      
      case 1:  // Try going down
      if(can_go(x, y + 2)) {   // If you can go down
        maze[x][y+1] = false;   // remove wall below current position
        generate_maze(x, y + 2); // move down two spaces
      }
      break;
      
      case 2:  // Try going left
      if(can_go(x - 2, y)) {  // If you can go left
        maze[x-1][y] = false;  // remove wall left of current position
        generate_maze(x-2, y);  // move left two spaces
      }
      break;
      
      case 3:  // Try going right
      if(can_go(x + 2, y)) {  // If you can go right
        maze[x+1][y] = false;  // remove wall right of current position
        generate_maze(x+2, y);  // move right two spaces
      }
      break;
    }
  }
}
Example #2
0
void create_new_leaf_here(int ch)
{
    assert (!can_go(ch));
    if (current_vertex != -1)
        edges[current_vertex][ch] = newEdge(current_vertex, current_letter);
    else if (current_edge != -1)
    {
        vertex new_vertex = newVertex(vdepth[from[current_edge]] + depth);
        if (last_vertex != -1)
        {
            suf[last_vertex] = new_vertex;
            last_vertex = -1;
        }

        edge new_edge = newEdge2(new_vertex, to[current_edge], left[current_edge] + depth, right[current_edge]);
        int old_symbol = s[left[new_edge]];
        edges[new_vertex][old_symbol] = new_edge;
        edges[new_vertex][ch] = newEdge(new_vertex, current_letter);
        to[current_edge] = new_vertex;
        right[current_edge] = left[current_edge] + depth;
        current_vertex = new_vertex;
        last_edge = current_edge;
        last_depth = depth;
        current_edge = -1;
        depth = 0;
        last_vertex = new_vertex;
    }
}
Example #3
0
void ccp_seen(int cn,int co)
{
	struct ccp_mem *mem=get_ccp_mem(cn);

	if (ch[co].attack_cn==cn) {	// he's attacking us
		if (ch[co].points_tot>ch[cn].points_tot) ccp_sector_score(cn,-10000);
	}

	if (co==mem->fighting) return;

	if (ch[cn].a_hp<ch[cn].hp[5]*800) return;
	if (ch[cn].a_mana<ch[cn].hp[5]*800) return;
	
	if (ch[co].flags&CF_PLAYER) return;
	if (ch[co].alignment>0) return;
	if (IS_COMPANION(co)) return;

	if (!do_char_can_see(cn,co)) return;
	if (!can_go(ch[cn].x,ch[cn].y,ch[co].x,ch[co].y)) return;

	if (ch[co].points_tot>(ch[cn].points_tot*3)/2) return;	// too strong

	if (ch[co].points_tot<ch[cn].points_tot/3) {			// too weak
		if (ch[co].points_tot>ch[cn].points_tot) ccp_sector_score(cn,-3000);
		return;
	}

	ccp_set_enemy(cn,co);
	
	ccp_sector_score(cn,1000);
}
Example #4
0
void append(int ch)
{
    while (!can_go(ch))
    {
        create_new_leaf_here(ch);
        jump_suffix_link();
    }
    go(ch);
    //print_stats();
}
Example #5
0
File: cell.cpp Project: py4/m4z3
void Cell::dump_on_screen(SDL_Surface*& screen)
{
	SDL_Rect rect;
	if(!can_go('U'))
	{
		rect.x = j * 25;
		rect.y = i * 25;
		rect.h = 5;
		rect.w = 25;
		SDL_FillRect(screen,&rect,0x000000);
	}
	if(!can_go('D'))
	{
		rect.x = j * 25;
		rect.y = (i+1) * 25;
		rect.h = 5;
		rect.w = 25;
		SDL_FillRect(screen,&rect,0x000000);
	}
	if(!can_go('L'))
	{
		rect.x = j * 25;
		rect.y = i * 25;
		rect.h = 25;
		rect.w = 5;
		SDL_FillRect(screen,&rect,0x000000);
	}
	if(!can_go('R'))
	{
		rect.x = (j+1) * 25;
		rect.y = i * 25;
		rect.h = 25;
		rect.w = 5;
		SDL_FillRect(screen,&rect,0x000000);
	}
}
Example #6
0
/* Pair : row,column */
int lee_algo(Pair* source, Pair* target){
  int i, j, v1, v2;
  Pair p, v, step[4] = {{1,0},{0,1},{-1,0},{0,-1}};
  /* initialize all cells like unvisited */
  for(i=0; i<sz.r; ++i){
    for(j=0; j<sz.c; ++j){
      W[i][j] = -1;
    }
  }
  q_clear();
  p_set(W,source,0);
  q_push(source);
  while(!q_empty()){
    p = q_pop();
    for(i=0; i<4; ++i){
      v = p_sum(step+i,&p);
      v1 = p_get(Z,&p); 
      v2 = p_get(Z,&v);
      #ifdef DEBUG
      printf("try: %d,%d\n",v.r,v.c);
      #endif
      if(p_in(&v) && p_get(W,&v)==-1 && (v1-v2 <= 3) && (v1-v2 >= -1) && can_go(&v)){
        
        #ifdef DEBUG
        printf("from: %d,%d go: %d,%d\n",p.r,p.c,v.r,v.c);
        #endif
        
        p_set(W,&v,p_get(W,&p)+1);
        q_push(&v);
        if(p_eq(&v,target)) break;
      }
    }
    if(p_eq(&v,target)) break;
  }
  #ifdef DEBUG
  printf("q: %d\n",q_empty());
  #endif
  return p_get(W,target);
}
Example #7
0
int hadlock_algo(Pair* source, Pair* target){
  int i, j, v1, v2, res;
  Pair p, v, step[4] = {{1,0},{0,1},{-1,0},{0,-1}};
  /* initialize all cells like unvisited */
  for(i=0; i<sz.r; ++i){
    for(j=0; j<sz.c; ++j){
      W[i][j] = -1;
    }
  }
  q_clear();
  p_set(W,source,0);
  q_push(source);
  while(!q_empty()){
    p = q_pop();
    for(i=0; i<4; ++i){
      v = p_sum(step+i,&p);
      v1 = p_get(Z,&p); 
      v2 = p_get(Z,&v);
      if(p_in(&v) && (p_get(W,&v)==-1 || (p_get(W,&v)>p_get(W,&p))) && (v1-v2 <= 3) && (v1-v2 >= -1) && can_go(&v)){
        if(p_mhtn_norm(&v,target) < p_mhtn_norm(&p,target)){
          p_set(W,&v,p_get(W,&p));
          q_push_back(&v);
        }else{ /* > */
          /*if(!(p_get(W,&v)!=-1 || (p_get(W,&v)==p_get(W,&p)+1))){ */
          p_set(W,&v,p_get(W,&p)+1);
          q_push_front(&v);  
        }
        if(p_eq(&v,target)){
          break;
        }
      }
    }
    if(p_eq(&v,target)) break;
  }
  res = p_get(W,target);
  if(res != -1) res = 2*res+p_mhtn_norm(source,target); 
  return res;
}