nicolaswinkler 7 years ago
parent
commit
b6a4b04cc6
4 changed files with 44 additions and 18 deletions
  1. 20 16
      src/Minimax.cpp
  2. 21 1
      src/MoveGeneration.cpp
  3. 2 0
      src/MoveGeneration.h
  4. 1 1
      src/UciParser.cpp

+ 20 - 16
src/Minimax.cpp

@@ -70,10 +70,14 @@ std::pair<Move, MoveValue> chessy::miniMax(ChessGame& cg, int depth)
 {
     std::vector<Move> moves;
     moves.reserve(200);
-    if (cg.getTurn() == WHITE_SIDE)
+    if (cg.getTurn() == WHITE_SIDE) {
         generateAllMoves<WHITE_SIDE>(cg, moves);
-    else
+        orderMoves<WHITE_SIDE>(cg, moves);
+    }
+    else {
         generateAllMoves<BLACK_SIDE>(cg, moves);
+        orderMoves<BLACK_SIDE>(cg, moves);
+    }
 
     const Board& b = cg.getBoard();
     auto isCheck = [&cg, &b] () {
@@ -118,23 +122,27 @@ MoveValue chessy::negamaxImplementation(ChessGame& cg, int depth,
     }
 
     const Board& b = cg.getBoard();
-    auto isCheck = [&cg, &b] () {
-        return cg.getTurn() == BLACK_SIDE ? b.isCheck<WHITE_SIDE>() :
+    auto isCheck = [&cg, &b] (Side turn) {
+        return turn == BLACK_SIDE ? b.isCheck<WHITE_SIDE>() :
             b.isCheck<BLACK_SIDE>();
     };
 
     std::vector<Move> moves;
-    if (cg.getTurn() == WHITE_SIDE)
+    if (cg.getTurn() == WHITE_SIDE) {
         generateAllMoves<WHITE_SIDE>(cg, moves);
-    else
+        orderMoves<WHITE_SIDE>(cg, moves);
+    }
+    else {
         generateAllMoves<BLACK_SIDE>(cg, moves);
+        orderMoves<BLACK_SIDE>(cg, moves);
+    }
 
     bool thereIsMove = false;
     for (Move move : moves) {
         MoveInfo mi{ move, cg };
         UndoInfo ui = cg.doMove(mi);
         MoveValue val;
-        if (isCheck())
+        if (isCheck(cg.getTurn()))
             val = -1e+30;
         else {
             val = -negamaxImplementation(cg, depth - 1, -beta, -alpha);
@@ -147,7 +155,7 @@ MoveValue chessy::negamaxImplementation(ChessGame& cg, int depth,
             alpha = val;
     }
     if (!thereIsMove)
-        return 0.0;
+        return isCheck(otherSide(cg.getTurn())) ? -1e+30 : 0.0;
     return alpha;
 }
 
@@ -168,10 +176,8 @@ MoveValue chessy::evaluate(const ChessGame& game)
     piecePoints += 1 * p.popcount();
     piecePoints += 3 * n.popcount();
     piecePoints += 3 * b.popcount();
-    piecePoints += 4 * r.popcount();
-    piecePoints += 6 * q.popcount();
-    if (k == Bitboard(0ULL))
-        piecePoints -= 100000;
+    piecePoints += 5 * r.popcount();
+    piecePoints += 9 * q.popcount();
 
     constexpr Side other = otherSide(side);
     p = bd.getPawns<other>();
@@ -185,10 +191,8 @@ MoveValue chessy::evaluate(const ChessGame& game)
     piecePoints -= 1 * p.popcount();
     piecePoints -= 3 * n.popcount();
     piecePoints -= 3 * b.popcount();
-    piecePoints -= 4 * r.popcount();
-    piecePoints -= 6 * q.popcount();
-    if (k == Bitboard(0ULL))
-        piecePoints += 100000;
+    piecePoints -= 5 * r.popcount();
+    piecePoints -= 9 * q.popcount();
 
     return piecePoints;
 }

+ 21 - 1
src/MoveGeneration.cpp

@@ -381,7 +381,7 @@ template<Side side>
 void chessy::generateEnPassant(const ChessGame& cg, std::vector<Move>& moves)
 {
     if (cg.getEnPassantIndex() != -1) {
-        Bitboard mask = side == WHITE_SIDE ? 0x000000FF00000000;
+        //Bitboard mask = side == WHITE_SIDE ? 0x000000FF00000000;
     }
 }
 
@@ -402,6 +402,23 @@ void chessy::generateAllMoves(const ChessGame& cg, std::vector<Move>& moves)
 }
 
 
+template<Side side>
+void chessy::orderMoves(const ChessGame& cg, std::vector<Move>& moves)
+{
+    auto a = moves.begin();
+    auto b = moves.end() - 1;
+    Bitboard enemies = cg.getBoard().get<otherSide(side)>();
+    while(a < b) {
+        while (a < b && (Bitboard::fromIndex(a->destination) & enemies))
+            ++a;
+        while (!(Bitboard::fromIndex(b->destination) & enemies) && a < b)
+            --b;
+        if (a < b)
+            std::iter_swap(a, b);
+    }
+}
+
+
 // explicit instatiations
 namespace chessy
 {
@@ -424,4 +441,7 @@ namespace chessy
 
     template void generateAllMoves<WHITE_SIDE>(const ChessGame&, std::vector<Move>&);
     template void generateAllMoves<BLACK_SIDE>(const ChessGame&, std::vector<Move>&);
+
+    template void orderMoves<WHITE_SIDE>(const ChessGame&, std::vector<Move>&);
+    template void orderMoves<BLACK_SIDE>(const ChessGame&, std::vector<Move>&);
 }

+ 2 - 0
src/MoveGeneration.h

@@ -56,6 +56,8 @@ namespace chessy
     void generateEnPassant(const ChessGame& cg, std::vector<Move>& moves);
     template<Side side>
     void generateAllMoves(const ChessGame& cg, std::vector<Move>& moves);
+    template<Side side>
+    void orderMoves(const ChessGame& cg, std::vector<Move>& moves);
 }
 
 

+ 1 - 1
src/UciParser.cpp

@@ -156,7 +156,7 @@ void UciParser::go(const vector<string>& args)
 
     chessy::Move bestMove;
     chessy::MoveValue value;
-    int depth = 5;
+    int depth = 6;
     tie(bestMove, value) = chessy::miniMax(this->cg, depth);
 
     write("info", "depth", depth, "score", "cp", value);