Evaluate.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include "Evaluate.h"
  2. #include "ChessGame.h"
  3. using namespace chessy;
  4. namespace chessy
  5. {
  6. namespace values
  7. {
  8. const MoveValue PAWN = 1.0f;
  9. const MoveValue KNIGHT = 3.0f;
  10. const MoveValue BISHOP = 3.0f;
  11. const MoveValue ROOK = 5.0f;
  12. const MoveValue QUEEN = 9.0f;
  13. }
  14. }
  15. #include <random>
  16. static typename std::mt19937 e(1034345);
  17. static std::uniform_real_distribution<MoveValue> ee(0, 0.001);
  18. template<Side side>
  19. MoveValue chessy::evaluatePositives(const ChessGame& game)
  20. {
  21. MoveValue piecePoints = 0;
  22. const Board& bd = game.getBoard();
  23. Bitboard p = bd.getPawns<side>();
  24. Bitboard n = bd.getKnights<side>();
  25. Bitboard b = bd.getBishops<side>();
  26. Bitboard r = bd.getRooks<side>();
  27. Bitboard q = bd.getQueens<side>();
  28. Bitboard k = bd.getKing<side>();
  29. piecePoints += values::PAWN * p.popcount();
  30. piecePoints += values::KNIGHT * n.popcount();
  31. piecePoints += values::BISHOP * b.popcount();
  32. piecePoints += values::ROOK * r.popcount();
  33. piecePoints += values::QUEEN * q.popcount();
  34. for (auto knight : PositionSet{ n })
  35. piecePoints += KnightMoveGenerator{ knight, bd.get<side>() }.getBitboard().popcount() * 0.05;
  36. for (auto bishop : PositionSet{ b })
  37. piecePoints += PrimitiveBishopMoveGenerator{ bishop, bd.get<otherSide(side)>(), bd.get<side>() }.getBitboard().popcount() * 0.03;
  38. for (auto rook : PositionSet{ r })
  39. piecePoints += PrimitiveRookMoveGenerator{ rook, bd.get<otherSide(side)>(), bd.get<side>() }.getBitboard().popcount() * 0.03;
  40. for (auto queen : PositionSet{ q })
  41. piecePoints += PrimitiveQueenMoveGenerator{ queen, bd.get<otherSide(side)>(), bd.get<side>() }.getBitboard().popcount() * 0.012;
  42. return piecePoints;// +ee(e);
  43. /*
  44. constexpr Side other = otherSide(side);
  45. p = bd.getPawns<other>();
  46. n = bd.getKnights<other>();
  47. b = bd.getBishops<other>();
  48. r = bd.getRooks<other>();
  49. q = bd.getQueens<other>();
  50. k = bd.getKing<other>();
  51. piecePoints -= 1 * p.popcount();
  52. piecePoints -= 3 * n.popcount();
  53. piecePoints -= 3 * b.popcount();
  54. piecePoints -= 5 * r.popcount();
  55. piecePoints -= 9 * q.popcount();
  56. //for (auto knight : PositionSet{ n })
  57. // piecePoints -= KnightMoveGenerator{ knight, bd.get<otherSide(side)>() }.getBitboard().popcount() * 0.2;
  58. for (auto bishop : PositionSet{ b })
  59. piecePoints -= PrimitiveBishopMoveGenerator{ bishop, bd.get<side>(), bd.get<otherSide(side)>() }.getBitboard().popcount() * 0.2;
  60. */
  61. }
  62. MoveValue chessy::evaluate(const ChessGame& game)
  63. {
  64. if (game.getTurn() == WHITE_SIDE)
  65. return evaluatePositives<WHITE_SIDE>(game) -
  66. evaluatePositives<BLACK_SIDE>(game);
  67. else
  68. return evaluatePositives<BLACK_SIDE>(game) -
  69. evaluatePositives<WHITE_SIDE>(game);
  70. }