Пример #1
0
/* Function: AlignmentShuffle()
 * Date:     SRE, Sun Apr 22 18:37:15 2001 [St. Louis]
 *
 * Purpose:  Returns a shuffled version of ali2, in ali1.
 *           (ali1 and ali2 can be identical, to shuffle
 *           in place.) The alignment columns are shuffled,
 *           preserving % identity within the columns.
 *
 * Args:     ali1 - allocated space for shuffled alignment
 *                  [0..nseq-1][0..alen-1]
 *           ali2 - alignment to be shuffled
 *           nseq - number of sequences in the alignment       
 *           alen - length of alignment, in columns.
 *
 * Returns:  int
 */
int
AlignmentShuffle(char **ali1, char **ali2, int nseq, int alen)
{
  int  i;
  int  pos;
  char c;

  if (ali1 != ali2) 
    {
      for (i = 0; i < nseq; i++) strcpy(ali1[i], ali2[i]);
    }

  for (i = 0; i < nseq; i++)
    ali1[i][alen] = '\0';

  for (; alen > 1; alen--) 
    {
      pos = CHOOSE(alen);
      for (i = 0; i < nseq; i++) 
	{
	  c               = ali1[i][pos];
	  ali1[i][pos]    = ali1[i][alen-1];
	  ali1[i][alen-1] = c;
	}
    }

  return 1;
}
	Position MapGenerator::AddItem(char c, map<Position, char>& level, int minDist)
	{
		//const int minDist = meta.width / 3;
		//find playerPos
		Position playerPos{ -1, -1 };
		for (int x = 0; x < Position::width; x++)
		{
			for (int y = 0; y < Position::height; y++)
			{
				if (level[Position{ x, y }] == '@')
				{
					playerPos = Position{ x, y };
					break;
				}
			}
		}
		if (playerPos == Position{ -1, -1 }) minDist = 0;
		vector<Position> safePoses;
		for (int x = 0; x < Position::width; x++)
		{
			for (int y = 0; y < Position::height; y++)
			{
				if (abs(x - playerPos.x) >= minDist && abs(y - playerPos.y) > minDist && level[Position{ x, y }] == '.')
				{
					safePoses.push_back(Position{ x, y });
				}
			}
		}
		auto targetPos = CHOOSE(safePoses);
		level[targetPos] = c;// 'g';
		return targetPos;
	}
	void ChooserScene::bloop(float)
	{
		Node* blob = CHOOSE(blobs);
		blob->stopAllActions();
		blob->runAction(Sequence::createWithTwoActions(
			ScaleTo::create(0.2f, 0.9f), ScaleTo::create(0.2f, 0.8f)
			));
	}
Пример #4
0
X 
rel(void) {
	static char    *c[] = {"that", "which"};
	X               v = getxx();
	v->type = "-rel";
	v->list.s[0] = CHOOSE(c);
	return v;
}
Пример #5
0
/* Function: StrShuffle()
 * 
 * Purpose:  Returns a shuffled version of s2, in s1.
 *           (s1 and s2 can be identical, to shuffle in place.)
 *  
 * Args:     s1 - allocated space for shuffled string.
 *           s2 - string to shuffle.
 *           
 * Return:   1 on success.
 */
int
StrShuffle(char *s1, char *s2)
{
  int  len;
  int  pos;
  char c;
  
  if (s1 != s2) strcpy(s1, s2);
  for (len = strlen(s1); len > 1; len--)
    {				
      pos       = CHOOSE(len);
      c         = s1[pos];
      s1[pos]   = s1[len-1];
      s1[len-1] = c;
    }
  return 1;
}
Пример #6
0
/* Function: AlignmentBootstrap()
 * Date:     SRE, Sun Apr 22 18:49:14 2001 [St. Louis]
 *
 * Purpose:  Returns a bootstrapped alignment sample in ali1, 
 *           constructed from ali2 by sampling columns with 
 *           replacement. 
 *           
 *           Unlike the other shuffling routines, ali1 and 
 *           ali2 cannot be the same. ali2 is left unchanged.
 *           ali1 must be a properly allocated space for an
 *           alignment the same size as ali2.
 *
 * Args:     ali1 - allocated space for bootstrapped alignment
 *                  [0..nseq-1][0..alen-1]
 *           ali2 - alignment to be bootstrapped
 *           nseq - number of sequences in the alignment       
 *           alen - length of alignment, in columns. 
 *                  
 * Returns:  1 on success.                 
 */
