Beispiel #1
0
int Mv_management::get_dir(int i)
{
    if (data->vcEvents[i].vcPage[0].Movement_type==0)//do not move
        return(5);
    if (data->vcEvents[i].vcPage[0].Movement_type==1)// random
    {
        return(random_move( i));
    }
    if (data->vcEvents[i].vcPage[0].Movement_type==2)// up down
    {
        return(up_down(i));
    }
    if (data->vcEvents[i].vcPage[0].Movement_type==3)// left right
    {
        return(left_right(i));
    }
    if (data->vcEvents[i].vcPage[0].Movement_type==4)// go to the hero
    {
        return(to_hero(i));
    }
    if (data->vcEvents[i].vcPage[0].Movement_type==5)// run frome the hero
    {
        return(escape_hero(i));
    }
    if (data->vcEvents[i].vcPage[0].Movement_type==6)// use the stack
    {
     return(custom(i));
    }

    return(5);
}
Beispiel #2
0
counter move_one_polyhedron(int id, polyhedron *p, int N, const double periodic[3],
                         const double walls[3], bool real_walls, double neighborR,
                        double dist, double angwidth, int max_neighbors, double dr) {
  const double len[3] = {periodic[0]+walls[0], periodic[1]+walls[1], periodic[2]+walls[2]};
  polyhedron temp = random_move(p[id], dist, angwidth, len);
  counter move;
  move.totalmoves ++;
  if (in_cell(temp, walls, real_walls)) {
    bool overlaps = overlaps_with_any(temp, p, periodic);
    if (!overlaps) {
      const bool get_new_neighbors =
        (periodic_diff(temp.pos, temp.neighbor_center, periodic).normsquared() >
         sqr(neighborR/2.0));
      if (get_new_neighbors) {
        // If we've moved too far, then the overlap test may have given a false
        // negative. So we'll find our new neighbors, and check against them.
        // If we still don't overlap, then we'll have to update the tables
        // of our neighbors that have changed.
        temp.neighbors = new int[max_neighbors];
        update_neighbors(temp, id, p, N, neighborR + 2*dr, periodic);
        move.updates ++;
        // However, for this check (and this check only), we don't need to
        // look at all of our neighbors, only our new ones.
        // fixme: do this!
        //int *new_neighbors = new int[max_neighbors];

        overlaps = overlaps_with_any(temp, p, periodic);
        if (!overlaps) {
          // Okay, we've checked twice, just like Santa Clause, so we're definitely
          // keeping this move and need to tell our neighbors where we are now.
          temp.neighbor_center = temp.pos;
          inform_neighbors(temp, p[id], id, p);
          move.informs ++;
          delete[] p[id].neighbors;
        }
        else delete[] temp.neighbors;
      }
      if (!overlaps) {
        p[id] = temp;
        move.workingmoves ++; // move sucessful
        return move;
      }
    }
  }
  return move; // move unsucessful
}
Beispiel #3
0
int main()
{
   int board[BOARD_SIZE_HORIZ][BOARD_SIZE_VERT] = { {0} };
   int player_num, computer_num;
   int last_move; 


   /* Ask Alice if she wants to go first */
   player_num = print_welcome();
   if (player_num == 1) computer_num = 2;
   else computer_num = 1;

   /* If Alice wants to go first, let her make a move */
   if (player_num == 1)
   {
      display_board(board);
      last_move = player_move(board,player_num);
      display_board(board);
   }


   /* The main loop */
   
   while (1)
   {
      /* Make a computer move, then display the board */
      last_move = random_move(board,computer_num);
      printf("Computer moved in column: %d\n", last_move);
      display_board(board);

      /* Check whether the computer has won */
      if (check_win_or_tie(board,last_move)) return 0;


      /* Let Alice make a move, then display the board */
      last_move = player_move(board,player_num);
      display_board(board);

      /* Check whether Alice has won */
      if (check_win_or_tie(board,last_move)) return 0;


   } /* end of while (1) */

} /* end of main() */
Beispiel #4
0
int do_train(string arg)
{
	object me, where;
	me=this_player();
	where=environment(me);
/*
	if( where->query("no_fight") )
		return notify_fail("安全区内不能训兽!\n");
*/
	if( !arg )
		return notify_fail("你要驯服什么?\n");
	if( arg != (string)this_object()->query("id") )
		return notify_fail("你要驯服什么?\n");
	if( this_object()->query_temp("rider") )
		return notify_fail("这匹坐骑上已经有人了。\n");
	if( (string)this_object()->query("owner")==(string)me->query("id") )
		return notify_fail("这匹坐骑已经是你的了。\n");
	if( this_object()->is_fighting() )
		return notify_fail("这匹坐骑已经分身不暇了。\n");
	if( !living(this_object()) )
		return notify_fail("现在耍什么威风?\n");
	if( where->query("no_fight") ) {
		random_move();
		return 1;
	}


	message_vision(HIM"$N冲上前去和$n扭打成一团。\n"NOR, me, this_object());

       	this_object()->set_temp("owner",me->query("id"));
	reset_status();
	this_object()->kill_ob(me);
	me->fight_ob(this_object());

	return 1;
}
Beispiel #5
0
static inline void
filter (void)
{
  static void (* overlap)(guchar *, const guchar *);
  GimpPixelRgn  src;
  GimpPixelRgn  dst;
  GimpRGB       color;
  guchar     pixel[4];
  gint       division_x;
  gint       division_y;
  gint       offset_x;
  gint       offset_y;
  Tile      *tiles;
  gint       numof_tiles;
  Tile      *t;
  gint       i;
  gint       x;
  gint       y;
  gint       move_max_pixels;
  gint       clear_x0;
  gint       clear_y0;
  gint       clear_x1;
  gint       clear_y1;
  gint       clear_width;
  gint       clear_height;
  guchar    *pixels;
  guchar    *buffer;
  gint       dindex;
  gint       sindex;
  gint       px, py;
  GRand     *gr;

  gr = g_rand_new ();

  /* INITIALIZE */
  gimp_pixel_rgn_init (&src, p.drawable, 0, 0,
                       p.drawable->width, p.drawable->height, FALSE, FALSE);
  gimp_pixel_rgn_init (&dst, p.drawable, 0, 0,
                       p.drawable->width, p.drawable->height, TRUE, TRUE);
  pixels = g_new (guchar,
                  p.drawable->bpp * p.drawable->width * p.drawable->height);
  buffer = g_new (guchar,
                  p.drawable->bpp * p.params.tile_width * p.params.tile_height);

  overlap = p.drawable_has_alpha ? overlap_RGBA : overlap_RGB;

  gimp_progress_init (_("Paper Tile"));

  gimp_drawable_mask_bounds (p.drawable->drawable_id,
                             &p.selection.x0, &p.selection.y0,
                             &p.selection.x1, &p.selection.y1);
  p.selection.width  = p.selection.x1 - p.selection.x0;
  p.selection.height = p.selection.y1 - p.selection.y0;

  gimp_tile_cache_ntiles (2 * (p.selection.width / gimp_tile_width () + 1));

  /* TILES */
  division_x = p.params.division_x;
  division_y = p.params.division_y;
  if (p.params.fractional_type == FRACTIONAL_TYPE_FORCE)
    {
      if (0 < p.drawable->width  % p.params.tile_width) division_x++;
      if (0 < p.drawable->height % p.params.tile_height) division_y++;
      if (p.params.centering)
        {
          if (1 < p.drawable->width % p.params.tile_width)
            {
              division_x++;
              offset_x =
                (p.drawable->width % p.params.tile_width) / 2 -
                p.params.tile_width;
            }
          else
            {
              offset_x = 0;
            }

          if (1 < p.drawable->height % p.params.tile_height)
            {
              division_y++;
              offset_y =
                (p.drawable->height % p.params.tile_height) / 2 -
                p.params.tile_height;
            }
          else
            {
              offset_y = 0;
            }
        }
      else
        {
          offset_x = 0;
          offset_y = 0;
        }
    }
  else
    {
      if (p.params.centering)
        {
          offset_x = (p.drawable->width  % p.params.tile_width) / 2;
          offset_y = (p.drawable->height % p.params.tile_height) / 2;
        }
      else
        {
          offset_x = 0;
          offset_y = 0;
        }
    }

  move_max_pixels = p.params.move_max_rate * p.params.tile_width / 100.0;
  numof_tiles = division_x * division_y;
  t = tiles = g_new(Tile, numof_tiles);

  for (y = 0; y < division_y; y++)
    {
      gint srcy = offset_y + p.params.tile_height * y;

      for (x = 0; x < division_x; x++, t++)
        {
          gint srcx = offset_x + p.params.tile_width * x;

          if (srcx < 0)
            {
              t->x     = 0;
              t->width = srcx + p.params.tile_width;
            }
          else if (srcx + p.params.tile_width < p.drawable->width)
            {
              t->x     = srcx;
              t->width = p.params.tile_width;
            }
          else
            {
              t->x     = srcx;
              t->width = p.drawable->width - srcx;
            }

          if (srcy < 0)
            {
              t->y      = 0;
              t->height = srcy + p.params.tile_height;
            }
          else if (srcy + p.params.tile_height < p.drawable->height)
            {
              t->y      = srcy;
              t->height = p.params.tile_height;
            }
          else
            {
              t->y      = srcy;
              t->height = p.drawable->height - srcy;
            }

          t->z = g_rand_int (gr);
          random_move (&t->move_x, &t->move_y, move_max_pixels);
        }
    }

  qsort (tiles, numof_tiles, sizeof *tiles, tile_compare);

  gimp_pixel_rgn_get_rect (&src, pixels, 0, 0, p.drawable->width,
                           p.drawable->height);

  if (p.params.fractional_type == FRACTIONAL_TYPE_IGNORE)
    {
      clear_x0     = offset_x;
      clear_y0     = offset_y;
      clear_width  = p.params.tile_width * division_x;
      clear_height = p.params.tile_height * division_y;
    }
  else
    {
      clear_x0     = 0;
      clear_y0     = 0;
      clear_width  = p.drawable->width;
      clear_height = p.drawable->height;
    }

  clear_x1 = clear_x0 + clear_width;
  clear_y1 = clear_y0 + clear_height;

  switch (p.params.background_type)
    {
    case BACKGROUND_TYPE_TRANSPARENT:
      for (y = clear_y0; y < clear_y1; y++)
        {
          for (x = clear_x0; x < clear_x1; x++)
            {
              dindex = p.drawable->bpp * (p.drawable->width * y + x);
              for (i = 0; i < p.drawable->bpp; i++)
                {
                  pixels[dindex+i] = 0;
                }
            }
        }
      break;

    case BACKGROUND_TYPE_INVERTED:
      for (y = clear_y0; y < clear_y1; y++)
        {
          for (x = clear_x0; x < clear_x1; x++)
            {
              dindex = p.drawable->bpp * (p.drawable->width * y + x);
              pixels[dindex+0] = 255 - pixels[dindex+0];
              pixels[dindex+1] = 255 - pixels[dindex+1];
              pixels[dindex+2] = 255 - pixels[dindex+2];
            }
        }
      break;

    case BACKGROUND_TYPE_IMAGE:
      break;

    case BACKGROUND_TYPE_FOREGROUND:
      gimp_context_get_foreground (&color);
      gimp_rgb_get_uchar (&color, &pixel[0], &pixel[1], &pixel[2]);
      pixel[3] = 255;
      for (y = clear_y0; y < clear_y1; y++)
        {
          for (x = clear_x0; x < clear_x1; x++)
            {
              dindex = p.drawable->bpp * (p.drawable->width * y + x);
              for (i = 0; i < p.drawable->bpp; i++)
                {
                  pixels[dindex+i] = pixel[i];
                }
            }
        }
      break;

    case BACKGROUND_TYPE_BACKGROUND:
      gimp_context_get_background (&color);
      gimp_rgb_get_uchar (&color, &pixel[0], &pixel[1], &pixel[2]);
      pixel[3] = 255;
      for (y = clear_y0; y < clear_y1; y++)
        {
          for (x = clear_x0; x < clear_x1; x++)
            {
              dindex = p.drawable->bpp * (p.drawable->width * y + x);
              for(i = 0; i < p.drawable->bpp; i++)
                {
                  pixels[dindex+i] = pixel[i];
                }
            }
        }
      break;

    case BACKGROUND_TYPE_COLOR:
      gimp_rgba_get_uchar (&p.params.background_color,
                           pixel, pixel + 1, pixel + 2, pixel + 3);
      for (y = clear_y0; y < clear_y1; y++)
        {
          for (x = clear_x0; x < clear_x1; x++)
            {
              dindex = p.drawable->bpp * (p.drawable->width * y + x);
              for(i = 0; i < p.drawable->bpp; i++)
                {
                  pixels[dindex+i] = pixel[i];
                }
            }
        }
      break;
    }

  /* DRAW */
  for (t = tiles, i = 0; i < numof_tiles; i++, t++)
    {
      gint x0 = t->x + t->move_x;
      gint y0 = t->y + t->move_y;

      gimp_pixel_rgn_get_rect (&src, buffer, t->x, t->y, t->width, t->height);

      for (y = 0; y < t->height; y++)
        {
          py = y0 + y;
          for (x = 0; x < t->width; x++)
            {
              px = x0 + x;
              sindex = p.drawable->bpp * (t->width * y + x);
              if (0 <= px && px < p.drawable->width &&
                  0 <= py && py < p.drawable->height)
                {
                  dindex = p.drawable->bpp * (p.drawable->width * py + px);
                  overlap(&pixels[dindex], &buffer[sindex]);
                }
              else if (p.params.wrap_around)
                {
                  px = (px + p.drawable->width)  % p.drawable->width;
                  py = (py + p.drawable->height) % p.drawable->height;
                  dindex = p.drawable->bpp * (p.drawable->width * py + px);
                  overlap(&pixels[dindex], &buffer[sindex]);
                }
            }
        }

      gimp_progress_update ((gdouble) i / (gdouble) numof_tiles);
    }

  gimp_pixel_rgn_set_rect (&dst, pixels, 0, 0, p.drawable->width,
                           p.drawable->height);

  gimp_progress_update (1.0);
  gimp_drawable_flush (p.drawable);
  gimp_drawable_merge_shadow (p.drawable->drawable_id, TRUE);
  gimp_drawable_update (p.drawable->drawable_id,
                        p.selection.x0, p.selection.y0,
                        p.selection.width, p.selection.height);

  g_rand_free (gr);
  g_free (buffer);
  g_free (pixels);
  g_free (tiles);
}
/*** Main Triangulation Function ***/
int main() 
{
  initialize_api(); // Initializes the robot API
  robot_connect("127.0.0.1"); // This always needs to be 127.0.0.1 to connect to the robot
  
  barcode_configure(2 /*number of digits*/, 10 /*barcodes per pass*/, 2 /*kept passes*/, 
                    2 /*skip pixel width*/, 2 /*min bar width*/, 100 /* Allowed skew */, -1 /*Otsu thresholding*/,
                    image_width /*image width*/, image_height /*image height*/);
  sleep(1);
  
  int** barcodes; // Pointer for a two-dimensional array, will hold returned barcodes
  int* xy; // Pointer to an array [x,y]
  camera_set(CAM_ANGLE); // Sets the tilt on the camera to the current defined camera angle
  
  // Create a thread to handle grabbing images for checking barcodes
  pthread_create(&vision_thread, NULL, barcode_start, NULL);
  
  // The final x and y coordinates of the three barcodes
  double x1,y1,x2,y2,x3,y3;
  x1 = y1 = x2 = y2 = x3 = y3 = 0.0;

  // Grab the current seen barcodes
  barcode_frame_wait(); // Waits for the next frame from the current counter
  barcodes = barcode_get_barcodes(); // Retrieves the barcodes in the 2D array
  
  int num_codes = (int)barcodes[0];  // Number of barcodes detected
  int num_digits = (int)barcodes[1]; // Number of digits per barcode (will be 2)
  printf("Detected %d barcodes %d digits long:\n", num_codes, num_digits);
  
  
  // Find three barcodes if we didn't see three at first 
  while (num_codes < 3) {
    // Code to shutdown when the high bump is pressed. Just in case we never find the cans...
    if (get_high_bump() == 1) {
      free(barcodes);
      robot_stop();
      
      /*** Teardown Code ***/
      vision_running = 0; // Tells the background thread to nicely finish
      pthread_join(vision_thread, NULL); // Waits nicely for the barcode module to shutdown
      shutdown_api(); // Shuts down the robot API
      return 0; // Returns 0
    }
    printf("Didn't see 3 barcodes. Moving to find them...\n\n");
    random_move((int)barcodes[0]);
    free(barcodes);
    sleep(3);
    barcode_frame_wait_start(); // Reset the frame wait counter to wait for next full frame
    barcode_frame_wait(); // Waits for the next frame from the current counter
    barcodes = barcode_get_barcodes(); // Retrieves the barcodes in the 2D array
    
    num_codes = (int)barcodes[0];  // Number of barcodes detected
    num_digits = (int)barcodes[1]; // Number of digits per barcode (will be 2)
    printf("Detected %d barcodes %d digits long:\n", num_codes, num_digits);
  }
    
  int i;
  /* Runs through the barcodes detected, which start at index [2] */
  for(i = 2; i < num_codes+2; i++)
    {
      /* This block converts the raw digits into actual numbers */
      int j;
      uint32_t sum = 0;
      for(j = 0; j < num_digits; j++)
	{
	  sum += barcodes[i][j] * pow(10,(num_digits-1)-j);
	}
      
      /* Get the X and Y positions of the cans relative to the robot */
      xy = barcode_get_cur_xy(sum);
      int bottom_y = convert_camera_y(barcodes[i][j+3]);
      double x_pos = can_xpos(bottom_y);
      double y_pos = can_ypos(x_pos,convert_camera_x(xy[0]));
      
      printf("Barcode: %d\tX_CAM: %d\tY_CAM: %d\n",sum,convert_camera_x(xy[0]),bottom_y);
      printf("X_POS: %f\n",x_pos);
      printf("Y_POS: %f\n",y_pos);
      
      // Save the (x,y) coordinates in the proper variables for use later
      switch(i - 1) {
      case 1:
	x1 = x_pos;
	y1 = y_pos;
	break;
      case 2:
	x2 = x_pos;
	y2 = y_pos;
	break;
      case 3:
	x3 = x_pos;
	y3 = y_pos;
	break;
      default:
	break;
      }    
      
      free(xy);
    }

  // We don't need the camera or barcodes any more, so free them up
  vision_running = 0; // Tells the background thread to nicely finish
  pthread_join(vision_thread, NULL); // Waits nicely for the barcode module to shutdown
  free(barcodes);

  /*** Calculate the centroid, and the distance and angle of rotation to it ***/
  double x_centroid = (x1 + x2 + x3) / 3.0;
  double y_centroid = (y1 + y2 + y3) / 3.0;
  double dist_centroid = sqrt((x_centroid * x_centroid) + (y_centroid * y_centroid));
  int16_t dist_cm = (int16_t) round(dist_centroid);
  double theta_centroid = atan((y_centroid / x_centroid));
  int16_t theta_deg = (int16_t) round(DEG_CONV * (theta_centroid * calculate_error_damp(dist_centroid)));
  printf("\n\nCentroid X: %f\nCentroid Y: %f\nDistance: %f\nAngle: %d\n\n",x_centroid,y_centroid,dist_centroid,theta_deg);

  // Use the distance and angle calculations to set the proper robot turn and move values
  int8_t speed;
  if(theta_deg >= 0){
    speed = ROBOT_SPEED;
  }
  else{ // If the angle is negative, turn clockwise (= negative speed)
    speed = ROBOT_SPEED * (-1);
    theta_deg = theta_deg*(-1); // Angle must be positive for turn_robot_wait()
  }
  printf("Turning by %d degrees\n", theta_deg);
  turn_robot_wait(speed,theta_deg);
  printf("Moving %d cm\n",dist_cm);
  move_distance_wait(ROBOT_SPEED,dist_cm);

  // Make sure the robot has stopped
  robot_stop();

  /*** Teardown Code ***/
  shutdown_api(); // Shuts down the robot API
  return 0; // Returns 0
 }
