nicolaswinkler 8 lat temu
rodzic
commit
9a79a134d8
6 zmienionych plików z 85 dodań i 11 usunięć
  1. 11 3
      src/BitBoard.h
  2. 19 0
      src/Board.cpp
  3. 19 0
      src/Board.h
  4. 16 0
      src/ChessGame.cpp
  5. 12 7
      src/ChessGame.h
  6. 8 1
      src/makefile

+ 11 - 3
src/BitBoard.h

@@ -7,6 +7,7 @@
 namespace chessy
 {
     using U64 = uint64_t;
+    using Index = int8_t;
 
     struct Bitboard;
 }
@@ -20,6 +21,7 @@ struct chessy::Bitboard
     Bitboard        (const Bitboard&)       = default;
     ~Bitboard       (void)                  = default;
     inline Bitboard (U64 bits) : bits(bits) {}
+    inline Bitboard (Index row, Index column) : bits(row + column * 8) {}
 
     inline void     setBit      (int i)     { bits |= 1 << i; }
     inline void     unsetBit    (int i)     { bits |= ~(1 << i); }
@@ -47,12 +49,18 @@ struct chessy::Bitboard
     inline Bitboard operator |  (const Bitboard& b) const   { return bits | b.bits; }
     inline Bitboard operator ^  (const Bitboard& b) const   { return bits ^ b.bits; }
     inline Bitboard operator ~  (void) const                { return ~bits; }
+    inline bool     operator == (const Bitboard& b) const   { return bits == b.bits; }
+    inline bool     operator != (const Bitboard& b) const   { return bits != b.bits; }
+    inline          operator U64(void) const                { return bits; }
 };
 
 
-static_assert(std::is_pod<chessy::Bitboard>().value, "chessy::Bitboard should be a POD structure.");
-static_assert(std::is_trivial<chessy::Bitboard>().value, "chessy::Bitboard should be a trivial structure.");
-static_assert(sizeof(chessy::Bitboard) == sizeof(uint64_t), "chessy::Bitboard should be 64 bits in size.");
+static_assert(std::is_pod<chessy::Bitboard>().value,
+        "chessy::Bitboard should be a POD structure.");
+static_assert(std::is_trivial<chessy::Bitboard>().value,
+        "chessy::Bitboard should be a trivial structure.");
+static_assert(sizeof(chessy::Bitboard) == sizeof(uint64_t),
+        "chessy::Bitboard should be 64 bits in size.");
 
 
 #endif /* CHESSY_BITBOARD_H */

+ 19 - 0
src/Board.cpp

@@ -22,6 +22,25 @@ void Board::resetBoard(void)
 }
 
 
+bool Board::tryToMove(Bitboard start, Bitboard end, Bitboard& b)
+{
+    if (start & b)
+        b = b ^ start ^ end;
+}
+
+
+void Board::applyMove(const Move& move)
+{
+    Bitboard start {move.startRow, move.startColumn};
+    Bitboard end {move.endRow, move.endColumn};
+
+    tryToMove(start, end, whitePawns);
+    tryToMove(start, end, whiteRooks);
+    tryToMove(start, end, whiteKnights);
+    tryToMove(start, end, whiteBishops);
+}
+
+
 Bitboard Board::getWhites(void) const
 {
     return

+ 19 - 0
src/Board.h

@@ -5,10 +5,20 @@
 
 namespace chessy
 {
+    struct Move;
     class Board;
 }
 
 
+struct chessy::Move
+{
+    Index startRow;
+    Index startColumn;
+    Index endRow;
+    Index endColumn;
+};
+
+
 class chessy::Board
 {
     Bitboard whitePawns;
@@ -35,6 +45,15 @@ public:
      */
     void resetBoard(void);
 
+private:
+    bool tryToMove(Bitboard start, Bitboard end, Bitboard& b);
+public:
+
+    /*!
+     * applies a move to the board
+     */
+    void applyMove(const Move& move);
+
     /*!
      * \return
      *      a bitboard where every field occupied by a white piece

+ 16 - 0
src/ChessGame.cpp

@@ -2,3 +2,19 @@
 #include <string>
 
 using namespace chessy;
+
+
+bool ChessGame::isValidMove(const Move& move) const
+{
+    return false;
+}
+
+
+std::vector<Move> ChessGame::getValidMoves(void) const
+{
+    std::vector<Move> ret;
+    return ret;
+}
+
+
+

+ 12 - 7
src/ChessGame.h

@@ -1,6 +1,8 @@
 #ifndef CHESSY_CHESSGAME_H
 #define CHESSY_CHESSGAME_H
 
+#include <vector>
+
 #include "Board.h"
 
 namespace chessy
@@ -13,21 +15,24 @@ class chessy::ChessGame
 {
     Board board;
 
-    bool canCastleQueenSideWhite;
-    bool canCastleQueenSideBlack;
-    bool canCastleKingSideWhite;
-    bool canCastleKingSideBlack;
+    bool canCastleQueenSideWhite =  true;
+    bool canCastleQueenSideBlack =  true;
+    bool canCastleKingSideWhite =   true;
+    bool canCastleKingSideBlack =   true;
 
-    short reversibleHalfMoves;
+    short reversibleHalfMoves =     0;
 
-    char enPassantWhite;
-    char enPassantBlack;
+    Index enPassantWhite =          -1;
+    Index enPassantBlack =          -1;
 
 public:
     ChessGame(void) = default;
     ChessGame(const ChessGame&) = default;
     ~ChessGame(void) = default;
 
+    bool isValidMove(const Move& move) const;
+    std::vector<Move> getValidMoves(void) const;
 };
 
 #endif // CHESSY_CHESSGAME_H
+

+ 8 - 1
src/makefile

@@ -1,6 +1,6 @@
 IDIR=       .
 CXX=        g++
-CXXFLAGS=   -std=c++14 -O3
+CXXFLAGS=   -std=c++14
 LNFLAGS=    
 DEPS=       Bitfield.h
 OBJ=        main.o Board.o ChessGame.o UciParser.o
@@ -12,6 +12,13 @@ OBJ=        main.o Board.o ChessGame.o UciParser.o
 chessy: $(OBJ)
 	$(CXX) -o $@ $^ $(LNFLAGS)
 
+release: CXXFLAGS += -O3
+release: chessy
+
+flto: CXXFLAGS += -flto
+flto: LNFLAGS += -flto
+flto: release
+
 .PHONY: clean
 
 clean: