#include <sstream>
#include "bitcount.h"
-#include "mersenne.h"
#include "movegen.h"
#include "movepick.h"
#include "position.h"
#include "psqtab.h"
+#include "rkiss.h"
#include "san.h"
#include "tt.h"
#include "ucioption.h"
using std::cout;
using std::endl;
-static inline bool isZero(char c) { return c == '0'; }
-struct PieceLetters : public std::map<char, Piece> {
+////
+//// Position's static data definitions
+////
+
+Key Position::zobrist[2][8][64];
+Key Position::zobEp[64];
+Key Position::zobCastle[16];
+Key Position::zobSideToMove;
+Key Position::zobExclusion;
+
+Score Position::PieceSquareTable[16][64];
+
+// Material values arrays, indexed by Piece
+const Value Position::PieceValueMidgame[17] = {
+ VALUE_ZERO,
+ PawnValueMidgame, KnightValueMidgame, BishopValueMidgame,
+ RookValueMidgame, QueenValueMidgame, VALUE_ZERO,
+ VALUE_ZERO, VALUE_ZERO,
+ PawnValueMidgame, KnightValueMidgame, BishopValueMidgame,
+ RookValueMidgame, QueenValueMidgame
+};
+
+const Value Position::PieceValueEndgame[17] = {
+ VALUE_ZERO,
+ PawnValueEndgame, KnightValueEndgame, BishopValueEndgame,
+ RookValueEndgame, QueenValueEndgame, VALUE_ZERO,
+ VALUE_ZERO, VALUE_ZERO,
+ PawnValueEndgame, KnightValueEndgame, BishopValueEndgame,
+ RookValueEndgame, QueenValueEndgame
+};
+
+// Material values array used by SEE, indexed by PieceType
+const Value Position::seeValues[] = {
+ VALUE_ZERO,
+ PawnValueMidgame, KnightValueMidgame, BishopValueMidgame,
+ RookValueMidgame, QueenValueMidgame, QueenValueMidgame*10
+};
+
+
+namespace {
+
+ // Bonus for having the side to move (modified by Joona Kiiski)
+ const Score TempoValue = make_score(48, 22);
+
+ bool isZero(char c) { return c == '0'; }
+
+ struct PieceLetters : public std::map<char, Piece> {
PieceLetters() {
assert(false);
return 0;
}
-};
-
-
-////
-//// Constants and variables
-////
-
-/// Bonus for having the side to move (modified by Joona Kiiski)
-
-static const Score TempoValue = make_score(48, 22);
-
-
-Key Position::zobrist[2][8][64];
-Key Position::zobEp[64];
-Key Position::zobCastle[16];
-Key Position::zobSideToMove;
-Key Position::zobExclusion;
-
-Score Position::PieceSquareTable[16][64];
-
-static PieceLetters pieceLetters;
-
-// Material values used by SEE, indexed by PieceType
-const Value Position::seeValues[] = {
- VALUE_ZERO, PawnValueMidgame, KnightValueMidgame, BishopValueMidgame,
- RookValueMidgame, QueenValueMidgame, QueenValueMidgame*10
-};
+ } pieceLetters;
+}
-/// Constructors
+/// CheckInfo c'tor
CheckInfo::CheckInfo(const Position& pos) {
/// or the FEN string, we want the new born Position object do not depend
/// on any external data so we detach state pointer from the source one.
-Position::Position(int th) : threadID(th) {}
-
Position::Position(const Position& pos, int th) {
memcpy(this, &pos, sizeof(Position));
detach(); // Always detach() in copy c'tor to avoid surprises
threadID = th;
+ nodes = 0;
}
Position::Position(const string& fen, int th) {
/// Position::print() prints an ASCII representation of the position to
-/// the standard output. If a move is given then also the san is print.
+/// the standard output. If a move is given then also the san is printed.
void Position::print(Move move) const {
assert(move_is_ok(m));
assert(square_is_ok(s));
+ Bitboard occ, xray;
Square f = move_from(m), t = move_to(m);
assert(square_is_occupied(f));
return true;
// Move the piece and scan for X-ray attacks behind it
- Bitboard occ = occupied_squares();
- Color us = color_of_piece_on(f);
- clear_bit(&occ, f);
- set_bit(&occ, t);
- Bitboard xray = ( (rook_attacks_bb(s, occ) & pieces(ROOK, QUEEN))
- |(bishop_attacks_bb(s, occ) & pieces(BISHOP, QUEEN))) & pieces_of_color(us);
+ occ = occupied_squares();
+ do_move_bb(&occ, make_move_bb(f, t));
+ xray = ( (rook_attacks_bb(s, occ) & pieces(ROOK, QUEEN))
+ |(bishop_attacks_bb(s, occ) & pieces(BISHOP, QUEEN)))
+ & pieces_of_color(color_of_piece_on(f));
// If we have attacks we need to verify that are caused by our move
// and are not already existent ones.
assert(is_ok());
assert(move_is_ok(m));
+ nodes++;
Key key = st->key;
// Copy some fields of old state to our new StateInfo object except the
Value npMaterial[2];
};
- memcpy(&newSt, st, sizeof(ReducedStateInfo));
+ if (&newSt != st)
+ memcpy(&newSt, st, sizeof(ReducedStateInfo));
+
newSt.previous = st;
st = &newSt;
memset(st, 0, sizeof(StateInfo));
st->epSquare = SQ_NONE;
startPosPlyCounter = 0;
+ nodes = 0;
memset(byColorBB, 0, sizeof(Bitboard) * 2);
memset(byTypeBB, 0, sizeof(Bitboard) * 8);
Key Position::compute_key() const {
- Key result = Key(0ULL);
+ Key result = 0;
for (Square s = SQ_A1; s <= SQ_H8; s++)
if (square_is_occupied(s))
Key Position::compute_pawn_key() const {
- Key result = Key(0ULL);
+ Key result = 0;
Bitboard b;
Square s;
Key Position::compute_material_key() const {
- Key result = Key(0ULL);
+ Key result = 0;
for (Color c = WHITE; c <= BLACK; c++)
for (PieceType pt = PAWN; pt <= QUEEN; pt++)
{
/// Position::is_draw() tests whether the position is drawn by material,
/// repetition, or the 50 moves rule. It does not detect stalemates, this
/// must be done by the search.
-// FIXME: Currently we are not handling 50 move rule correctly when in check
bool Position::is_draw() const {
return true;
// Draw by the 50 moves rule?
- if (st->rule50 > 100 || (st->rule50 == 100 && !is_check()))
+ if (st->rule50 > 99 && (st->rule50 > 100 || !is_mate()))
return true;
// Draw by repetition?
bool Position::is_mate() const {
- MoveStack moves[256];
+ MoveStack moves[MOVES_MAX];
return is_check() && (generate_moves(*this, moves) == moves);
}
bool Position::has_mate_threat() {
- MoveStack mlist[256], *last, *cur;
+ MoveStack mlist[MOVES_MAX], *last, *cur;
StateInfo st1, st2;
bool mateFound = false;
void Position::init_zobrist() {
+ RKISS RKiss;
int i,j, k;
for (i = 0; i < 2; i++) for (j = 0; j < 8; j++) for (k = 0; k < 64; k++)
- zobrist[i][j][k] = Key(genrand_int64());
+ zobrist[i][j][k] = RKiss.rand<Key>();
for (i = 0; i < 64; i++)
- zobEp[i] = Key(genrand_int64());
+ zobEp[i] = RKiss.rand<Key>();
for (i = 0; i < 16; i++)
- zobCastle[i] = Key(genrand_int64());
+ zobCastle[i] = RKiss.rand<Key>();
- zobSideToMove = Key(genrand_int64());
- zobExclusion = Key(genrand_int64());
+ zobSideToMove = RKiss.rand<Key>();
+ zobExclusion = RKiss.rand<Key>();
}
// Is there more than 2 checkers?
if (failedStep) (*failedStep)++;
- if (debugCheckerCount && count_1s(st->checkersBB) > 2)
+ if (debugCheckerCount && count_1s<CNT32>(st->checkersBB) > 2)
return false;
// Bitboards OK?
if (debugPieceCounts)
for (Color c = WHITE; c <= BLACK; c++)
for (PieceType pt = PAWN; pt <= KING; pt++)
- if (pieceCount[c][pt] != count_1s(pieces(pt, c)))
+ if (pieceCount[c][pt] != count_1s<CNT32>(pieces(pt, c)))
return false;
if (failedStep) (*failedStep)++;