浏览代码

more move stuff

Nicolas Winkler 7 年之前
父节点
当前提交
09117f75e0
共有 5 个文件被更改,包括 125 次插入17 次删除
  1. 10 5
      src/BitBoard.h
  2. 22 6
      src/MoveGeneration.h
  3. 64 3
      src/UciParser.cpp
  4. 24 0
      src/UciParser.h
  5. 5 3
      src/main.cpp

+ 10 - 5
src/BitBoard.h

@@ -46,9 +46,12 @@ struct chessy::Bitboard
 {
     U64 bits;
 
-    Bitboard        (void)                  = default;
-    Bitboard        (const Bitboard&)       = default;
-    ~Bitboard       (void)                  = default;
+    Bitboard            (void)                  = default;
+    Bitboard            (const Bitboard&)       = default;
+    Bitboard            (Bitboard&&)            = default;
+    ~Bitboard           (void)                  = default;
+    Bitboard& operator= (const Bitboard&)       = default;
+    Bitboard& operator= (Bitboard&&)            = default;
 
     inline constexpr Bitboard(U64 bits) : bits{ bits } {}
     inline static Bitboard fromIndex(Index i) { return U64(1) << i; }
@@ -104,9 +107,11 @@ struct chessy::Bitboard
 
 
 static_assert(std::is_pod<chessy::Bitboard>::value,
-        "chessy::Bitboard should be a POD structure.");
+    "chessy::Bitboard should be a POD structure.");
+static_assert(std::is_pod<chessy::Index>::value,
+    "chessy::Index should be a POD structure.");
 static_assert(sizeof(chessy::Bitboard) == sizeof(uint64_t),
-        "chessy::Bitboard should be 64 bits in size.");
+    "chessy::Bitboard should be 64 bits in size.");
 
 
 #endif // CHESSY_BITBOARD_H

+ 22 - 6
src/MoveGeneration.h

@@ -11,8 +11,10 @@ namespace chessy
     class ChessGame;
 
     struct Move;
-    class PositionSet;
 
+    class MoveGenerator;
+
+    class PositionSet;
     template<int side>
     class PawnPushGenerator;
     class KnightMoveGenerator;
@@ -31,17 +33,31 @@ struct chessy::Move
     Index origin;
     Index destination;
 
-    Move(void)          = default;
-    Move(const Move&)   = default;
-    ~Move(void)         = default;
+    Move            (void)          = default;
+    Move            (const Move&)   = default;
+    Move            (Move&&)        = default;
+    ~Move           (void)          = default;
+    Move& operator= (const Move&)   = default;
+    Move& operator= (Move&&)        = default;
 
     inline Move(Index origin, Index destination) :
-        origin{origin}, destination{destination} {}
+        origin{ origin }, destination{ destination } {}
 
     std::string asString(void) const;
 };
 
 
+class chessy::MoveGenerator
+{
+    const ChessGame& game;
+public:
+    inline MoveGenerator(const ChessGame& game) :
+        game{ game } {}
+
+
+};
+
+
 class chessy::PositionSet
 {
     Bitboard bitboard;
@@ -102,7 +118,7 @@ class chessy::PawnPushGenerator
 
         inline bool operator !=(const MoveIterator& psi) const
         {
-            pawnPushes != psi.pawnPushes;
+            return pawnPushes != psi.pawnPushes;
         }
     };
 public:

+ 64 - 3
src/UciParser.cpp

@@ -5,6 +5,17 @@
 
 using namespace std;
 
