Difference between revisions of "CPW-Engine board(0x88)"

From Chessprogramming wiki
Jump to: navigation, search
(Created page with "'''Home * Engines * CPW-Engine * Board (0x88)''' <pre> #include "stdafx.h" #include "0x88_math.h" #include "transposition.h" sboard b; void clearBoar...")
 
 
Line 10: Line 10:
  
 
void clearBoard() {
 
void clearBoard() {
 
    //reset b struct
 
  
    for (int i=0;i<128;i++) {
+
  //reset b struct
b.pieces[i] = PIECE_EMPTY;
+
 
        b.color[i] = COLOR_EMPTY;
+
  for (int i = 0; i<128; i++) {
    }
+
    b.pieces[i] = PIECE_EMPTY;
    b.castle = 0;
+
    b.color[i] = COLOR_EMPTY;
    b.ep     = -1;
+
  }
b.ply   = 0;
+
  b.castle = 0;
b.hash   = 0;
+
  b.ep = -1;
b.phash = 0;
+
  b.ply = 0;
b.stm   = 0;
+
  b.hash = 0;
b.rep_index=0;
+
  b.phash = 0;
 +
  b.stm = 0;
 +
  b.rep_index = 0;
 +
 
 +
  // reset perceived values
  
// reset perceived values
+
  b.PieceMaterial[WHITE] = 0;
 +
  b.PieceMaterial[BLACK] = 0;
 +
  b.PawnMaterial[WHITE] = 0;
 +
  b.PawnMaterial[BLACK] = 0;
 +
  b.PcsqMg[WHITE] = 0;
 +
  b.PcsqMg[BLACK] = 0;
 +
  b.PcsqEg[WHITE] = 0;
 +
  b.PcsqEg[BLACK] = 0;
  
    b.PieceMaterial[WHITE] = 0;
 
    b.PieceMaterial[BLACK] = 0;
 
    b.PawnMaterial[WHITE]  = 0;
 
    b.PawnMaterial[BLACK]  = 0;
 
    b.PcsqMg[WHITE] = 0;
 
    b.PcsqMg[BLACK] = 0;
 
b.PcsqEg[WHITE] = 0;
 
b.PcsqEg[BLACK] = 0;
 
  
 +
  // reset counters
  
// reset counters
+
  for (int i = 0; i<6; i++) {
+
    b.PieceCount[WHITE][i] = 0;
for (int i=0;i<6;i++) {
+
    b.PieceCount[BLACK][i] = 0;
b.PieceCount[ WHITE ] [ i ] = 0;
+
  }
b.PieceCount[ BLACK ] [ i ] = 0;
 
}
 
 
}
 
}
+
 
 
/********************************************************************
 
/********************************************************************
 
* fillSq() and clearSq(), beside placing a piece on a given square  *
 
* fillSq() and clearSq(), beside placing a piece on a given square  *
Line 54: Line 54:
 
void fillSq(U8 color, U8 piece, S8 sq) {
 
void fillSq(U8 color, U8 piece, S8 sq) {
  
// place a piece on the board
+
  // place a piece on the board
b.pieces[sq] = piece;
+
  b.pieces[sq] = piece;
b.color[sq] = color;
+
  b.color[sq] = color;
+
 
// update king location
+
  // update king location
if (piece == KING)
+
  if (piece == KING)
b.KingLoc[color] = sq;
+
    b.KingLoc[color] = sq;
  
if ( piece == PAWN ) {  
+
  if (piece == PAWN) {
// update pawn material
+
    // update pawn material
b.PawnMaterial[color] += e.PIECE_VALUE[piece];
+
    b.PawnMaterial[color] += e.PIECE_VALUE[piece];
  
// update pawn hashkey - please note conversion to a 32-bit integer
+
    // update pawn hashkey - please note conversion to a 32-bit integer
b.phash ^= zobrist.piecesquare[piece][color][sq];
+
    b.phash ^= zobrist.piecesquare[piece][color][sq];
}
+
  }
else {
+
  else {
// update piece material
+
    // update piece material
b.PieceMaterial[color] += e.PIECE_VALUE[piece];
+
    b.PieceMaterial[color] += e.PIECE_VALUE[piece];
}
+
  }
  
// update piece counter
+
  // update piece counter
b.PieceCount[color][piece]++;
+
  b.PieceCount[color][piece]++;
  
// update piece-square value
+
  // update piece-square value
b.PcsqMg[color] += e.mgPst[piece][color][sq];
+
  b.PcsqMg[color] += e.mgPst[piece][color][sq];
b.PcsqEg[color] += e.egPst[piece][color][sq];
+
  b.PcsqEg[color] += e.egPst[piece][color][sq];
  
// update hash key
+
  // update hash key
b.hash ^= zobrist.piecesquare[piece][color][sq];
+
  b.hash ^= zobrist.piecesquare[piece][color][sq];
 
}
 
}
  
  
 
void clearSq(S8 sq) {
 
void clearSq(S8 sq) {
   
 
// set intermediate variables, then do the same
 
// as in fillSq(), only backwards
 
  
U8 color = b.color[sq];
+
  // set intermediate variables, then do the same
U8 piece = b.pieces[sq];
+
  // as in fillSq(), only backwards
+
 
b.hash ^= zobrist.piecesquare[piece][color][sq];
+
  U8 color = b.color[sq];
 +
  U8 piece = b.pieces[sq];
 +
 
 +
  b.hash ^= zobrist.piecesquare[piece][color][sq];
  
if ( piece == PAWN ) {  
+
  if (piece == PAWN) {
b.PawnMaterial[color] -= e.PIECE_VALUE[piece];
+
    b.PawnMaterial[color] -= e.PIECE_VALUE[piece];
b.phash ^= zobrist.piecesquare[piece][color][sq];
+
    b.phash ^= zobrist.piecesquare[piece][color][sq];
}
+
  }
else  
+
  else
b.PieceMaterial[color] -= e.PIECE_VALUE[piece];
+
    b.PieceMaterial[color] -= e.PIECE_VALUE[piece];
  
b.PcsqMg[color] -= e.mgPst[piece][color][sq];
+
  b.PcsqMg[color] -= e.mgPst[piece][color][sq];
b.PcsqEg[color] -= e.egPst[piece][color][sq];
+
  b.PcsqEg[color] -= e.egPst[piece][color][sq];
  
b.PieceCount[color][piece]--;
+
  b.PieceCount[color][piece]--;
  
    b.pieces[sq] = PIECE_EMPTY;
+
  b.pieces[sq] = PIECE_EMPTY;
    b.color[sq] = COLOR_EMPTY;
+
  b.color[sq] = COLOR_EMPTY;
 
}
 
}
+
 
  
 
int board_loadFromFen(char * fen) {
 
int board_loadFromFen(char * fen) {
+
 
    clearBoard();
+
  clearBoard();
clearHistoryTable();
+
  clearHistoryTable();
+
 
    char * f = fen;
+
  char * f = fen;
+
 
    char col = 0;
+
  char col = 0;
    char row = 7;
+
  char row = 7;
+
 
    do {
+
  do {
        switch( f[0] ) {
+
    switch (f[0]) {
        case 'K': fillSq(WHITE, KING, SET_SQ(row, col));  col++; break;
+
    case 'K': fillSq(WHITE, KING, SET_SQ(row, col));  col++; break;
case 'Q': fillSq(WHITE, QUEEN, SET_SQ(row, col));  col++; break;
+
    case 'Q': fillSq(WHITE, QUEEN, SET_SQ(row, col));  col++; break;
case 'R': fillSq(WHITE, ROOK, SET_SQ(row, col));  col++; break;
+
    case 'R': fillSq(WHITE, ROOK, SET_SQ(row, col));  col++; break;
case 'B': fillSq(WHITE, BISHOP, SET_SQ(row, col)); col++; break;
+
    case 'B': fillSq(WHITE, BISHOP, SET_SQ(row, col)); col++; break;
case 'N': fillSq(WHITE, KNIGHT, SET_SQ(row, col)); col++; break;
+
    case 'N': fillSq(WHITE, KNIGHT, SET_SQ(row, col)); col++; break;
case 'P': fillSq(WHITE, PAWN, SET_SQ(row, col));  col++; break;
+
    case 'P': fillSq(WHITE, PAWN, SET_SQ(row, col));  col++; break;
case 'k': fillSq(BLACK, KING, SET_SQ(row, col));  col++; break;
+
    case 'k': fillSq(BLACK, KING, SET_SQ(row, col));  col++; break;
case 'q': fillSq(BLACK, QUEEN, SET_SQ(row, col));  col++; break;
+
    case 'q': fillSq(BLACK, QUEEN, SET_SQ(row, col));  col++; break;
case 'r': fillSq(BLACK, ROOK, SET_SQ(row, col));  col++; break;
+
    case 'r': fillSq(BLACK, ROOK, SET_SQ(row, col));  col++; break;
case 'b': fillSq(BLACK, BISHOP, SET_SQ(row, col)); col++; break;
+
    case 'b': fillSq(BLACK, BISHOP, SET_SQ(row, col)); col++; break;
case 'n': fillSq(BLACK, KNIGHT, SET_SQ(row, col)); col++; break;
+
    case 'n': fillSq(BLACK, KNIGHT, SET_SQ(row, col)); col++; break;
case 'p': fillSq(BLACK, PAWN, SET_SQ(row, col));  col++; break;
+
    case 'p': fillSq(BLACK, PAWN, SET_SQ(row, col));  col++; break;
        case '/': row--; col=0; break;
+
    case '/': row--; col = 0; break;
        case '1': col+=1; break;
+
    case '1': col += 1; break;
        case '2': col+=2; break;
+
    case '2': col += 2; break;
        case '3': col+=3; break;
+
    case '3': col += 3; break;
        case '4': col+=4; break;
+
    case '4': col += 4; break;
        case '5': col+=5; break;
+
    case '5': col += 5; break;
        case '6': col+=6; break;
+
    case '6': col += 6; break;
        case '7': col+=7; break;
+
    case '7': col += 7; break;
        case '8': col+=8; break;
+
    case '8': col += 8; break;
        };
+
     };
+
 
        f++;
 
     } while ( f[0] != ' ' );
 
 
 
     f++;
 
     f++;
+
  } while (f[0] != ' ');
    if (f[0]=='w') {  
+
 
b.stm = WHITE;
+
  f++;
} else {  
+
 
b.stm = BLACK;  
+
  if (f[0] == 'w') {
b.hash ^= zobrist.color;
+
    b.stm = WHITE;
}
+
  }
+
  else {
    f+=2;
+
    b.stm = BLACK;
+
    b.hash ^= zobrist.color;
    do {
+
  }
        switch( f[0] ) {
+
 
        case 'K': b.castle |= CASTLE_WK; break;
+
  f += 2;
        case 'Q': b.castle |= CASTLE_WQ; break;
+
 
        case 'k': b.castle |= CASTLE_BK; break;
+
  do {
        case 'q': b.castle |= CASTLE_BQ; break;
+
    switch (f[0]) {
        }
+
    case 'K': b.castle |= CASTLE_WK; break;
+
    case 'Q': b.castle |= CASTLE_WQ; break;
        f++;
+
    case 'k': b.castle |= CASTLE_BK; break;
     } while (f[0] != ' ' );
+
    case 'q': b.castle |= CASTLE_BQ; break;
+
     }
b.hash ^= zobrist.castling[b.castle];
 
  
 
     f++;
 
     f++;
+
  } while (f[0] != ' ');
if (f[0] != '-') {
+
 
b.ep = convert_a_0x88(f);
+
  b.hash ^= zobrist.castling[b.castle];
b.hash ^= zobrist.ep[b.ep];
+
 
}
+
  f++;
+
 
    do { f++; } while (f[0] != ' ' );
+
  if (f[0] != '-') {
    f++;
+
    b.ep = convert_a_0x88(f);
+
    b.hash ^= zobrist.ep[b.ep];
    sscanf(f, "%d", &b.ply);
+
  }
 +
 
 +
  do { f++; } while (f[0] != ' ');
 +
  f++;
 +
 
 +
  sscanf(f, "%d", &b.ply);
 +
 
 +
  b.rep_index = 0;
 +
  b.rep_stack[b.rep_index] = b.hash;
  
b.rep_index = 0;
+
  return 0;
b.rep_stack[b.rep_index] = b.hash;
 
 
    return 0;
 
 
}
 
}
  
Line 196: Line 197:
 
void board_display() {
 
void board_display() {
  
    S8 sq;
+
  S8 sq;
+
 
char parray[3][7] = { {'K','Q','R','B','N','P'},
+
  char parray[3][7] = { { 'K','Q','R','B','N','P' },
                      {'k','q','r','b','n','p'},
+
  { 'k','q','r','b','n','p' },
                      { 0 , 0 , 0 , 0 , 0,  0, '.'}
+
  { 0 , 0 , 0 , 0 , 0,  0, '.' }
                    };
+
  };
+
 
    printf("  a b c d e f g h\n\n");
+
  printf("  a b c d e f g h\n\n");
 
    for (S8 row=7; row>=0; row--) {
 
           
 
        printf("%d ", row+1);
 
 
        for (S8 col=0; col<8; col++) {
 
            sq = SET_SQ(row, col);
 
printf(" %c",parray[b.color[sq]][b.pieces[sq]] );
 
        }
 
  
printf("  %d\n", row+1);
+
  for (S8 row = 7; row >= 0; row--) {
  
 +
    printf("%d ", row + 1);
 +
 +
    for (S8 col = 0; col<8; col++) {
 +
      sq = SET_SQ(row, col);
 +
      printf(" %c", parray[b.color[sq]][b.pieces[sq]]);
 
     }
 
     }
   
+
 
    printf("\n  a b c d e f g h\n\n");
+
    printf(" %d\n", row + 1);
 +
 
 +
  }
 +
 
 +
  printf("\n  a b c d e f g h\n\n");
 
}
 
}
 
</pre>
 
</pre>
 
'''[[CPW-Engine|Up one Level]]'''
 
'''[[CPW-Engine|Up one Level]]'''

Latest revision as of 15:39, 18 December 2018

Home * Engines * CPW-Engine * Board (0x88)

#include "stdafx.h"
#include "0x88_math.h"
#include "transposition.h"

sboard b;


void clearBoard() {

  //reset b struct

  for (int i = 0; i<128; i++) {
    b.pieces[i] = PIECE_EMPTY;
    b.color[i] = COLOR_EMPTY;
  }
  b.castle = 0;
  b.ep = -1;
  b.ply = 0;
  b.hash = 0;
  b.phash = 0;
  b.stm = 0;
  b.rep_index = 0;

  // reset perceived values

  b.PieceMaterial[WHITE] = 0;
  b.PieceMaterial[BLACK] = 0;
  b.PawnMaterial[WHITE] = 0;
  b.PawnMaterial[BLACK] = 0;
  b.PcsqMg[WHITE] = 0;
  b.PcsqMg[BLACK] = 0;
  b.PcsqEg[WHITE] = 0;
  b.PcsqEg[BLACK] = 0;


  // reset counters

  for (int i = 0; i<6; i++) {
    b.PieceCount[WHITE][i] = 0;
    b.PieceCount[BLACK][i] = 0;
  }
}

/********************************************************************
* fillSq() and clearSq(), beside placing a piece on a given square  *
* or erasing it, must take care for all the incrementally  updated  *
* stuff: hash keys, piece counters, material and pcsq values, king  *
* location.                                                         *
********************************************************************/

