示例#1
0
文件: eval.c 项目: mrpeachum/checkers
int runaway(bitboard *board, int pos, bool color)
{
  const bitboard empty = ~(board[WHITE] | board[BLACK]);

  /* In this function the variable next is *not* used for the current field */

  bitboard next,     /* Neighboring field */
           nextnext; /* Neighbor to a neighboring field into the same dir */
  int8 currway=-1,   /* Runaway analysis for the current field */
       minval=10;    /* Minimum runaway analysis of free neighboring fields */
  int i=0;

  /* Return 0 if hostile back row is reached */
  if (pos & king_bits[(int)color]) return 0;
  
  for (i = 0; i < N_DIRS; i++) { /* Check both directions (NW/SE & NE/SW) */
  
    /* Determine neighbors */
    if (color == BLACK) {
      next = DOWN_NEIGHBOR(pos, i);
      nextnext = DOWN_NEIGHBOR(DOWN_NEIGHBOR(pos, i), i);
    } else {
      next = UP_NEIGHBOR(pos, i);
      nextnext = UP_NEIGHBOR(UP_NEIGHBOR(pos, i), i);
    }

/*
printf("*** Field %d: threatened by enemy %d=%d\n", \
  fieldnumber(next),  T_ENEMY(T_COLOR(color)), threatened(board, T_ENEMY(T_COLOR(color))) & next);  
printf("*** Field %d\n", \
  fieldnumber(next));
*/
printf(""); /* Needed to work properly (Weired !!!) */ 

    /* Check recursively if field is empty and unthreatened */
    if (next & empty & ~(threatened(board, T_ENEMY(T_COLOR(color))) & next))
      currway = runaway(board, next, color);

    /* Determine minimum runaway value if there was a path
       to the hostile home row found                       */
    if ((currway > -1) && (currway < minval)) minval = currway;
  }
  
  /* Return minimum value + 1 */
  if (minval == 10) return -1; else return minval + 1;
}
示例#2
0
/*
 * The score is always computed as it was WHITE to play.
 * The same applies to all the heuristic functions.
 *
 * Notes:
 * 	- The quiescence search guarantees that there are no captures pending for the side playing.
 * 	- Contrary to other engines, we don't give a bonus to the side playing because he already has an advantage in mobility:
 * 		the opponent may have pending captures, and these are not giving him any bonuses.
 */