int
AlignmentBootstrap(char **ali1, char **ali2, int nseq, int alen)
{
  int  pos;
  int  col;
  int  i;

  for (pos = 0; pos < alen; pos++)
    {
      col = CHOOSE(alen);
      for (i = 0; i < nseq; i++) 
	ali1[i][pos] = ali2[i][col];
    }
  for (i = 0; i < nseq; i++)
    ali1[i][alen] = '\0';

  return 1;
}
Пример #7
0
/* Function: StrRegionalShuffle()
 * Date:     SRE, Thu Nov 20 11:02:34 1997 [St. Louis]
 * 
 * Purpose:  Returns a regionally shuffled version of s2, in s1.
 *           (s1 and s2 can be identical to regionally 
 *           shuffle in place.) See [Pearson88].
 *           
 * Args:     s1 - allocated space for regionally shuffled string.
 *           s2 - string to regionally shuffle
 *           w  - window size (typically 10 or 20)      
 *           
 * Return:   1.
 */
int
StrRegionalShuffle(char *s1, char *s2, int w)
{
  int  len;
  char c;
  int  pos;
  int  i, j;

  if (s1 != s2) strcpy(s1, s2);
  len = strlen(s1);

  for (i = 0; i < len; i += w)
    for (j = MIN(len-1, i+w-1); j > i; j--)
      {
	pos     = i + CHOOSE(j-i);
	c       = s1[pos];
	s1[pos] = s1[j];
	s1[j]   = c;
      }
  return 1;
}
Пример #8
0
		case 8: sASSERT_EQ( cursor_reset_pos, 0 ); break;
		
		// There should be no other discards.
		default: sASSERT(0);
	}
	
	(*which_discard)++;
}

DEFINE_RULE(branch_discard_1)
	auto_consume_whitespace = 0;
	SEQ(
		RULE(token, "a"),
		CHOOSE(
			RULE(token, "x"), // which_discard == 0
			RULE(token, "y"), // which_discard == 1
			RULE(token, "z"), // which_discard == 2
			RULE(token, "b")
		),
		OPTIONAL(RULE(token,"q")), // which_discard == 3
		RULE(token,"c"),
		NEG_LOOKAHEAD(RULE(token,"1")), // which_discard == 4
		RULE(token,"d")
	);
END_RULE

