|
| 1 | +package main |
| 2 | + |
| 3 | +var pieceVal = [12]int{100, -100, 325, -325, 350, -350, 500, -500, 950, -950, 10000, -10000} |
| 4 | + |
| 5 | +var knightFile = [8]int{-4, -3, -2, +2, +2, 0, -2, -4} |
| 6 | +var knightRank = [8]int{-15, 0, +5, +6, +7, +8, +2, -4} |
| 7 | +var centerFile = [8]int{-8, -1, 0, +1, +1, 0, -1, -3} |
| 8 | +var kingFile = [8]int{+1, +2, 0, -2, -2, 0, +2, +1} |
| 9 | +var kingRank = [8]int{+1, 0, -2, -4, -6, -8, -10, -12} |
| 10 | +var pawnRank = [8]int{0, 0, 0, 0, +2, +6, +25, 0} |
| 11 | +var pawnFile = [8]int{0, 0, +1, +10, +10, +8, +10, +8} |
| 12 | +const longDiag = 10 |
| 13 | + |
| 14 | +// Piece Square Table |
| 15 | +var pSqTab [12][64]int |
| 16 | + |
| 17 | +//TODO: eval hash |
| 18 | +//TODO: pawn hash |
| 19 | +//TODO: pawn structures. isolated, backward, duo, passed (guarded and not), double and more... |
| 20 | +//TODO: bishop pair |
| 21 | +//TODO: King safety. pawn shelter, guarding pieces |
| 22 | +//TODO: King attack. Attacking area surrounding the enemy king, closeness to the enemy king |
| 23 | +//TODO: space, center control, knight outposts, connected rooks, 7th row and more |
| 24 | +//TODO: combine middle game and end game values |
| 25 | + |
| 26 | +// evaluate returns score from white pov |
| 27 | +func evaluate(b *boardStruct) int { |
| 28 | + ev := 0 |
| 29 | + for sq := A1; sq <= H8; sq++ { |
| 30 | + p12 := b.sq[sq] |
| 31 | + if p12 == empty { |
| 32 | + continue |
| 33 | + } |
| 34 | + ev += pieceVal[p12] |
| 35 | + ev += pSqScore(p12, sq) |
| 36 | + } |
| 37 | + return ev |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +// Score returns the piece square table value for a given piece on a given square. Stage = MG/EG |
| 42 | +func pSqScore(p12, sq int) int { |
| 43 | + return pSqTab[p12][sq] |
| 44 | +} |
| 45 | + |
| 46 | +// PstInit intits the pieces-square-tables when the program starts |
| 47 | +func pSqInit() { |
| 48 | + tell("info string pStInit startar") |
| 49 | + for p12 := 0; p12 < 12; p12++ { |
| 50 | + for sq := 0; sq < 64; sq++ { |
| 51 | + pSqTab[p12][sq] = 0 |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + for sq := 0; sq < 64; sq++ { |
| 56 | + |
| 57 | + fl := sq % 8 |
| 58 | + rk := sq / 8 |
| 59 | + |
| 60 | + pSqTab[wP][sq] = pawnFile[fl] + pawnRank[rk] |
| 61 | + |
| 62 | + pSqTab[wN][sq] = knightFile[fl] + knightRank[rk] |
| 63 | + pSqTab[wB][sq] = centerFile[fl] + centerFile[rk]*2 |
| 64 | + |
| 65 | + pSqTab[wR][sq] = centerFile[fl] * 5 |
| 66 | + |
| 67 | + pSqTab[wQ][sq] = centerFile[fl] + centerFile[rk] |
| 68 | + |
| 69 | + pSqTab[wK][sq] = (kingFile[fl] + kingRank[rk]) * 8 |
| 70 | + } |
| 71 | + |
| 72 | + // bonus for e4 d5 and c4 |
| 73 | + pSqTab[wP][E2], pSqTab[wP][D2], pSqTab[wP][E3], pSqTab[wP][D3], pSqTab[wP][E4], pSqTab[wP][D4], pSqTab[wP][C4] = 0, 0, 6, 6, 24, 20, 12 |
| 74 | + |
| 75 | + // long diagonal |
| 76 | + for sq := A1; sq <= H8; sq += NE { |
| 77 | + pSqTab[wB][sq] += longDiag - 2 |
| 78 | + } |
| 79 | + for sq := H1; sq <= A8; sq += NW { |
| 80 | + pSqTab[wB][sq] += longDiag |
| 81 | + } |
| 82 | + |
| 83 | + // for Black |
| 84 | + for pc := Pawn; pc <= King; pc++ { |
| 85 | + |
| 86 | + wP12 := pc2P12(pc, WHITE) |
| 87 | + bP12 := pc2P12(pc, BLACK) |
| 88 | + |
| 89 | + for bSq := 0; bSq < 64; bSq++ { |
| 90 | + wSq := oppRank(bSq) |
| 91 | + pSqTab[bP12][bSq] = -pSqTab[wP12][wSq] |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +// mirror the rank_sq |
| 97 | +func oppRank(sq int) int { |
| 98 | + fl := sq % 8 |
| 99 | + rk := sq / 8 |
| 100 | + rk = 7 - rk |
| 101 | + return rk*8 + fl |
| 102 | +} |
0 commit comments