int evaluate(Position * const position) {
	int score;
	unsigned int game_phase;

	uint32 white = position->white();
	uint32 black = position->black();
	uint32 kings = position->kings();
	player_t to_play = position->to_play();

	uint32 w_men = white & ~kings;
	uint32 w_kings = white & kings;
	uint32 b_men = black & ~kings;
	uint32 b_kings = black & kings;
	uint32 occupied = white | black;
	uint32 empty = ~occupied;

	int n_w_men = popcnt(w_men);
	int n_w_kings = popcnt(w_kings);
	int n_b_men = popcnt(b_men);
	int n_b_kings = popcnt(b_kings);

	int n_pieces = n_w_men + n_w_kings + n_b_men + n_b_kings;

	game_phase = (n_pieces >= MIDDLE_GAME ? (n_pieces >= OPENING ? 0 : 1) : 2);

	// material
	score = (n_w_men - n_b_men) * MAN_VALUE[game_phase] + (n_w_kings - n_b_kings) * KING_VALUE[game_phase];

	position->set_to_play(WHITE);
	score += w_mobility(w_men, w_kings, black, b_kings, empty, game_phase);
	position->set_to_play(BLACK);
	score -= b_mobility(b_men, b_kings, white, w_kings, empty, game_phase);
	position->set_to_play(to_play);

	score += back_rank(white, black, w_kings, b_kings, game_phase);
	score += triangle(w_men, b_men, w_kings, b_kings, game_phase);

	score += balance(white, black, game_phase);

	score += runaway(w_men, b_men, empty, game_phase);
	score += bridge_exploit(white, black, w_kings, b_kings, game_phase);

	score += diagonal_control(w_kings, b_kings, occupied, game_phase);

	return (to_play == WHITE ? score : -score);
}
	void CCS225RemoteTask::performNextMotion()
	{	
		float desired_distance;

		switch (MotionIndex)
		{	
			case 0: 
				{
					printf("Loading X calibration point...\n\r");
					//Set desired position:
					des_posOp[0] = XC_X;
					des_posOp[1] = XC_Y;
					des_posOp[2] = XC_Z;
					des_posOp[3] = XC_W;
					des_posOp[4] = XC_VX;
					des_posOp[5] = XC_VY;
					des_posOp[6] = XC_VZ;

					t_mode = GOTO;
					data_->setNewMotion(t_mode, des_posOp); //Set initial position

				}break;

				case 1: 
				{
					printf("Loading Y calibration point...\n\r");

					//Set desired position:
					des_posOp[0] = YC_X;
					des_posOp[1] = YC_Y;
					des_posOp[2] = YC_Z;
					des_posOp[3] = YC_W;
					des_posOp[4] = YC_VX;
					des_posOp[5] = YC_VY;
					des_posOp[6] = YC_VZ;


					t_mode = GOTO;
					data_->setNewMotion(t_mode, des_posOp); //Set initial position

				}break;

				case 2: 
				{
				
					//Set desired position:
					des_posOp[0] = ZC_X;
					des_posOp[1] = ZC_Y;
					des_posOp[2] = ZC_Z;
					des_posOp[3] = ZC_W;
					des_posOp[4] = ZC_VX;
					des_posOp[5] = ZC_VY;
					des_posOp[6] = ZC_VZ;


					printf("Loading Z calibration point...\n\r");

					t_mode = GOTO;
					data_->setNewMotion(t_mode, des_posOp); //Set initial position

				}break;

				case 3:
				{
						//Set desired position:
					des_posOp[0] = X_DEFAULT;
					des_posOp[1] = Y_DEFAULT;
					des_posOp[2] = Z_DEFAULT;
					des_posOp[3] = 0.707;			//this is forward... up is 1,0,0,0
					des_posOp[4] = 0;
					des_posOp[5] = 0.707;
					des_posOp[6] = 0;



					printf("Loading default point...\n\r");

					t_mode = GOTO;
					data_->setNewMotion(t_mode, des_posOp); //Set initial position


				}break;
				case 4: // The main tracking Next Motion
				{
					// Read in target's position
					printf("Going to default\n\r");
					readFromSharedMemory(); //update xdes, ydes, zdes

					//t0 = sutil::CSystemClock::getSimTime();
					
					/*
					// Read in obstacle 1's position
					obs1_x = get_obs1_x();
					obs1_y = get_obs1_y();
					obs1_z = get_obs1_z();

					if ( toocloseto(1) )
					{
						//adjust xdes, ydes, zdes
					}
					*/

					//TODO:
					//Set desired position:
					des_pos[0] = Q0_DEFAULT;
					des_pos[1] = Q1_DEFAULT;
					des_pos[2] = Q2_DEFAULT;
					des_pos[3] = Q3_DEFAULT;
					des_pos[4] = Q4_DEFAULT;
					des_pos[5] = Q5_DEFAULT;

					t_mode = JGOTO;
					data_->setNewMotion(t_mode, des_pos); //Set next position
				}break;
			case 5: // The main tracking Next Motion
				{
					readFromSharedMemory(); //update xdes, ydes, zdes and xobst, yobst, zobst

					if(toocloseto()){
						runaway();
					}
					else
					{	
						// Read in target's position
						printf("In normal operation...\n\r");

						//t0 = sutil::CSystemClock::getSimTime();
	
						//TODO:
						//Set desired position:
						
					}

					desired_distance = sqrt(pow(xdes,2) + pow(ydes,2) + pow(zdes,2));

					if(desired_distance > SAFETY_SPHERE_RADIUS)
					{
						xdes = xdes / (desired_distance / SAFETY_SPHERE_RADIUS);
						ydes = ydes / (desired_distance / SAFETY_SPHERE_RADIUS);
						zdes = zdes / (desired_distance / SAFETY_SPHERE_RADIUS);
					}

					des_posOp[0] = xdes;
					des_posOp[1] = ydes;
					des_posOp[2] = zdes;
					des_posOp[3] = 0.707;			//this is forward... up is 1,0,0,0
					des_posOp[4] = 0;
					des_posOp[5] = 0.707;
					des_posOp[6] = 0;

					t_mode = GOTO;
					data_->setNewMotion(t_mode, des_posOp); //Set next position
				}break;

				case 6: // The main tracking Next Motion
				{
					des_pos[0] = Q0_DEAD;
					des_pos[1] = Q1_DEAD;
					des_pos[2] = Q2_DEAD;
					des_pos[3] = Q3_DEAD;
					des_pos[4] = Q4_DEAD;
					des_pos[5] = Q5_DEAD;

					t_mode = JGOTO;
					data_->setNewMotion(t_mode, des_pos); //Set next position
				}break;
			default:
				{
					printf("ERROR: You shouldn't be here --> all hell broke loose.\r\n");
				}

		} // End of switch (MotionIndex)

	}