DEFINE_RULE(branch_discard_2)
	auto_consume_whitespace = 0;
	SEQ(
		ZERO_OR_MORE(RULE(token, "a")), // which_discard == 5
		RULE(token,"b"),
		ONE_OR_MORE(RULE(token, "a"))   // which_discard == 6
Пример #9
0
/* Function: StrDPShuffle()
 * Date:     SRE, Fri Oct 29 09:15:17 1999 [St. Louis]
 *
 * Purpose:  Returns a shuffled version of s2, in s1.
 *           (s1 and s2 may be identical; i.e. a string
 *           may be shuffled in place.) The shuffle is a  
 *           "doublet-preserving" (DP) shuffle. Both
 *           mono- and di-symbol composition are preserved.
 *           
 *           Done by searching for a random Eulerian 
 *           walk on a directed multigraph. 
 *           Reference: S.F. Altschul and B.W. Erickson, Mol. Biol.
 *           Evol. 2:526-538, 1985. Quoted bits in my comments
 *           are from Altschul's outline of the algorithm.
 *
 * Args:     s1   - RETURN: the string after it's been shuffled
 *                    (space for s1 allocated by caller)
 *           s2   - the string to be shuffled
 *
 * Returns:  0 if string can't be shuffled (it's not all [a-zA-z]
 *             alphabetic.
 *           1 on success. 
 */
int
StrDPShuffle(char *s1, char *s2)
{
  int    len;
  int    pos;	/* a position in s1 or s2 */
  int    x,y;   /* indices of two characters */
  char **E;     /* edge lists: E[0] is the edge list from vertex A */
  int   *nE;    /* lengths of edge lists */
  int   *iE;    /* positions in edge lists */
  int    n;	/* tmp: remaining length of an edge list to be shuffled */
  char   sf;    /* last character in s2 */
  char   Z[26]; /* connectivity in last edge graph Z */ 
  int    keep_connecting; /* flag used in Z connectivity algorithm */
  int    is_eulerian;		/* flag used for when we've got a good Z */
  
  /* First, verify that the string is entirely alphabetic.
   */
  len = strlen(s2);
  for (pos = 0; pos < len; pos++)
    if (! isalpha(s2[pos])) return 0;

  /* "(1) Construct the doublet graph G and edge ordering E
   *      corresponding to S."
   * 
   * Note that these also imply the graph G; and note,
   * for any list x with nE[x] = 0, vertex x is not part
   * of G.
   */
  E  = MallocOrDie(sizeof(char *) * 26);
  nE = MallocOrDie(sizeof(int)    * 26);
  for (x = 0; x < 26; x++)
    {
      E[x]  = MallocOrDie(sizeof(char) * (len-1));
      nE[x] = 0; 
    }

  x = toupper(s2[0]) - 'A';
  for (pos = 1; pos < len; pos++)
    {
      y = toupper(s2[pos]) - 'A';
      E[x][nE[x]] = y;
      nE[x]++;
      x = y;
    }
  
  /* Now we have to find a random Eulerian edge ordering.
   */
  sf = toupper(s2[len-1]) - 'A'; 
  is_eulerian = 0;
  while (! is_eulerian)
    {
      /* "(2) For each vertex s in G except s_f, randomly select
       *      one edge from the s edge list of E(S) to be the
       *      last edge of the s list in a new edge ordering."
       *
       * select random edges and move them to the end of each 
       * edge list.
       */
      for (x = 0; x < 26; x++)
	{
	  if (nE[x] == 0 || x == sf) continue;
	  
	  pos           = CHOOSE(nE[x]);
	  y             = E[x][pos];		
	  E[x][pos]     = E[x][nE[x]-1];
	  E[x][nE[x]-1] = y;
	}

      /* "(3) From this last set of edges, construct the last-edge
       *      graph Z and determine whether or not all of its
       *      vertices are connected to s_f."
       * 
       * a probably stupid algorithm for looking at the
       * connectivity in Z: iteratively sweep through the
       * edges in Z, and build up an array (confusing called Z[x])
       * whose elements are 1 if x is connected to sf, else 0.
       */
      for (x = 0; x < 26; x++) Z[x] = 0;
      Z[(int) sf] = keep_connecting = 1;

      while (keep_connecting) {
	keep_connecting = 0;
	for (x = 0; x < 26; x++)
	  {
	    y = E[x][nE[x]-1];            /* xy is an edge in Z */
	    if (Z[x] == 0 && Z[y] == 1)   /* x is connected to sf in Z */
	      {
		Z[x] = 1;
		keep_connecting = 1;
	      }
	  }
      }

      /* if any vertex in Z is tagged with a 0, it's
       * not connected to sf, and we won't have a Eulerian
       * walk.
       */
      is_eulerian = 1;
      for (x = 0; x < 26; x++)
	{
	  if (nE[x] == 0 || x == sf) continue;
	  if (Z[x] == 0) {
	    is_eulerian = 0;
	    break;
	  }
	}

      /* "(4) If any vertex is not connected in Z to s_f, the
       *      new edge ordering will not be Eulerian, so return to
       *      (2). If all vertices are connected in Z to s_f, 
       *      the new edge ordering will be Eulerian, so
       *      continue to (5)."
       *      
       * e.g. note infinite loop while is_eulerian is FALSE.
       */
    }

  /* "(5) For each vertex s in G, randomly permute the remaining
   *      edges of the s edge list of E(S) to generate the s
   *      edge list of the new edge ordering E(S')."
   *      
   * Essentially a StrShuffle() on the remaining nE[x]-1 elements
   * of each edge list; unfortunately our edge lists are arrays,
   * not strings, so we can't just call out to StrShuffle().
   */
  for (x = 0; x < 26; x++)
    for (n = nE[x] - 1; n > 1; n--)
      {
	pos       = CHOOSE(n);
	y         = E[x][pos];
	E[x][pos] = E[x][n-1];
	E[x][n-1] = y;
      }

  /* "(6) Construct sequence S', a random DP permutation of
   *      S, from E(S') as follows. Start at the s_1 edge list.
   *      At each s_i edge list, add s_i to S', delete the
   *      first edge s_i,s_j of the edge list, and move to
   *      the s_j edge list. Continue this process until
   *      all edge lists are exhausted."
   */ 
  iE = MallocOrDie(sizeof(int) * 26);
  for (x = 0; x < 26; x++) iE[x] = 0; 

  pos = 0; 
  x = toupper(s2[0]) - 'A';
  while (1) 
    {
      s1[pos++] = 'A' + x;	/* add s_i to S' */
      
      y = E[x][iE[x]];
      iE[x]++;			/* "delete" s_i,s_j from edge list */
  
      x = y;			/* move to s_j edge list. */

      if (iE[x] == nE[x])
	break;			/* the edge list is exhausted. */
    }
  s1[pos++] = 'A' + sf;
  s1[pos]   = '\0';  

  /* Reality checks.
   */
  if (x   != sf)  Die("hey, you didn't end on s_f.");
  if (pos != len) Die("hey, pos (%d) != len (%d).", pos, len);
  
  /* Free and return.
   */
  Free2DArray((void **) E, 26);
  free(nE);
  free(iE);
  return 1;
}
Пример #10
0
/***********************************************************************
 *
 * This routine implements the logic for these instructions:
 *
 *	NEAREST		has_dist = false,  want_nearest = true
 *	FARTHEST	has_dist = false,  want_nearest = false
 *	NEAREST2	has_dist = true,   want_nearest = true
 *	FARTHEST2	has_dist = true,   want_nearest = false
 *
 */
static void generic_vision_search(KFORTH_MACHINE *kfm, bool has_dist, bool want_nearest)
{
	static const int xoffset[8] = {  0,  1,  1,  1,  0, -1, -1, -1 };
	static const int yoffset[8] = { -1, -1,  0,  1,  1,  1,  0, -1 };

	CELL *cell;
	ORGANISM *o;
	UNIVERSE *u;
	KFORTH_INTEGER value;
	int i, mask, dist, dir;
	int best_where, best_dir;
	int what, where;
	bool found;

	ASSERT( kfm != NULL );

	if( has_dist ) {
		if( kfm->data_stack_size < 2 )
			return;

		value = kforth_data_stack_pop(kfm);
		dist = (int) value;

		value = kforth_data_stack_pop(kfm);
		mask = (int) (value & VISION_MASK);

	} else {
		if( kfm->data_stack_size < 1 )
			return;

		dist = EVOLVE_MAX_BOUNDS+1;

		value = kforth_data_stack_pop(kfm);
		mask = (int) (value & VISION_MASK);
	}

	cell = (CELL*) kfm->client_data;
	o = cell->organism;
	u = o->universe;

	if( mask == 0 || dist <= 0 ) {
		kforth_data_stack_push(kfm, 0);
		kforth_data_stack_push(kfm, 0);
		return;
	}

	if( want_nearest ) {
		best_where = EVOLVE_MAX_BOUNDS + 1000;
	} else {
		best_where = -1;
	}

	found = false;

	/*
	 * Pick a random starting direction, then
	 * scan clock-wise.
	 */
	dir = CHOOSE(u->er, 0, 7);

	for(i=0; i<8; i++) {
		look_along_line(u, cell, xoffset[dir], yoffset[dir], &what, &where);

		ASSERT( where != 0 );
		ASSERT( what != 0 );

		if( (what & mask) && (where <= dist) ) {
			found = true;
			if( want_nearest ) {
				if( where < best_where ) {
					best_where = where;
					best_dir = dir;
				}
			} else {
				if( where > best_where ) {
					best_where = where;
					best_dir = dir;
				}
			}
		
		}
		dir += 1;
		if( dir > 7 )
			dir = 0;
	}

	if( found ) {
		kforth_data_stack_push(kfm, xoffset[best_dir]);
		kforth_data_stack_push(kfm, yoffset[best_dir]);
	} else {
		kforth_data_stack_push(kfm, 0);
		kforth_data_stack_push(kfm, 0);
	}

}