nicolaswinkler vor 8 Jahren
Ursprung
Commit
f59a8ffaa2
5 geänderte Dateien mit 68 neuen und 10 gelöschten Zeilen
  1. 5 3
      src/BitBoard.h
  2. 53 3
      src/Board.cpp
  3. 2 0
      src/Board.h
  4. 6 3
      src/ChessGame.cpp
  5. 2 1
      src/ChessGame.h

+ 5 - 3
src/BitBoard.h

@@ -27,13 +27,14 @@ union chessy::Index
 
     Index(void) = default;
     inline Index(int8_t ind) : index {ind} {}
+    inline Index(const std::string& name) : Index{name[0] - 'a', name[1] - '0'} {}
     inline Index(int row, int column) :
-        index{int8_t(row & 0x7 + (column & 0x7) << 3)} {}
+        index{int8_t(((row & 0x7) << 3) + (column & 0x7))} {}
     inline operator int8_t (void) const { return index; }
 
     inline std::array<char, 2> getName(void) const
     {
-        return {char('a' + row), char('0' + column)};
+        return {char('a' + ((index & 0x7) >> 3)), char('0' + (index & 0x7))};
     }
 };
 
@@ -47,7 +48,7 @@ struct chessy::Bitboard
     ~Bitboard       (void)                  = default;
 
     inline Bitboard (U64 bits) : bits {bits} {}
-    inline static Bitboard fromIndex(Index i) { return 1 << i; }
+    inline static Bitboard fromIndex(Index i) { return U64(1) << i; }
 
     inline void     setBit      (Index i)     { bits |= 1 << i.index; }
     inline void     unsetBit    (Index i)     { bits |= ~(1 << i.index); }
@@ -82,6 +83,7 @@ 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 explicit operator bool(void) const               { return bits != 0; }
 
 
     inline Bitboard mirror      (void) const

+ 53 - 3
src/Board.cpp

@@ -39,6 +39,24 @@ void Board::resetBoard(void)
 }
 
 
+void Board::setEmpty(void)
+{
+    whitePawns =    0;
+    whiteRooks =    0;
+    whiteKnights =  0;
+    whiteBishops =  0;
+    whiteQueens =   0;
+    whiteKing =     0;
+
+    blackPawns =    0;
+    blackRooks =    0;
+    blackKnights =  0;
+    blackBishops =  0;
+    blackQueens =   0;
+    blackKing =     0;
+}
+
+
 bool Board::tryToMove(Bitboard start, Bitboard end, Bitboard& b)
 {
     if (start & b)
@@ -63,7 +81,7 @@ void Board::setBoard(const std::string& fenPosition)
         {'Q', &blackQueens},
         {'K', &blackKing}
     };
-
+    setEmpty();
     for (auto character : fenPosition) {
         if (character >= '0' && character <= '9') {
             column += character - '0';
@@ -79,20 +97,52 @@ void Board::setBoard(const std::string& fenPosition)
             else {
                 throw runtime_error("invalid piece type "s + character);
             }
+            ++ column;
         }
     }
 }
 
 
+// a bit hacky, sry
 std::string Board::getFenBoard(void) const
 {
     using namespace std;
     stringstream str;
     for (int row = 0; row < 8; row++) {
         int counter = 0;
-        for (int column; column < 8; column++) {
-
+        for (int column = 0; column < 8; column++) {
+            PieceType black = getBlackAtPosition({row, column});
+            PieceType white = getWhiteAtPosition({row, column});
+            //str << " " << Bitboard::fromIndex({row, column}) << " " << " ";;
+            //str << "row,column,index: " << row << "," << column << "," <<
+            //    int(Index(row, column).index) << " ";
+
+            if (black == PieceType::EMPTY && white == PieceType::EMPTY)
+                ++ counter;
+            else if (counter > 0) {
+                str << counter;
+                counter = 0;
+            }
+            switch (black) {
+                case PieceType::PAWN:   str << 'P'; break;
+                case PieceType::KNIGHT: str << 'N'; break;
+                case PieceType::BISHOP: str << 'B'; break;
+                case PieceType::ROOK:   str << 'R'; break;
+                case PieceType::QUEEN:  str << 'Q'; break;
+                case PieceType::KING:   str << 'K'; break;
+                default:
+                switch (white) {
+                    case PieceType::PAWN:   str << 'p'; break;
+                    case PieceType::KNIGHT: str << 'n'; break;
+                    case PieceType::BISHOP: str << 'b'; break;
+                    case PieceType::ROOK:   str << 'r'; break;
+                    case PieceType::QUEEN:  str << 'q'; break;
+                    case PieceType::KING:   str << 'k'; break;
+                }
+            }
         }
+        if (counter > 0)
+            str << counter;
         if (row != 7)
             str << "/";
     }

+ 2 - 0
src/Board.h

@@ -55,6 +55,8 @@ public:
      */
     void resetBoard(void);
 
+    void setEmpty(void);
+
 private:
     bool tryToMove(Bitboard start, Bitboard end, Bitboard& b);
 public:

+ 6 - 3
src/ChessGame.cpp

@@ -46,9 +46,10 @@ void ChessGame::loadFromFen(const std::string& fenString)
     tokenizer >> moveCount;
 
     this->board.setBoard(board);
+    this->enPassant = Index {enPassant};
 
-    if (turn == "w") this->turn = 0;
-    else if (turn == "b") this->turn = 1;
+    if (turn == "w") this->turn = Turn::WHITE;
+    else if (turn == "b") this->turn = Turn::BLACK;
     else throw runtime_error("invalid turn "s + turn);
 
     canCastleQueenSideWhite = false;
@@ -99,6 +100,8 @@ std::string ChessGame::generateFen(void) const
 
     string board = this->board.getFenBoard();
 
+    string turn = this->turn == Turn::WHITE ? "w" : "b";
+ 
     string castlingRights = ""s +
         (canCastleKingSideBlack ? "K" : "") +
         (canCastleQueenSideBlack ? "Q" : "") +
@@ -116,7 +119,7 @@ std::string ChessGame::generateFen(void) const
     string halfmoves = to_string(reversibleHalfMoves);
     string mCount = to_string(moveCount);
 
-    return board + " " + castlingRights + " " + enPassant + " " + halfmoves +
+    return board + " " + turn + " " + castlingRights + " " + enPassant + " " + halfmoves +
        " " + mCount;
 }
 

+ 2 - 1
src/ChessGame.h

@@ -26,8 +26,9 @@ class chessy::ChessGame
     //! -1 for no en passant possible
     Index enPassant =              -1;
 
+    enum class Turn { WHITE, BLACK };
     //! 0 for white, 1 for black
-    char turn =                     0;
+    Turn turn =                     Turn::WHITE;
 
     int moveCount =                 0;