+
+const std::map<std::string, UciParser::CommandHandler> UciParser::commandHandlers = {
+    {"uci",         &UciParser::uci },
+    {"debug",       &UciParser::debug },
+    {"isready",     &UciParser::isready },
+    {"setoption",   &UciParser::setoption },
+    {"register",    &UciParser::doNothing },
+    {"ucinewgame",  &UciParser::ucinewgame },
+};
+
+
 int UciParser::parse(istream& in, ostream& out)
 {
     while (!in.eof()) {
@@ -18,13 +29,13 @@ int UciParser::parse(istream& in, ostream& out)
 
 int UciParser::executeLine(const string& line)
 {
-    stringstream s {line};
+    stringstream s{ line };
     string token;
     string command;
     vector<string> args;
     s >> command;
     while (s >> token) {
-        args.push_back(token);
+        args.push_back(std::move(token));
     }
     return executeCommand(command, args);
 }
@@ -33,5 +44,55 @@ int UciParser::executeLine(const string& line)
 int UciParser::executeCommand(const string& command,
     const vector<string>& args)
 {
-    return 0;
+    try {
+        CommandHandler ch = commandHandlers.at(command);
+        (this->*ch)(args);
+        return 0;
+    }
+    catch (out_of_range& oor) {
+        // no handler for command -> invalid command
+        return 1;
+    }
+}
+
+void UciParser::sendCommand(const std::string& command,
+    const std::vector<std::string>& args)
+{
+    cout << command;
+    std::for_each(args.begin(), args.end(), [] (auto& x) { cout << " " << x; });
+}
+
+
+void UciParser::uci(const std::vector<std::string>& args)
+{
+    sendCommand(UCIOK);
+}
+
+
+void UciParser::debug(const std::vector<std::string>& args)
+{
+    // not yet implemented
+}
+
+
+void UciParser::isready(const std::vector<std::string>& args)
+{
+    sendCommand(READYOK);
+}
+
+
+void UciParser::setoption(const std::vector<std::string>& args)
+{
+}
+
+
+void UciParser::ucinewgame(const std::vector<std::string>& args)
+{
+}
+
+
+void UciParser::doNothing(const std::vector<std::string>& args)
+{
+    // explicitly do nothing
+    return;
 }

+ 24 - 0
src/UciParser.h

@@ -4,12 +4,20 @@
 #include <iostream>
 #include <vector>
 #include <string>
+#include <algorithm>
+#include <map>
 
 /*!
  * manages the communication with a UCI connection
  */
 class UciParser
 {
+    using CommandHandler = void(UciParser::*)(const std::vector<std::string>&);
+    static const std::map<std::string, CommandHandler> commandHandlers;
+
+    static constexpr const char* UCIOK = "uciok";
+    static constexpr const char* READYOK = "readyok";
+
 public:
     UciParser(void) = default;
     virtual ~UciParser(void) = default;
@@ -19,6 +27,22 @@ public:
     virtual int executeLine(const std::string& line);
     virtual int executeCommand(const std::string& command,
                                const std::vector<std::string>& args);
+
+    virtual void sendCommand(const std::string& command,
+        const std::vector<std::string>& args = {});
+
+    virtual void uci            (const std::vector<std::string>& args);
+    virtual void debug          (const std::vector<std::string>& args);
+    virtual void isready        (const std::vector<std::string>& args);
+    virtual void setoption      (const std::vector<std::string>& args);
+    virtual void ucinewgame     (const std::vector<std::string>& args);
+
+    /*!
+     * Is needed for commands that should not trigger any action, but are
+     * still valid commands. An example would be the "register" command,
+     * since this chess engine does not have such a feature.
+     */
+    virtual void doNothing      (const std::vector<std::string>& args);
 };
 
 #endif // UCIPARSER_H

+ 5 - 3
src/main.cpp

@@ -8,6 +8,11 @@ using namespace chessy;
 
 auto main(int argc, char** argv) -> int
 {
+    UciParser uciParser;
+    uciParser.parse(cin, cout);
+    return 0;
+
+
     ChessGame cg;
     string line;
     getline(cin, line);
@@ -61,7 +66,4 @@ auto main(int argc, char** argv) -> int
         return 1;
     }
     return 0;
-    UciParser uciParser;
-    uciParser.parse(cin, cout);
-    return 0;
 }