Просмотр исходного кода

Merge branch 'master' of https://gitlab.vis.ethz.ch/niwinkle/chessy

Signed-off-by: Nicolas Winkler <nicolas.winkler@gmx.ch>
Nicolas Winkler 7 лет назад
Родитель
Сommit
3172061b34
7 измененных файлов с 36 добавлено и 8 удалено
  1. 12 1
      src/ChessGame.cpp
  2. 1 0
      src/ChessGame.h
  3. 6 3
      src/Minimax.cpp
  4. 12 0
      src/UciParser.cpp
  5. 1 0
      src/UciParser.h
  6. 3 3
      src/main.cpp
  7. 1 1
      src/makefile

+ 12 - 1
src/ChessGame.cpp

@@ -152,6 +152,14 @@ UndoInfo ChessGame::doMove(const MoveInfo& mi)
 {
     UndoInfo ui;
     ui.before = board;
+    board.move(mi.move);
+    turn = otherSide(turn);
+    moveCount++;
+    if(mi.movedPiece != PAWN &&
+            board.getAtPosition(mi.move.destination) != EMPTY) {
+        reversibleHalfMoves++;
+        ui.decrementReversibleHalfMoves = true;
+    }
     return ui;
 }
 
@@ -159,5 +167,8 @@ UndoInfo ChessGame::doMove(const MoveInfo& mi)
 void ChessGame::undoMove(const UndoInfo& ui)
 {
     board = ui.before;
-    this->reversibleHalfMoves--;
+    turn = otherSide(turn);
+    moveCount--;
+    if (ui.decrementReversibleHalfMoves)
+        reversibleHalfMoves--;
 }

+ 1 - 0
src/ChessGame.h

@@ -38,6 +38,7 @@ struct chessy::MoveInfo
 struct chessy::UndoInfo
 {
     Board before;
+    bool decrementReversibleHalfMoves;
 };
 
 

+ 6 - 3
src/Minimax.cpp

@@ -12,7 +12,7 @@ size_t Perft::search(void)
 {
     size_t result = 0;
     auto searcher = [&result] (const MoveInfo& mi, ChessGame& cg, int depth) {
-        if (depth > 0) {
+        if (depth > 1) {
             UndoInfo ui = cg.doMove(mi);
             Perft p{ depth - 1, cg };
             result += p.search();
@@ -25,7 +25,10 @@ size_t Perft::search(void)
 
     Search<decltype(searcher)> search (searcher, chessGame);
 
-    search.iterateAll<WHITE_SIDE>(chessGame, depth);
+    if (chessGame.getTurn() == WHITE_SIDE)
+        search.iterateAll<WHITE_SIDE>(chessGame, depth);
+    else 
+        search.iterateAll<BLACK_SIDE>(chessGame, depth);
 
     return result;
 }
@@ -261,4 +264,4 @@ MiniMax::BestMove MiniMax::minimax(int depth)
     bestMove.value *= 0.9f;
     bestMove.value += v * 0.2f;
     return -bestMove;
-}
+}

+ 12 - 0
src/UciParser.cpp

@@ -18,6 +18,7 @@ const map<std::string, UciParser::CommandHandler> UciParser::commandHandlers = {
     { "stop",        &UciParser::stop },
     { "quit",        &UciParser::quit },
     { "getfen",      &UciParser::getfen },
+    { "perft",       &UciParser::perft },
 };
 
 
@@ -192,6 +193,17 @@ void UciParser::getfen(const vector<string>& args)
 }
 
 
+void UciParser::perft(const vector<string>& args)
+{
+    using chessy::Perft;
+    if (!args.empty()) {
+        int depth = std::stoi(args[0]);
+        Perft p{ depth, cg };
+        out << "Nodes searched: " << p.search() << endl;
+    }
+}
+
+
 void UciParser::doNothing(const vector<string>& args)
 {
     // explicitly do nothing

+ 1 - 0
src/UciParser.h

@@ -84,6 +84,7 @@ public:
 
     // engine-specific extension commands
     virtual void getfen         (const std::vector<std::string>& args);
+    virtual void perft          (const std::vector<std::string>& args);
 
     /*!
      * Is needed for commands that should not trigger any action, but are

+ 3 - 3
src/main.cpp

@@ -13,10 +13,10 @@ using namespace chessy;
  */
 auto main(int argc, char** argv) -> int
 {
-    ChessGame cg;
-    Perft p{ 1 , cg };
+    /*ChessGame cg;
+    Perft p{ 2 , cg };
     cout << p.search() << endl;
-    return 0;
+    return 0;*/
     /*ChessGame cg{ "8/1pP5/3Qp2p/2K1P1P1/1p1p4/4p3/k2P3P/4b1n1 w - - 0 1" };
     auto printer = [](const MoveInfo& mi) {
         cout << mi.move.asString() << endl;

+ 1 - 1
src/makefile

@@ -23,7 +23,7 @@ all: chessy
 
 release: CXXFLAGS += -O3
 release: $(EXECUTABLE)
-	$(STRIP) $(EXECUTABLE)
+#	$(STRIP) $(EXECUTABLE)
 
 flto: CXXFLAGS += -flto 
 flto: LNFLAGS += -flto