Beispiel #7
0
static void
randomize_tiles (GeglProperties      *o,
                 const GeglRectangle *rect,
                 gint                 division_x,
                 gint                 division_y,
                 gint                 offset_x,
                 gint                 offset_y,
                 gint                 n_tiles,
                 Tile                *tiles)
{
  Tile  *t = tiles;
  gint   move_max_pixels = o->move_rate * o->tile_width / 100;
  gint   x;
  gint   y;
  GRand *gr;

  gr = g_rand_new ();

  for (y = 0; y < division_y; y++)
    {
      gint srcy = offset_y + o->tile_height * y;

      for (x = 0; x < division_x; x++, t++)
        {
          gint srcx = offset_x + o->tile_width * x;

          if (srcx < 0)
            {
              t->x     = 0;
              t->width = srcx + o->tile_width;
            }
          else if (srcx + o->tile_width < rect->width)
            {
              t->x     = srcx;
              t->width = o->tile_width;
            }
          else
            {
              t->x     = srcx;
              t->width = rect->width - srcx;
            }

          if (srcy < 0)
            {
              t->y      = 0;
              t->height = srcy + o->tile_height;
            }
          else if (srcy + o->tile_height < rect->height)
            {
              t->y      = srcy;
              t->height = o->tile_height;
            }
          else
            {
              t->y      = srcy;
              t->height = rect->height - srcy;
            }

          t->z = g_rand_int (gr);
          random_move (&t->move_x, &t->move_y, move_max_pixels, gr);
        }
    }

  qsort (tiles, n_tiles, sizeof (*tiles), tile_compare);
}
Beispiel #8
0
Datei: 2048.c Projekt: Frky/2048
int main(void) {
    int dir;
    srand(time(NULL));
    uint8_t nb_empty_box, nb_merge;
#if USE_BIN 
    /* Creation of a grid */
    grid_t grid = create_empty_grid_bin();
    /* Grid backup */
    grid_t saved_grid;
    /* Initialisation of the grid */
    grid = init_grid_bin(grid, &nb_empty_box);
    /* Display the grid */
    display_grid_bin(grid);
    /* Main loop for a game */
    while (nb_empty_box > 0 || merge_possible_bin(grid)) {
        do {
            saved_grid = grid;
            /* While the move does not lead to a change on the grid */
            // dir = random_move();
            dir = best_move_bin(grid, nb_empty_box);
            /* Trying to play the move */
            grid = move_bin(grid, dir, &nb_merge);
            nb_empty_box += nb_merge;
        } while (grid == saved_grid);
        /* Add tile */
        grid = add_tile_bin(grid);
        nb_empty_box--;
        /* Display the new grid */
        display_grid_bin(grid);
        printf("Empty boxes: %u\n", nb_empty_box);
    }
#else
    /* Creation of a grid */
    uint16_t **grid = create_empty_grid();
    /* Initialisation of the grid */
    init_grid(grid);
    /* Display the grid */
    display_grid(grid);
#if DEBUG
    printf("Value: %i\n", evaluate(grid));
#endif
    /* Main loop for a game */
    while (!game_over(grid)) {
        /* While the move does not lead to a change on the grid */
        do {
#if IA
#if RANDOM
            dir = random_move();
#else
            dir = best_move(grid);
#endif
#else
            dir = ask_dir();
#endif
        /* Trying to play the move */
        } while (!move(grid, dir));
        /* Add a new tile at the end of each round */
        add_tile(grid);
        /* Display the new grid */
        display_grid(grid);
#if DEBUG
        printf("Value: %i\n", evaluate(grid));
#endif
    }
#endif
    return 0;
}