示例#4
0
文件: eval.c 项目: mrpeachum/checkers
/**
 * Evaluates an integer variable to a board position stating to whose favor
 * the board position is considered.
 *
 * \param board Board configuration
 * \param btm The color whose turn it is ("black to move")
 * \return An integer. Positive values express an advantage for black and vv.
 */
int eval(bitboard *board, bool btm)
{
  const bitboard empty = ~(board[WHITE] | board[BLACK]);
  
  /* The factors whose sum will be returned */
  int material_val=0,   /* Material */
      trapped_val=0,    /* Trapped Kings */
      doghole_val=0,    /* Dog Holes */
      safe_val=0,       /* Safe Bricks */
      runaway_val=0,    /* Runaway Men */
      turn_val=0,       /* Turn */
      backrank_val=0,   /* Weak Backrank */
      kills_val=0;      /* Possible Kills */

  bitboard to = board[BLACK]|board[WHITE];
  bitboard next=0;      /* mask to the current brick */
  bool color=0;         /* color of the current brick */
  int color_bias=0;     /* -1 for white, 1 for black - useful for adding or
                           subtracting values depending on the brick's color */

	int i=0;							/* loop variable for the detection of next fields */
	bitboard next_moves;	/* mask to the possible next moves of a king */
	bitboard up;          /* mask to one upper neighboring field */
	bitboard down;        /* mask to one lower neighboring field */
  int8 currway=0;       /* runaway analysis for the current brick */
  bitboard rest[N_BOARDS];

  #define EXTRA 2
  int8 curkills=0;      /* possible kills for current brick */
  int8 moves=0;         /* direction in which current brick can move */
  int8 kills[4];
  kills[WHITE]=0;       /* total kill possibilities white bricks have */
  kills[BLACK]=0;       /* total kill possibilities black bricks have */
  kills[WHITE+EXTRA]=0; /* max. extra kills white bricks have */
  kills[BLACK+EXTRA]=0; /* max. extra kills black bricks have */

  /* one loop through all bricks is faster than many loops */
  while (to) {
    next = LAST_ONE(to);
    
    /* set values according to brick color */
    if (next & board[BLACK]) {
      color = BLACK;
      color_bias = 1;
      if (next & board[KING])
        moves = UP + DOWN;
      else
        moves = DOWN;
    } else {
      color = WHITE;
      color_bias = -1;
      if (next & board[KING])
        moves = UP + DOWN;
      else
        moves = UP;
    }

    /* Possible Kills */
    curkills = poss_kills(board, color, moves, next, 0, 0);
    if (curkills > 0) {
      #ifdef DEBUG_EVAL_DETAILS
      printf("- Possible kills for field %d: %d\n", fieldnumber(next), curkills);
      #endif
      kills[(int)color]++;
      if (curkills-1 > kills[color + EXTRA]) kills[color + EXTRA] = curkills-1;
    }

    if (next & board[KING]) {

      /* Material */
      material_val += color_bias * MAT_KING;
      
      /* Trapped Kings */
      /* Compute a mask with possible next moves for the king first */
			next_moves = 0;
			for (i = 0; i < N_DIRS; i++) { /* Check both directions (NW/SE & NE/SW) */
				/* Compute neighbor possitions */
				down = DOWN_NEIGHBOR(next, i);
  			up = UP_NEIGHBOR(next, i);
      		
   			/* If the neighboring field is empty or occupied by an enemy
   				 which can be killed add the appropriate target field      */
			  if (down & empty) {
			 		next_moves |= down;
		 		} else {
		 		  if ((down & board[ENEMY(color)]) && (DOWN_NEIGHBOR(down, i) & empty))
		 		  	next_moves |= DOWN_NEIGHBOR(down, i);
		    }
			  if (up & empty) {
			 		next_moves |= up;
		 		} else {
		 		  if ((up & board[ENEMY(color)]) && (UP_NEIGHBOR(up, i) & empty))
		 		  	next_moves |= UP_NEIGHBOR(up, i);
		    }
	    }
         
      /* In order *not* to be trapped there must exist a possible kill or
      	 one of the poss. moves is either empty or not enemy-threatened   */
		  if ((curkills == 0) &&
          !(next_moves &
            empty & ~(threatened(board, T_ENEMY(T_COLOR(color))) ) )) {
  
          #ifdef DEBUG_EVAL_DETAILS
          printf("- Trapped king on position %d\n", fieldnumber(next));
          #endif
        trapped_val -= color_bias * TRAPPED_KING;
      }

    } else {

      /* Material */
      material_val += color_bias * MAT_MAN;
      
      /* Safe men on the right or left border are a plus */
      if (next & 0x18181818) { // Men on pos. 4, 5, 12, 13, 20, 21, 28 or 29
        #ifdef DEBUG_EVAL_DETAILS
          printf("- Safe man on position %d\n", fieldnumber(next));
        #endif
        safe_val += color_bias * SAFE_MAN;
      }
      
      /* Runaway Checkers */
      /* The runaway function needs the board without the current man in order
         to avoid a wrong result in certain situations */
      rest[BLACK] = board[BLACK] & ~next;
      rest[WHITE] = board[WHITE] & ~next;
      rest[KING] = board[KING] & ~next;
      
      currway = runaway(rest, next, color);
      rest[WHITE] = board[WHITE] & ~next;
      rest[BLACK] = board[BLACK] & ~next;
      rest[KING] = board[KING] & ~next;

      currway = runaway(rest, next, color);
      if (currway > -1) {
        #ifdef DEBUG_EVAL_DETAILS
        printf("- Man on field %d can become king within %d moves.\n",
               fieldnumber(next), currway);
        #endif 
        runaway_val += color_bias * (RUNAWAY_MAN - currway * RUNAWAY_ROW);
      }

    }

    to ^= next;
  }
  
  /* Check for victory */
  if (board[WHITE] == 0) material_val += MAT_VICTORY;
  if (board[BLACK] == 0) material_val -= MAT_VICTORY;

  /* Whose turn is it? */
  float kill_fact[2];
  if (btm) {
    turn_val += 3;
    kill_fact[BLACK] = KILL_FACT_TURN;
    kill_fact[WHITE] = KILL_FACT_NOTURN;
  } else {
    turn_val -= 3;
    kill_fact[BLACK] = KILL_FACT_NOTURN;
    kill_fact[WHITE] = KILL_FACT_TURN;
  }
  
  /* Compute possible kills values */
  if ((kills[BLACK] > 0)) {
    kills_val += kill_fact[BLACK] * POSS_KILL;
    kills_val += kill_fact[BLACK] * POSS_ALTKILL * (kills[BLACK]-1);
    kills_val += kill_fact[BLACK] * POSS_EXTRAKILL * kills[BLACK+EXTRA];
  }
  if ((kills[WHITE] > 0)) {
    kills_val -= kill_fact[WHITE] * POSS_KILL;
    kills_val -= kill_fact[WHITE] * POSS_ALTKILL * (kills[WHITE]-1);
    kills_val -= kill_fact[WHITE] * POSS_EXTRAKILL * kills[WHITE+EXTRA];
  }

  /* Check for weak back ranks */
  int backbricks=0;  
  to = board[WHITE] & king_bits[BLACK];
  while (to) {
    next = LAST_ONE(to);
    backbricks++;
    to ^= next;
  }
  switch(backbricks) {
  case 0:
    backrank_val += BACK_0;
    break;
  case 1:
    backrank_val += BACK_1;
    break;
  case 2:
    backrank_val += BACK_2;
    break;
  case 3:
    backrank_val += BACK_3;
    break;
  }
  backbricks=0;
  to = board[BLACK] & king_bits[WHITE];
  while (to) {
    next = LAST_ONE(to);
    backbricks++;
    to ^= next;
  }
  switch(backbricks) {
  case 0:
    backrank_val -= BACK_0;
    break;
  case 1:
    backrank_val -= BACK_1;
    break;
  case 2:
    backrank_val -= BACK_2;
    break;
  case 3:
    backrank_val -= BACK_3;
    break;
  }
  
  /* Dog Holes */
  if ((board[WHITE] & 0x00000010) && (board[BLACK] & 0x00000001)) {
    // White men on pos. 5 and black brick on pos. 1
    #ifdef DEBUG_EVAL_DETAILS
    printf("- White man in dog hole.\n");
    #endif
    doghole_val += DOG_HOLE;
  } 
  if ((board[BLACK] & 0x08000000) && (board[WHITE] & 0x80000000)) {
    // Black men on pos. 28 and white brick on pos. 31
    #ifdef DEBUG_EVAL_DETAILS
    printf("- Black man in dog hole.\n");
    #endif
    doghole_val -= DOG_HOLE;
  }
  
  /* Output the parameters */
  #ifdef DEBUG_EVAL
  printf("Material: %d\n", material_val);
  printf("Trapped Kings: %d\n", trapped_val);
  printf("Dog holes: %d\n", doghole_val);
  printf("Safe men: %d\n", safe_val);
  printf("Runaway Checkers: %d\n", runaway_val);
  printf("Current Turn: %d\n", turn_val);
  printf("Back Rank: %d\n", backrank_val);
  printf("Possible Kills: %d (B: %d poss., max. %d; W: %d poss., max. %d)\n",
         kills_val, kills[BLACK], kills[BLACK+EXTRA]+1, kills[WHITE], kills[WHITE+EXTRA]+1);
  #endif

  return material_val + trapped_val + doghole_val + safe_val + runaway_val +
         turn_val + backrank_val + kills_val;
}
示例#5
0
void AsiMS2000::selectCommand(int commandNum)
{
  switch(commandNum)
  {
      case 0:
          accel();
          break;
      case 1:
          aalign();
          break;
      case 2:
          afcont();
          break;
      case 3:
          aflim();
          break;
      case 4:
          afocus();
          break;
      case 5:
          afset();
          break;
      case 6:
          afmove();
          break;
      case 7:
          ahome();
          break;
      case 8:
          aij();
          break;
      case 9:
          array();
          break;
      case 10:
          azero();
          break;
      case 11:
          backlash();
          break;
      case 12:
          bcustom();
          break;
      case 13:
          benable();
          break;
      case 14:
          build();
          break;
      case 15:
          cdate();
          break;
      case 16:
          cnts();
          break;
      case 17:
          customa();
          break;
      case 18:
          customb();
          break;
      case 19:
          dack();
          break;
      case 20:
          dump();
          break;
      case 21:
          ensync();
          break;
      case 22:
          epolarity();
          break;
      case 23:
          error();
          break;
      case 24:
          halt();
          break;
      case 25:
          here();
          break;
      case 26:
          home();
          break;
      case 27:
          info();
          break;
      case 28:
          joystick();
          break;
      case 29:
          jsspd();
          break;
      case 30:
          kadc();
          break;
      case 31:
          kd();
          break;
      case 32:
          ki();
          break;
      case 33:
          kp();
          break;
      case 34:
          lcd();
          break;
      case 35:
          led();
          break;
      case 36:
          lladdr();
          break;
      case 37:
          load();
          break;
      case 38:
          lock();
          break;
      case 39:
          lockrg();
          break;
      case 40:
          lockset();
          break;
      case 41:
          maintain();
          break;
      case 42:
          motctrl();
          break;
      case 43:
          move();
          break;
      case 44:
          movrel();
          break;
      case 45:
          pcros();
          break;
      case 46:
          pedal();
          break;
      case 47:
          rbmode();
          break;
      case 48:
          rdadc();
          break;
      case 49:
          rdsbyte();
          break;
      case 50:
          rdstat();
          break;
      case 51:
          relock();
          break;
      case 52:
          reset();
          break;
      case 53:
          rt();
          break;
      case 54:
          runaway();
          break;
      case 55:
          saveset();
          break;
      case 56:
          savepos();
          break;
      case 57:
          scan();
          break;
      case 58:
          scanr();
          break;
      case 59:
          scanv();
          break;
      case 60:
          secure();
          break;
      case 61:
          sethome();
          break;
      case 62:
          setlow();
          break;
      case 63:
          setup();
          break;
      case 64:
          si();
          break;
      case 65:
          speed();
          break;
      case 66:
          spin();
          break;
      case 67:
          status();
          break;
      case 68:
          stopbits();
          break;
      case 69:
          ttl();
          break;
      case 70:
          um();
          break;
      case 71:
          units();
          break;
      case 72:
          unlock();
          break;
      case 73:
          vb();
          break;
      case 74:
          vector();
          break;
      case 75:
          version();
          break;
      case 76:
          wait();
          break;
      case 77:
          where();
          break;
      case 78:
          who();
          break;
      case 79:
          wrdac();
          break;
      case 80:
          zero();
          break;
      case 81:
          z2b();
          break;
      case 82:
          zs();
          break;
      case 83:
          overshoot();
          break;
  }
}