浏览代码

better evaluation function

Nicolas Winkler 7 年之前
父节点
当前提交
2b34e9ec6d
共有 1 个文件被更改,包括 18 次插入9 次删除
  1. 18 9
      src/Minimax.cpp

+ 18 - 9
src/Minimax.cpp

@@ -88,6 +88,7 @@ std::pair<Move, MoveValue> chessy::miniMax(ChessGame& cg, int depth)
     Move bestMove{ 0, 0 };
     MoveValue alpha = -1e+30;
     MoveValue beta = 1e+30;
+    Move lastValidMove = bestMove;
     for (Move move : moves) {
         MoveInfo mi{ move, cg };
         UndoInfo ui = cg.doMove(mi);
@@ -97,16 +98,18 @@ std::pair<Move, MoveValue> chessy::miniMax(ChessGame& cg, int depth)
             continue;
         }
         else {
-            val = -negamaxImplementation(cg,  depth - 1, -beta, -alpha);
+            lastValidMove = move;
+            val = -negamaxImplementation(cg, depth - 1, -beta, -alpha);
         }
+        //cout << move.asString() << ": " << val << endl;
         cg.undoMove(ui);
-        if(val >= alpha) {
+        if(val > alpha) {
             alpha = val;
             bestMove = move;
         }
     }
     if (bestMove.origin == 0 && bestMove.destination == 0) {
-        // checkmate
+        bestMove = lastValidMove;
     }
     return { bestMove, alpha }; //negamaxImplementation(chessGame, 5);
 }
@@ -168,14 +171,17 @@ MoveValue chessy::evaluate(const ChessGame& game)
     Bitboard r = bd.getRooks<side>();
     Bitboard q = bd.getQueens<side>();
     Bitboard k = bd.getKing<side>();
-    for (auto pawn : PositionSet{ p })
-        piecePoints += 0.5 / 8.0 * pawn.getRow() + 1.0;
     piecePoints += 1 * p.popcount();
     piecePoints += 3 * n.popcount();
     piecePoints += 3 * b.popcount();
     piecePoints += 5 * r.popcount();
     piecePoints += 9 * q.popcount();
 
+    //for (auto knight : PositionSet{ n })
+    //    piecePoints += KnightMoveGenerator{ knight, bd.get<side>() }.getBitboard().popcount() * 0.15;
+    for (auto bishop : PositionSet{ b })
+        piecePoints += PrimitiveBishopMoveGenerator{ bishop, bd.get<otherSide(side)>(), bd.get<side>() }.getBitboard().popcount() * 0.2;
+
     constexpr Side other = otherSide(side);
     p = bd.getPawns<other>();
     n = bd.getKnights<other>();
@@ -183,14 +189,17 @@ MoveValue chessy::evaluate(const ChessGame& game)
     r = bd.getRooks<other>();
     q = bd.getQueens<other>();
     k = bd.getKing<other>();
-    for (auto pawn : PositionSet{ p })
-        piecePoints += 0.5 / 8.0 * (7 - pawn.getRow()) + 1.0;
     piecePoints -= 1 * p.popcount();
     piecePoints -= 3 * n.popcount();
     piecePoints -= 3 * b.popcount();
     piecePoints -= 5 * r.popcount();
     piecePoints -= 9 * q.popcount();
 
+    //for (auto knight : PositionSet{ n })
+    //    piecePoints -= KnightMoveGenerator{ knight, bd.get<otherSide(side)>() }.getBitboard().popcount() * 0.2;
+    for (auto bishop : PositionSet{ b })
+        piecePoints -= PrimitiveBishopMoveGenerator{ bishop, bd.get<side>(), bd.get<otherSide(side)>() }.getBitboard().popcount() * 0.2;
+
     return piecePoints;
 }
 
@@ -198,9 +207,9 @@ MoveValue chessy::evaluate(const ChessGame& game)
 MoveValue chessy::evaluate(Side side, const ChessGame& game)
 {
     if (side == WHITE_SIDE)
-        evaluate<WHITE_SIDE>(game);
+        return evaluate<WHITE_SIDE>(game);
     else
-        evaluate<BLACK_SIDE>(game);
+        return evaluate<BLACK_SIDE>(game);
 }