void fillSq(U8 color, U8 piece, S8 sq) {

  // place a piece on the board
  b.pieces[sq] = piece;
  b.color[sq] = color;

  // update king location
  if (piece == KING)
    b.KingLoc[color] = sq;

  if (piece == PAWN) {
    // update pawn material
    b.PawnMaterial[color] += e.PIECE_VALUE[piece];

    // update pawn hashkey - please note conversion to a 32-bit integer
    b.phash ^= zobrist.piecesquare[piece][color][sq];
  }
  else {
    // update piece material
    b.PieceMaterial[color] += e.PIECE_VALUE[piece];
  }

  // update piece counter
  b.PieceCount[color][piece]++;

  // update piece-square value
  b.PcsqMg[color] += e.mgPst[piece][color][sq];
  b.PcsqEg[color] += e.egPst[piece][color][sq];

  // update hash key
  b.hash ^= zobrist.piecesquare[piece][color][sq];
}


void clearSq(S8 sq) {

  // set intermediate variables, then do the same 
  // as in fillSq(), only backwards

  U8 color = b.color[sq];
  U8 piece = b.pieces[sq];

  b.hash ^= zobrist.piecesquare[piece][color][sq];

  if (piece == PAWN) {
    b.PawnMaterial[color] -= e.PIECE_VALUE[piece];
    b.phash ^= zobrist.piecesquare[piece][color][sq];
  }
  else
    b.PieceMaterial[color] -= e.PIECE_VALUE[piece];

  b.PcsqMg[color] -= e.mgPst[piece][color][sq];
  b.PcsqEg[color] -= e.egPst[piece][color][sq];

  b.PieceCount[color][piece]--;

  b.pieces[sq] = PIECE_EMPTY;
  b.color[sq] = COLOR_EMPTY;
}


