aboutsummaryrefslogtreecommitdiff
path: root/src/board/fen.cpp
blob: f44719a7b6d8e697fcafcd4efcabb4eb89256b91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "fen.hpp"
#include "board.hpp"
#include <cassert>
#include <cctype>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <print>
#include <string>
#include <sys/types.h>

bool WHITE = true;
bool BLACK = false;

static void updateBitboards(Game *g) {
  g->PieceBitboard = 0;
  g->WhitePieceBitboard = 0;
  g->BlackPieceBitboard = 0;
  std::memset(g->PieceBitboards, 0, sizeof(g->PieceBitboards));
  for (uint8_t i = 0; i < 64; i++) {
    Piece piece = g->pieces[i];
    if (piece.type == NONEPIECE) {
      continue;
    }
    g->PieceBitboards[piece.color][piece.type] |= (1ULL << i);

    if (piece.color) {
      g->WhitePieceBitboard |= (1ULL << i);
    } else {
      g->BlackPieceBitboard |= (1ULL << i);
    }
  }
  g->PieceBitboard = g->WhitePieceBitboard | g->BlackPieceBitboard;
}
static void SetPiece(int i, PieceType type, bool color, Game *g) {
  if (i < 0 || i >= 64) {
    std::println("Fatal: piece outside board");
    assert(false && "Malformed board");
    exit(1);
  }
  g->pieces[i] = {.color = color, .type = type};
}
static void clearBoard(Game *g) {
  for (auto &piece : g->pieces) {
    piece = {.color = false, .type = NONEPIECE};
  }
  g->blackCastleKing = false;
  g->blackCastleQueen = false;
  g->whiteCastleKing = false;
  g->whiteCastleQueen = false;
}
enum parserState : std::uint8_t {
  BOARD,
  PLAYER_TO_MOVE,
  CASTLE,
  ENPASSANT,
  HALF_MOVE_CLOCK,
  FULL_MOVE_CLOCK,
};
void setBoardFen(const std::string &fen, Game *g) {
  clearBoard(g);
  // example fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
  int file = 0;
  int rank = 7;

  Position enpassant = {};

  parserState parser_state = BOARD;

  for (uint i = 0; i < fen.length(); i++) {
    char c = fen[i];
    if (c == ' ') {
      if (parser_state == BOARD) {
        parser_state = PLAYER_TO_MOVE;
        continue;
      }
      if (parser_state == PLAYER_TO_MOVE) {
        parser_state = CASTLE;
        continue;
      }
      if (parser_state == CASTLE) {
        parser_state = ENPASSANT;
        continue;
      }
      if (parser_state == ENPASSANT) {
        parser_state = HALF_MOVE_CLOCK;
        continue;
      }
      if (parser_state == HALF_MOVE_CLOCK) {
        parser_state = FULL_MOVE_CLOCK;
        continue;
      }
    }
    if (parser_state == ENPASSANT) {
      continue;
    }
    if (parser_state == CASTLE) {
      if (c == '-') {
        g->blackCastleKing = false;
        g->blackCastleQueen = false;
        g->whiteCastleKing = false;
        g->whiteCastleQueen = false;
        continue;
      }
      if (c == 'K') {
        g->whiteCastleKing = true;
        continue;
      }
      if (c == 'Q') {
        g->whiteCastleQueen = true;
        continue;
      }
      if (c == 'k') {
        g->blackCastleKing = true;
        continue;
      }
      if (c == 'q') {
        g->blackCastleQueen = true;
        continue;
      }
    }
    if (parser_state == PLAYER_TO_MOVE) {
      if (toupper(c) == 'W') {
        g->turn = WHITE;
        continue;
      }
      if (toupper(c) == 'B') {
        g->turn = BLACK;
        continue;
      }
    }
    if (parser_state == BOARD) {
      if (isdigit(c) != 0) {
        int move = c - '0';
        file += move;
        if (file > 8) {
          std::println("Fatal: file was to big");
          assert(false && "Malformed file");
          exit(1);
          return; // malformed file
        }
        continue;
      }
      if (c == '/') {
        file = 0;
        rank--;
        if (rank < 0) {
          std::println("Fatal: rank was to big");
          assert(false && "Malformed rank");
          exit(1);
          return; // malformed rank
        }
        continue;
      };
      if (toupper(c) == 'N') {
        SetPiece((rank * 8) + file, KNIGHT, isupper(c) != 0, g);
        file++;
        continue;
      };
      if (toupper(c) == 'R') {
        SetPiece((rank * 8) + file, ROOK, isupper(c) != 0, g);
        file++;
        continue;
      }
      if (toupper(c) == 'Q') {
        SetPiece((rank * 8) + file, QUEEN, isupper(c) != 0, g);
        file++;
        continue;
      }
      if (toupper(c) == 'B') {
        SetPiece((rank * 8) + file, BISHOP, isupper(c) != 0, g);
        file++;
        continue;
      }
      if (toupper(c) == 'P') {
        SetPiece((rank * 8) + file, PAWN, isupper(c) != 0, g);
        file++;
        continue;
      }
      if (toupper(c) == 'K') {
        SetPiece((rank * 8) + file, KING, isupper(c) != 0, g);
        file++;
        continue;
      }
    }
    break;
  }
  g->enPassant = enpassant;

  updateBitboards(g);

  int whiteKingCount = __builtin_popcountll(g->PieceBitboards[true][KING]);
  int blackKingCount = __builtin_popcountll(g->PieceBitboards[false][KING]);

  if (whiteKingCount != 1) {
    std::cerr << "ERROR: expected exactly one white king\n";
    exit(1);
  }
  if (blackKingCount != 1) {
    std::cerr << "ERROR: expected exactly one black king\n";
    exit(1);
  }
}