nicolaswinkler 8 jaren geleden
bovenliggende
commit
473f6d5d43
6 gewijzigde bestanden met toevoegingen van 165 en 19 verwijderingen
  1. 54 6
      src/BitBoard.h
  2. 18 0
      src/BitOperations.h
  3. 2 2
      src/Board.cpp
  4. 3 10
      src/Board.h
  5. 1 1
      src/ChessGame.h
  6. 87 0
      src/MoveGeneration.h

+ 54 - 6
src/BitBoard.h

@@ -4,14 +4,31 @@
 #include <cinttypes>
 #include <type_traits>
 
+
 namespace chessy
 {
     using U64 = uint64_t;
-    using Index = int8_t;
+    union Index;
 
     struct Bitboard;
 }
 
+union chessy::Index
+{
+    int8_t index;
+
+    // Anonymous structs are actually not allowed in C++.
+    // Fortunately any sane compiler still supports them.
+    struct {
+        uint8_t column : 3;
+        uint8_t row    : 3;
+    };
+
+    Index() = default;
+    inline Index(int8_t ind) : index {ind} {}
+    inline operator int8_t (void) const { return index; }
+};
+
 
 struct chessy::Bitboard
 {
@@ -20,8 +37,9 @@ struct chessy::Bitboard
     Bitboard        (void)                  = default;
     Bitboard        (const Bitboard&)       = default;
     ~Bitboard       (void)                  = default;
-    inline Bitboard (U64 bits) : bits(bits) {}
-    inline Bitboard (Index row, Index column) : bits(row + column * 8) {}
+
+    inline Bitboard (U64 bits) : bits {bits} {}
+    inline static Bitboard fromIndex(Index i) { return 1 << i; }
 
     inline void     setBit      (int i)     { bits |= 1 << i; }
     inline void     unsetBit    (int i)     { bits |= ~(1 << i); }
@@ -45,6 +63,10 @@ struct chessy::Bitboard
     inline void     moveSEOne   (void)      { bits = (bits >> 9) & ~aColumn; }
     inline void     moveSWOne   (void)      { bits = (bits >> 7) & ~hColumn; }
 
+    inline void     operator &= (const Bitboard& b)         { bits &= b.bits; }
+    inline void     operator |= (const Bitboard& b)         { bits |= b.bits; }
+    inline void     operator ^= (const Bitboard& b)         { bits ^= b.bits; }
+
     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 ^  (const Bitboard& b) const   { return bits ^ b.bits; }
@@ -52,15 +74,41 @@ struct chessy::Bitboard
     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; }
+
+
+    inline Bitboard mirror      (void) const
+    {
+#if __GNUC__ > 4 && __GNUC_MINOR__ >= 3
+        return __builtin_bswap64(bits);
+#else
+        return (bits << 56) |
+            ((bits & 0xFF00) << 40) |
+            ((bits & 0xFF0000) << 24) |
+            ((bits & 0xFF000000) << 8) |
+            ((bits & 0xFF00000000) >> 8) |
+            ((bits & 0xFF0000000000) >> 24) |
+            ((bits & 0xFF000000000000) >> 40) |
+            (bits >> 56);
+#endif
+    }
+
+    static int trailingZeroes   (U64 x) {
+#if __GNUC__ > 4 && __GNUC_MINOR__ >= 5
+        return __builtin_ctzll(x);
+#else
+        for (unsigned char i = 0; i < 64; i++)
+            if (x & (1 << i))
+                return i;
+#endif
+    }
 };
 
 
 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 */
+#endif // CHESSY_BITBOARD_H
+

+ 18 - 0
src/BitOperations.h

@@ -0,0 +1,18 @@
+#ifndef CHESSY_BITOPERATIONS_H
+#define CHESSY_BITOPERATIONS_H 
+
+
+namespace chessy
+{
+
+
+
+
+
+
+
+}
+
+
+#endif // CHESSY_BITOPERATIONS_H
+

+ 2 - 2
src/Board.cpp

@@ -31,8 +31,8 @@ bool Board::tryToMove(Bitboard start, Bitboard end, Bitboard& b)
 
 void Board::applyMove(const Move& move)
 {
-    Bitboard start {move.startRow, move.startColumn};
-    Bitboard end {move.endRow, move.endColumn};
+    Bitboard start = Bitboard::fromIndex(move.origin);
+    Bitboard end = Bitboard::fromIndex(move.destination);
 
     tryToMove(start, end, whitePawns);
     tryToMove(start, end, whiteRooks);

+ 3 - 10
src/Board.h

@@ -2,23 +2,14 @@
 #define CHESSY_BOARD_H
 
 #include "BitBoard.h"
+#include "MoveGeneration.h"
 
 namespace chessy
 {
-    struct Move;
     class Board;
 }
 
 
-struct chessy::Move
-{
-    Index startRow;
-    Index startColumn;
-    Index endRow;
-    Index endColumn;
-};
-
-
 class chessy::Board
 {
     Bitboard whitePawns;
@@ -81,6 +72,8 @@ public:
      *      is 0, every free field 1
      */
     Bitboard getFree(void) const;
+
+    
 };
 
 #endif // CHESSY_BOARD_H

+ 1 - 1
src/ChessGame.h

@@ -22,7 +22,7 @@ class chessy::ChessGame
 
     short reversibleHalfMoves =     0;
 
-    Index enPassantRow =           -1;
+    char enPassantRow =            -1;
     char turn =                     0;
 
     const static char WHITE =       0;

+ 87 - 0
src/MoveGeneration.h

@@ -0,0 +1,87 @@
+#ifndef CHESSY_MOVEGENERATION_H
+#define CHESSY_MOVEGENERATION_H 
+
+#include "BitBoard.h"
+
+namespace chessy
+{
+    struct Move;
+    
+    class PositionSet;
+
+    class MoveGenerator;
+}
+
+
+struct chessy::Move
+{
+    Index origin;
+    Index destination;
+};
+
+
+class chessy::PositionSet
+{
+    Bitboard bitboard;
+
+    struct PositionSetIterator
+    {
+        Bitboard bitboard;
+
+        inline Index operator *(void) const
+        {
+            //if (bitboard == Bitboard(0)) return -1;
+            return Bitboard::trailingZeroes(bitboard.bits);
+        } 
+
+        inline void operator ++(void)
+        {
+            // remove LSB
+            bitboard.bits &= bitboard.bits - 1;
+        }
+
+        inline bool operator !=(const PositionSetIterator& psi) const
+        {
+            return bitboard != psi.bitboard;
+        }
+    };
+public:
+    
+    inline PositionSetIterator begin(void) const
+    {
+        return PositionSetIterator { bitboard };
+    }
+    inline PositionSetIterator end(void) const
+    {
+        return PositionSetIterator { 0 };
+    }
+};
+
+
+class chessy::MoveGenerator
+{
+    struct MoveIterator
+    {
+        inline Move operator *(void) const
+        {
+            // TODO: implement
+        }
+
+        inline void operator ++(void)
+        {
+            // TODO: implement
+        }
+
+        inline bool operator !=(const MoveIterator& psi) const
+        {
+            // TODO: implement
+        }
+    };
+public:
+    MoveGenerator(void);
+    //PawnPushProducer generatePawnPushes(void) const;
+};
+
+
+
+#endif // CHESSY_MOVEGENERATION_H