int board_loadFromFen(char * fen) {

  clearBoard();
  clearHistoryTable();

  char * f = fen;

  char col = 0;
  char row = 7;

  do {
    switch (f[0]) {
    case 'K': fillSq(WHITE, KING, SET_SQ(row, col));   col++; break;
    case 'Q': fillSq(WHITE, QUEEN, SET_SQ(row, col));  col++; break;
    case 'R': fillSq(WHITE, ROOK, SET_SQ(row, col));   col++; break;
    case 'B': fillSq(WHITE, BISHOP, SET_SQ(row, col)); col++; break;
    case 'N': fillSq(WHITE, KNIGHT, SET_SQ(row, col)); col++; break;
    case 'P': fillSq(WHITE, PAWN, SET_SQ(row, col));   col++; break;
    case 'k': fillSq(BLACK, KING, SET_SQ(row, col));   col++; break;
    case 'q': fillSq(BLACK, QUEEN, SET_SQ(row, col));  col++; break;
    case 'r': fillSq(BLACK, ROOK, SET_SQ(row, col));   col++; break;
    case 'b': fillSq(BLACK, BISHOP, SET_SQ(row, col)); col++; break;
    case 'n': fillSq(BLACK, KNIGHT, SET_SQ(row, col)); col++; break;
    case 'p': fillSq(BLACK, PAWN, SET_SQ(row, col));   col++; break;
    case '/': row--; col = 0; break;
    case '1': col += 1; break;
    case '2': col += 2; break;
    case '3': col += 3; break;
    case '4': col += 4; break;
    case '5': col += 5; break;
    case '6': col += 6; break;
    case '7': col += 7; break;
    case '8': col += 8; break;
    };

    f++;
  } while (f[0] != ' ');

  f++;

  if (f[0] == 'w') {
    b.stm = WHITE;
  }
  else {
    b.stm = BLACK;
    b.hash ^= zobrist.color;
  }

  f += 2;

  do {
    switch (f[0]) {
    case 'K': b.castle |= CASTLE_WK; break;
    case 'Q': b.castle |= CASTLE_WQ; break;
    case 'k': b.castle |= CASTLE_BK; break;
    case 'q': b.castle |= CASTLE_BQ; break;
    }

    f++;
  } while (f[0] != ' ');

  b.hash ^= zobrist.castling[b.castle];

  f++;

  if (f[0] != '-') {
    b.ep = convert_a_0x88(f);
    b.hash ^= zobrist.ep[b.ep];
  }

  do { f++; } while (f[0] != ' ');
  f++;

  sscanf(f, "%d", &b.ply);

  b.rep_index = 0;
  b.rep_stack[b.rep_index] = b.hash;

  return 0;
}


void board_display() {

  S8 sq;

  char parray[3][7] = { { 'K','Q','R','B','N','P' },
  { 'k','q','r','b','n','p' },
  { 0 , 0 , 0 , 0 , 0,  0, '.' }
  };

  printf("   a b c d e f g h\n\n");

  for (S8 row = 7; row >= 0; row--) {

    printf("%d ", row + 1);

    for (S8 col = 0; col<8; col++) {
      sq = SET_SQ(row, col);
      printf(" %c", parray[b.color[sq]][b.pieces[sq]]);
    }

    printf("  %d\n", row + 1);

  }

  printf("\n   a b c d e f g h\n\n");
}

Up one Level