aboutsummaryrefslogtreecommitdiff
path: root/src/uci.cpp
blob: b4e384aeb4f848bc2e3b0b44fff3ded72dbc9467 (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
#include "uci.hpp"
#include "board.hpp"
#include "bot.hpp"
#include "fen.hpp"
#include "moves.hpp"

#include <algorithm>
#include <cassert>
#include <cctype>
#include <iostream>
#include <print>
#include <sstream>
#include <string>

#include <chrono>
#include <fstream>
#include <iomanip>
#include <unordered_map>

std::ofstream uciLog("uci_log.txt", std::ios::app);

void LogUci(const std::string &message) {
  if (!uciLog.is_open())
    return;

  auto now = std::chrono::system_clock::now();
  auto time = std::chrono::system_clock::to_time_t(now);

  uciLog << "[" << std::put_time(std::localtime(&time), "%H:%M:%S") << "] "
         << message << "\n";

  uciLog.flush();
}
using namespace std;

Move UciToMove(string uci) {
  Position from;
  Position to;

  from.file = uci[0] - 'a';
  from.rank = '8' - uci[1];

  to.file = uci[2] - 'a';
  to.rank = '8' - uci[3];

  return {from, to};
}
Game initBoard(string startingFEN,
               std::unordered_map<uint64_t, float> *transPositions) {
  Game b{};
  b.enPassant = IndexToPosition(0); // default value
  b.canEnpassant = false;
  b.state = TURN;
  b.turn = true;
  b.castle = "";
  b.halfMoveClock = 0;
  b.MoveClock = 0;
  b.Transpositions = transPositions;

  Piece EmptyPiece = {
      false,
      NONEPIECE,
      false,
  };
  for (int i = 0; i < 64; i++) {
    b.pieces[i] = EmptyPiece;
  };

  setBoardFen(startingFEN, &b);

  return b;
};
void Uci() {
  unordered_map<uint64_t, float> transpositions = {};
  Game game =
      initBoard("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
                &transpositions);

  string line;

  while (getline(cin, line)) {
    string cmd = line;
    LogUci("GUI -> ENGINE: " + line);
    transform(cmd.begin(), cmd.end(), cmd.begin(), ::tolower);

    if (cmd == "quit") {
      break;
    }
    if (cmd == "uci") {
      cout << "uciok\n";
      cout.flush();
    } else if (cmd == "ucinewgame") {
      game =
          initBoard("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
                    &transpositions);
    } else if (cmd == "isready") {
      cout << "readyok\n";
      cout.flush();
    } else if (cmd.starts_with("go")) {
      Move best = EngineGetBestMove(&game);

      cout << "bestmove ";
      cout << (char)(best.From.file + 'a') << 8 - best.From.rank
           << (char)(best.To.file + 'a') << 8 - best.To.rank;
      if (best.promotion != NONEPIECE) {
        switch (best.promotion) {
        case QUEEN:
          cout << "q";
          break;
        case ROOK:
          cout << "r";
          break;
        case KNIGHT:
          cout << "n";
          break;
        case BISHOP:
          cout << "b";
          break;
        default:
          assert(false && "Unexepted promotion type");
        }
      }
      println();

      cout.flush();
    } else if (cmd.starts_with("position")) {
      stringstream ss(line);
      string token;

      ss >> token; // position

      ss >> token;

      if (token == "startpos") {
        game = initBoard(
            "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
            &transpositions);
      } else if (token == "fen") {
        string fen;
        string part;

        // FEN has spaces, need 6 parts
        for (int i = 0; i < 6; i++) {
          ss >> part;
          if (i)
            fen += " ";
          fen += part;
        }

        game = initBoard(fen, &transpositions);
      }

      // check for moves
      while (ss >> token) {
        if (token == "moves")
          break;
      }

      while (ss >> token) {
        Move move = UciToMove(token);

        MakeMove(move, &game);
      }
    }
  }
}