// check the victim pieces returned by the move to determine if it's a
// game-over situation.  If so, also calculate the score depending on
// the pov (which player's point of view)
static bool is_game_over(victims_t victims, int pov, int ply) {
  tbassert(ptype_of(victims.stomped) != KING, "Stomped a king.\n");
  if (ptype_of(victims.zapped) == KING) {
    return true;
  }
  return false;
}
// MOBILITY heuristic: safe squares around king of color color.
int mobility(position_t *p, color_t color) {
  char* laser_map;
  if (color == WHITE) {
    laser_map = laser_map_black;
  } else {
    laser_map = laser_map_white;
  }
  int mobility = 0;
  square_t king_sq = p->kloc[color];
  tbassert(ptype_of(p->board[king_sq]) == KING,
           "ptype: %d\n", ptype_of(p->board[king_sq]));
  tbassert(color_of(p->board[king_sq]) == color,
           "color: %d\n", color_of(p->board[king_sq]));

  if (laser_map[king_sq] == 0) {
    mobility++;
  }
  for (int d = 0; d < 8; ++d) {
    square_t sq = king_sq + dir_of(d);
    if (laser_map[sq] == 0) {
      mobility++;
    }
  }
  return mobility;
}
// KAGGRESSIVE heuristic: bonus for King with more space to back
ev_score_t kaggressive_old(position_t *p, fil_t f, rnk_t r) {
  square_t sq = square_of(f, r);
  piece_t x = p->board[sq];
  color_t c = color_of(x);
  tbassert(ptype_of(x) == KING, "ptype_of(x) = %d\n", ptype_of(x));

  square_t opp_sq = p->kloc[opp_color(c)];
  fil_t of = fil_of(opp_sq);
  rnk_t _or = (rnk_t) rnk_of(opp_sq);

  int delta_fil = of - f;
  int delta_rnk = _or - r;

  int bonus = 0;

  if (delta_fil >= 0 && delta_rnk >= 0) {
    bonus = (f + 1) * (r + 1);
  } else if (delta_fil <= 0 && delta_rnk >= 0) {
    bonus = (BOARD_WIDTH - f) * (r + 1);
  } else if (delta_fil <= 0 && delta_rnk <= 0) {
    bonus = (BOARD_WIDTH - f) * (BOARD_WIDTH - r);
  } else if (delta_fil >= 0 && delta_rnk <= 0) {
    bonus = (f + 1) * (BOARD_WIDTH - r);
  }

  return (KAGGRESSIVE * bonus) / (BOARD_WIDTH * BOARD_WIDTH);
}
Example #4
0
// KAGGRESSIVE heuristic: bonus for King with more space to back
ev_score_t kaggressive(position_t *p, rnk_t r, fil_t f, full_board_t* board) {
  piece_t x = get_piece(p, r, f, board);
  color_t c = color_of(x);
  tbassert(ptype_of(x) == KING, "ptype_of(x) = %d\n", ptype_of(x));

  square_t opp_sq = board->pieces[opp_color(c)][0];
  fil_t of = fil_of(opp_sq);
  rnk_t _or = (rnk_t) rnk_of(opp_sq);

  int delta_fil = of - f;
  int delta_rnk = _or - r;

  int bonus = 0;

  if (delta_fil >= 0 && delta_rnk >= 0) {
    bonus = (f + 1) * (r + 1);
  } else if (delta_fil <= 0 && delta_rnk >= 0) {
    bonus = (BOARD_WIDTH - f) * (r + 1);
  } else if (delta_fil <= 0 && delta_rnk <= 0) {
    bonus = (BOARD_WIDTH - f) * (BOARD_WIDTH - r);
  } else if (delta_fil >= 0 && delta_rnk <= 0) {
    bonus = (f + 1) * (BOARD_WIDTH - r);
  }

  return (KAGGRESSIVE * bonus) / (BOARD_WIDTH * BOARD_WIDTH);
}
// Marks the path of the laser until it hits a piece or goes off the board. 
// Returns the number of unpinned pawns.
//
// p : current board state
// laser_map : end result will be stored here. Every square on the
//             path of the laser is marked with mark_mask
// c : color of king shooting laser
// mark_mask: what each square is marked with
int mark_laser_path(position_t *p, char *laser_map, color_t c,
                     char mark_mask) {
  int pinned_pawns = 0;
  uint8_t total_pawns;
  color_t color = opp_color(c);
  square_t o_king_sq = p->kloc[color];

  if (c == WHITE) { // opposing king pins our pawns 
    total_pawns = p->pawn_count[BLACK];
  } else {
    total_pawns = p->pawn_count[WHITE];
  }

  // Fire laser, recording in laser_map
  square_t sq = p->kloc[c];
  int bdir = ori_of(p->board[sq]);
  int beam = beam_of(bdir);

  tbassert(ptype_of(p->board[sq]) == KING,
           "ptype: %d\n", ptype_of(p->board[sq]));
  laser_map[sq] |= mark_mask;

  // we update h_attackable here
  h_attackable = h_dist(sq, o_king_sq);

  while (true) {
    sq += beam;
    laser_map[sq] |= mark_mask;
    tbassert(sq < ARR_SIZE && sq >= 0, "sq: %d\n", sq);

    switch (ptype_of(p->board[sq])) {
      case EMPTY:  // empty square
        h_attackable += h_dist(sq, o_king_sq);
        break;
      case PAWN:  // Pawn
        h_attackable += h_dist(sq, o_king_sq);
        if (color_of(p->board[sq]) == color) {
          pinned_pawns += 1;
        }
        bdir = reflect_of(bdir, ori_of(p->board[sq]));
        if (bdir < 0) {  // Hit back of Pawn
          return total_pawns - pinned_pawns;
        }
        beam = beam_of(bdir);
        break;
      case KING:  // King
        h_attackable += h_dist(sq, o_king_sq);
          return total_pawns - pinned_pawns;
        break;
      case INVALID:  // Ran off edge of board
        return total_pawns - pinned_pawns;
        break;
      default:  // Shouldna happen, man!
        tbassert(false, "Not cool, man.  Not cool.\n");
        break;
    }
  }
}
Example #6
0
// Translate a position struct into a fen string
// NOTE: When you use the test framework in search.c, you should modify this
// function to match your optimized board representation in move_gen.c
//
// Input:   (populated) position struct
//          empty string where FEN characters will be written
// Output:   null
int pos_to_fen(position_t *p, char *fen) {
  int pos = 0;
  int i;

  for (rnk_t r = BOARD_WIDTH - 1; r >=0 ; --r) {
    int empty_in_a_row = 0;
    for (fil_t f = 0; f < BOARD_WIDTH; ++f) {
      piece_t piece = get_piece(p, r, f, &base_board);
      if (ptype_of(piece) == INVALID) {     // invalid square
        tbassert(false, "Bad news, yo.\n");        // This is bad!
      }

      if (ptype_of(piece) == EMPTY) {       // empty square
        empty_in_a_row++;
        continue;
      } else {
        if (empty_in_a_row) fen[pos++] = '0' + empty_in_a_row;
        empty_in_a_row = 0;
        int ori = ori_of(piece);  // orientation
        color_t c = color_of(piece);

        if (ptype_of(piece) == KING) {
          for (i = 0; i < 2; i++) fen[pos++] = king_ori_to_rep[c][ori][i];
          continue;
        }

        if (ptype_of(piece) == PAWN) {
          for (i = 0; i < 2; i++) fen[pos++] = pawn_ori_to_rep[c][ori][i];
          continue;
        }
      }
    }
    // assert: for larger boards, we need more general solns
    tbassert(BOARD_WIDTH <= 10, "BOARD_WIDTH = %d\n", BOARD_WIDTH);
    if (empty_in_a_row == 10) {
      fen[pos++] = '1';
      fen[pos++] = '0';
    } else if (empty_in_a_row) {
      fen[pos++] = '0' + empty_in_a_row;
    }
    if (r) fen[pos++] = '/';
  }
  fen[pos++] = ' ';
  fen[pos++] = 'W';
  fen[pos++] = '\0';

  return pos;
}
Example #7
0
// MOBILITY heuristic: safe squares around king of color color.
int mobility(position_t *p, color_t color, char* laser_map, full_board_t* board) {
  int mobility = 0;
  square_t king_sq = board->pieces[color][0];
  tbassert(ptype_of(board->board[king_sq]) == KING,
           "ptype: %d\n", ptype_of(board->board[king_sq]));
  tbassert(color_of(board->board[king_sq]) == color,
           "color: %d\n", color_of(board->board[king_sq]));

  if (laser_map[king_sq] == 0) {
    mobility++;
  }
  for (int d = 0; d < 8; ++d) {
    square_t sq = king_sq + dir_of(d);
    if (in_bounds(sq) && laser_map[sq] == 0) {
      mobility++;
    }
  }
  return mobility;
}
Example #8
0
int pawnpin(position_t *p, color_t color, square_t* laser_list, int laser_list_len, full_board_t* board) {
  int unpinned_pawns = board->pawn_count[color];

  for(int i = 1; i < laser_list_len; i++) {
    if (color_of(board->board[laser_list[i]]) == color && ptype_of(board->board[laser_list[i]]) == PAWN) {
      unpinned_pawns--;
    }
  }

  return unpinned_pawns;
}
// H_SQUARES_ATTACKABLE heuristic: for shooting the enemy king
int h_squares_attackable(position_t *p, color_t c) {
  char* laser_map;
  if (c == WHITE) {
    laser_map = laser_map_white;
  } else {
    laser_map = laser_map_black;
  }
  square_t o_king_sq = p->kloc[opp_color(c)];
  tbassert(ptype_of(p->board[o_king_sq]) == KING,
           "ptype: %d\n", ptype_of(p->board[o_king_sq]));
  tbassert(color_of(p->board[o_king_sq]) != c,
           "color: %d\n", color_of(p->board[o_king_sq]));

  float h_attackable_temp = 0;
  for (fil_t f = 0; f < BOARD_WIDTH; f++) {
    for (rnk_t r = 0; r < BOARD_WIDTH; r++) {
      square_t sq = square_of(f, r);
      if (laser_map[sq] != 0) {
        h_attackable_temp += h_dist(sq, o_king_sq);
      }
    }
  }
  return h_attackable_temp;
}
static score_t get_game_over_score(victims_t victims, int pov, int ply) {
  tbassert(ptype_of(victims.stomped) != KING, "Stomped a king.\n");
  score_t score;
  if (color_of(victims.zapped) == WHITE) {
    score = -WIN * pov;
  } else {
    score = WIN * pov;
  }
  if (score < 0) {
    score += ply;
  } else {
    score -= ply;
  }
  return score;
}
Example #11
0
int check_pawn_counts(position_t *p) {
  int live_pawn_count = 0;
  for (fil_t f = 0; f < BOARD_WIDTH; f++) {
    square_t sq = (FIL_ORIGIN + f) * ARR_WIDTH + RNK_ORIGIN;
    for (rnk_t r = 0; r < BOARD_WIDTH; r++, sq++) {
      if (ptype_of(p->board[sq]) == PAWN) {
        live_pawn_count++;
      }
    }
  }
  int ploc_count = 0;
  for (int i = 0; i < NUM_PAWNS; i++) {
    if (p->ploc[i] != 0) {
      ploc_count++;
    }
  }
  if (ploc_count == live_pawn_count) {
    return 1;
  }
  return 0;
}
Example #12
0
int check_position_integrity(position_t *p) {
  for (fil_t f = 0; f < BOARD_WIDTH; f++) {
    square_t sq = (FIL_ORIGIN + f) * ARR_WIDTH + RNK_ORIGIN;
    for (rnk_t r = 0; r < BOARD_WIDTH; r++, sq++) {
      if (ptype_of(p->board[sq]) == PAWN) {
        int pawn_found = 0;
        for (int i = 0; i < NUM_PAWNS; i++) {
          if (p->ploc[i] == sq) {
            pawn_found = 1;
            break;
          }
        }
        if (pawn_found == 0) {
          return 0;
        }
      }
    }
  }

  return 1;
}
Example #13
0
static score_t get_game_over_score(victims_t* victims, int pov, int ply) {
  tbassert(ptype_of(victims->stomped) != KING, "Stomped a king.\n");
  // score negative when victims.zapped == WHITE
  score_t score = -1*(1 - 2*(color_of(victims->zapped)))*WIN*pov;
  return score + (1 - 2*(score >= 0))*ply;
}
Example #14
0
// check the victim pieces returned by the move to determine if it's a
// game-over situation.  If so, also calculate the score depending on
// the pov (which player's point of view)
static bool is_game_over(victims_t* victims, int pov, int ply) {
  tbassert(ptype_of(victims->stomped) != KING, "Stomped a king.\n");
  return ptype_of(victims->zapped) == KING;
}
// Static evaluation.  Returns score
score_t eval(position_t *p, bool verbose) {
  // seed rand_r with a value of 1, as per
  // http://linux.die.net/man/3/rand_r
  static __thread unsigned int seed = 1;
  // verbose = true: print out components of score
  ev_score_t score[2] = { 0, 0 };
  //  int corner[2][2] = { {INF, INF}, {INF, INF} };
  ev_score_t bonus;

  //char buf[MAX_CHARS_IN_MOVE];
  color_t c;
  for (fil_t f = 0; f < BOARD_WIDTH; f++) {
    for (rnk_t r = 0; r < BOARD_WIDTH; r++) {
      square_t sq = square_of(f, r);
      piece_t x = p->board[sq];

      //if (verbose) {
      //  square_to_str(sq, buf, MAX_CHARS_IN_MOVE);
      //}

      switch (ptype_of(x)) {
        case EMPTY:
          break;
        case PAWN:
          c = color_of(x);

          // MATERIAL heuristic: Bonus for each Pawn
          bonus = PAWN_EV_VALUE;
          // if (verbose) {
          //  printf("MATERIAL bonus %d for %s Pawn on %s\n", bonus, color_to_str(c), buf);
          // }
          score[c] += bonus;

          // PBETWEEN heuristic
          bonus = pbetween(p, f, r);
          // if (verbose) {
          //   printf("PBETWEEN bonus %d for %s Pawn on %s\n", bonus, color_to_str(c), buf);
          // }
          score[c] += bonus;

          // PCENTRAL heuristic
          bonus = pcentral(f, r);
          // if (verbose) {
          //   printf("PCENTRAL bonus %d for %s Pawn on %s\n", bonus, color_to_str(c), buf);
         //  }
          score[c] += bonus;
          break;

        case KING:
          c = color_of(x);

          // KFACE heuristic
          bonus = kface(p, f, r);
          // if (verbose) {
          //   printf("KFACE bonus %d for %s King on %s\n", bonus,
          //          color_to_str(c), buf);
          // }
          score[c] += bonus;

          // KAGGRESSIVE heuristic
          color_t othercolor = opp_color(c);
          square_t otherking = p->kloc[othercolor];
          fil_t otherf = fil_of(otherking);
          rnk_t otherr = rnk_of(otherking);
          bonus = kaggressive(f, r, otherf, otherr);
          assert(bonus == kaggressive_old(p, f, r));

          // if (verbose) {
          //   printf("KAGGRESSIVE bonus %d for %s King on %s\n", bonus, color_to_str(c), buf);
         //  }
          score[c] += bonus;
          break;
        case INVALID:
          break;
        default:
          tbassert(false, "Jose says: no way!\n");   // No way, Jose!
      }
      laser_map_black[sq] = 0;
      laser_map_white[sq] = 0;
    }
  }
   
  int black_pawns_unpinned = mark_laser_path(p, laser_map_white, WHITE, 1);  // 1 = path of laser with no moves
  
  ev_score_t w_hattackable = HATTACK * (int) h_attackable;
  score[WHITE] += w_hattackable;
  // if (verbose) {
  //   printf("HATTACK bonus %d for White\n", w_hattackable);
  // }

  // PAWNPIN Heuristic --- is a pawn immobilized by the enemy laser.
  int b_pawnpin = PAWNPIN * black_pawns_unpinned;
  score[BLACK] += b_pawnpin;

  int b_mobility = MOBILITY * mobility(p, BLACK);
  score[BLACK] += b_mobility;
  // if (verbose) {
  //   printf("MOBILITY bonus %d for Black\n", b_mobility);
  // }

  int white_pawns_unpinned = mark_laser_path(p, laser_map_black, BLACK, 1);  // 1 = path of laser with no moves
  
  ev_score_t b_hattackable = HATTACK * (int) h_attackable;
  score[BLACK] += b_hattackable;
  // if (verbose) {
  //   printf("HATTACK bonus %d for Black\n", b_hattackable);
  // }

  int w_mobility = MOBILITY * mobility(p, WHITE);
  score[WHITE] += w_mobility;
  // if (verbose) {
  //   printf("MOBILITY bonus %d for White\n", w_mobility);
  // }
  int w_pawnpin = PAWNPIN * white_pawns_unpinned;
  score[WHITE] += w_pawnpin;


  // score from WHITE point of view
  ev_score_t tot = score[WHITE] - score[BLACK];

  if (RANDOMIZE) {
    ev_score_t  z = rand_r(&seed) % (RANDOMIZE*2+1);
    tot = tot + z - RANDOMIZE;
  }

  if (color_to_move_of(p) == BLACK) {
    tot = -tot;
  }

  return tot / EV_SCORE_RATIO;
}
Example #16
0
// Static evaluation.  Returns score
score_t eval(position_t *p, bool verbose, full_board_t* board) {
  // seed rand_r with a value of 1, as per
  // http://linux.die.net/man/3/rand_r
  static __thread unsigned int seed = 1;
  // verbose = true: print out components of score
  ev_score_t score[2] = { 0, 0 };
  //  int corner[2][2] = { {INF, INF}, {INF, INF} };
  ev_score_t bonus;

  color_t c = WHITE;
  while (true) {
    for (pnum_t pnum = 0; pnum <= board->pawn_count[c]; pnum++) {
      square_t sq = board->pieces[c][pnum];
      rnk_t r = rnk_of(sq);
      fil_t f = fil_of(sq);
      piece_t x = board->board[sq];

      switch (ptype_of(x)) {
        case EMPTY:
          tbassert(false, "Jose says: no way!\n");   // No way, Jose!
        case PAWN:
          // PBETWEEN heuristic
          bonus = pbetween(p, r, f, board);
          score[c] += bonus;

          // PCENTRAL heuristic
          bonus = pcentral(r, f);
          score[c] += bonus;
          break;

        case KING:
          // KFACE heuristic
          bonus = kface(p, r, f, board);
          score[c] += bonus;

          // KAGGRESSIVE heuristic
          bonus = kaggressive(p, r, f, board);
          score[c] += bonus;
          break;
        case INVALID:
          tbassert(false, "Jose says: no way!\n");   // No way, Jose!
        default:
          tbassert(false, "Jose says: no way!\n");   // No way, Jose!
      }
    }
    if (c == BLACK)
      break;
    c = BLACK;
  }

  score[WHITE] += board->pawn_count[WHITE] * PAWN_EV_VALUE;
  score[BLACK] += board->pawn_count[BLACK] * PAWN_EV_VALUE;

  square_t white_laser_list[MAX_NUM_PIECES];
  square_t black_laser_list[MAX_NUM_PIECES];

  int whitePathCount = get_laser_path_list(p, white_laser_list, WHITE, board);
  int blackPathCount = get_laser_path_list(p, black_laser_list, BLACK, board);

  ev_score_t w_hattackable = HATTACK * h_squares_attackable(p, WHITE, white_laser_list, whitePathCount, board);
  score[WHITE] += w_hattackable;
  ev_score_t b_hattackable = HATTACK * h_squares_attackable(p, BLACK, black_laser_list, blackPathCount, board);
  score[BLACK] += b_hattackable;

  int w_mobility_list = MOBILITY * mobility_list(p, WHITE, black_laser_list, blackPathCount, board);
  score[WHITE] += w_mobility_list;

  int b_mobility_list = MOBILITY * mobility_list(p, BLACK, white_laser_list, whitePathCount, board);
  score[BLACK] += b_mobility_list;

  // PAWNPIN Heuristic --- is a pawn immobilized by the enemy laser.
  int w_pawnpin = PAWNPIN * pawnpin(p, WHITE, black_laser_list, blackPathCount, board); //use other color's laser map
  score[WHITE] += w_pawnpin;
  int b_pawnpin = PAWNPIN * pawnpin(p, BLACK, white_laser_list, whitePathCount, board); //use other color's laser map
  score[BLACK] += b_pawnpin;

  // score from WHITE point of view
  ev_score_t tot = score[WHITE] - score[BLACK];

  if (RANDOMIZE) {
    ev_score_t  z = rand_r(&seed) % (RANDOMIZE*2+1);
    tot = tot + z - RANDOMIZE;
  }

  if (color_to_move_of(p) == BLACK) {
    tot = -tot;
  }



  return tot / EV_SCORE_RATIO;
}
Example #17
0
// Translate a fen string into a board position struct
//
int fen_to_pos(position_t *p, char *fen) {
  static position_t dmy1, dmy2;

  // these sentinels simplify checking previous
  // states without stepping past null pointers.
  dmy1.key = 0;
  dmy1.victims.stomped = 1;
  dmy1.victims.zapped = 1;
  dmy1.history = NULL;

  dmy2.key = 0;
  dmy2.victims.stomped = 1;
  dmy2.victims.zapped = 1;
  dmy2.history = &dmy1;


  p->key = 0;          // hash key
  p->victims.stomped = 0;       // piece destroyed by stomper
  p->victims.zapped = 0;       // piece destroyed by shooter
  p->history = &dmy2;  // history

  memset(&base_board, 0, sizeof(full_board_t));

  if (fen[0] == '\0') {  // Empty FEN => use starting position
    fen = "ss3nw5/3nw2nw3/2nw7/1nw6SE1/nw9/9SE/1nw6SE1/7SE2/3SE2SE3/5SE3NN W";
  }

  int c_count = 0;  // Invariant: fen[c_count] is next char to be read

  for (int i = 0; i < ARR_SIZE; ++i) {
    base_board.board[i] = INVALID_PIECE;
  }

  c_count = parse_fen_board(p, fen);
  if (!c_count) {
    return 1;  // parse error of board
  }

  // King check

  int Kings[2] = {0, 0};
  for (fil_t f = 0; f < BOARD_WIDTH; ++f) {
    for (rnk_t r = 0; r < BOARD_WIDTH; ++r) {
      square_t sq = square_of(r, f);
      piece_t x = base_board.board[sq];
      ptype_t typ = ptype_of(x);
      if (typ == KING) {
        Kings[color_of(x)]++;
      }
    }
  }

  if (Kings[WHITE] == 0) {
    fen_error(fen, c_count, "No White Kings");
    return 1;
  } else if (Kings[WHITE] > 1) {
    fen_error(fen, c_count, "Too many White Kings");
    return 1;
  } else if (Kings[BLACK] == 0) {
    fen_error(fen, c_count, "No Black Kings");
    return 1;
  } else if (Kings[BLACK] > 1) {
    fen_error(fen, c_count, "Too many Black Kings");
    return 1;
  }

  char c;
  bool done = false;
  // Look for color to move and set ply accordingly
  while (!done && (c = fen[c_count++]) != '\0') {
    switch (c) {
      // ignore whitespace until the end
      case ' ':
      case '\t':
      case '\n':
      case '\r':
        break;

        // White to move
      case 'W':
      case 'w':
        p->ply = 0;
        base_board.ply = 0;
        done = true;
        break;

        // Black to move
      case 'B':
      case 'b':
        p->ply = 1;
        base_board.ply = 1;
        done = true;
        break;

      default:
        fen_error(fen, c_count, "Must specify White (W) or Black (B) to move");
        return 1;
        break;
    }
  }

  // Look for last move, if it exists
  int lm_from_sq, lm_to_sq, lm_rot;
  if (get_sq_from_str(fen, &c_count, &lm_from_sq) != 0) {  // parse error
    return 1;
  }
  if (lm_from_sq == 0) {   // from-square of last move
    p->last_move = 0;  // no last move specified
    p->key = compute_zob_key(p, &base_board);
    return 0;
  }

  c = fen[c_count];

  switch (c) {
    case 'R':
    case 'r':
      lm_to_sq = lm_from_sq;
      lm_rot = RIGHT;
      break;
    case 'U':
    case 'u':
      lm_to_sq = lm_from_sq;
      lm_rot = UTURN;
      break;
    case 'L':
    case 'l':
      lm_to_sq = lm_from_sq;
      lm_rot = LEFT;
      break;

    default:  // Not a rotation
      lm_rot = NONE;
      if (get_sq_from_str(fen, &c_count, &lm_to_sq) != 0) {
        return 1;
      }
      break;
  }
  p->last_move = move_of(EMPTY, lm_rot, lm_from_sq, lm_to_sq);
  p->key = compute_zob_key(p, &base_board);

  return 0;  // everything is okay
}