aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: ba5e57c9fa74bebcae940571c6d7c4963b93d46e (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
#include "board.hpp"
#include "fen.hpp"  
#include "moves.hpp"
#include <algorithm>
#include <cctype>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <print>
#include <string>
#include <vector>

Piece NonePiece = {
    false,
    NONE,
    false,
};
#include <iostream>

bool isValidChessRank(char rank) {
  if (rank >= '1' && rank <= '8') {
    return true;
  } else {
    return false;
  }
}
bool isValidChessFile(char rank) {
  if (rank >= 'A' && rank <= 'H') {
    return true;
  } else {
    return false;
  }
}
void PrintMoves(const std::vector<Move> &moves) {
  std::cout << "Moves (" << moves.size() << "):\n";

  for (const Move &move : moves) {
    std::cout << "(" << (int)move.From.file << ", " << (int)move.From.rank
              << ") -> (" << (int)move.To.file << ", " << (int)move.To.rank
              << ")\n";
  }
}
int main(int argc, char **argv) {
  std::string startingFen = "rnbqkbnr/pppppppp/8/8/8/8/8/RNBQKBNR w KQkq - 0 1";
  if (argc < 1 || argc > 2) {
    printf("wrong arg count, use: ./chess [optinal starting fen]\n");
    return 1;
  }
  printf("arg count: %d\n", argc);
  if (argc == 2) {
    startingFen = argv[1];
  }
  board b;
  b.turn = true;
  b.castle = "";
  b.halfMoveClock = 0;
  b.MoveClock = 0;
  for (int i = 0; i < 64; i++) {
    b.pieces[i] = NonePiece;
  };
  setBoardFen(startingFen, &b);
  printBoard(&b);
  while (true) {
    std::cout << "> ";
    std::string command;
    std::cin >> command;
    std::cout << command << "\n";

    transform(command.begin(), command.end(), command.begin(), ::toupper);
    if (command == "EXIT") {
      std::println();
      std::cout << "Exiting...\n";
      return 0;
    }

    if (command.length() != 4) {
      std::cout << "Unknown Command, use EXIT to exit";
      std::println();
      continue;
    }
    if (!isValidChessFile(command[0])) {
      std::cout << "ERROR: Expected valid file at first place";
      std::println();
      continue;
    }

    if (!isValidChessRank(command[1])) {
      std::cout << "ERROR: Expected valid rank at second place";
      std::println();
      continue;
    }

    if (!isValidChessFile(command[2])) {
      std::cout << "ERROR: Expected valid file at third place";
      std::println();
      continue;
    }

    if (!isValidChessRank(command[3])) {
      std::cout << "ERROR: Expected valid rank at four place";
      std::println();
      continue;
    }

    // we know its a valid chess pos
    int fromFile = command[0] - 'A';
    int fromRank = '8' - command[1];
    int toFile = command[2] - 'A';
    int toRank = '8' - command[3];
    std::cout << fromFile << "\n";
    std::cout << fromRank << "\n";
    std::cout << toFile << "\n";
    std::cout << toRank << "\n";
    Move move = {
        {static_cast<uint8_t>(fromRank), static_cast<uint8_t>(fromFile)},
        {static_cast<uint8_t>(toRank), static_cast<uint8_t>(toFile)}};

    auto moves = GetLegalMoves(&b);
    if (std::ranges::find(moves, move) != moves.end()) {
      std::cout << "Move is legal!\n";
    } else {
      std::cout << "Move is not legal!\n";
      PrintMoves(moves);
      continue;
    }
    std::println();
    PlayMove(move, &b);
    b.MoveClock++;
    b.turn = !b.turn;
    printBoard(&b);
  }
  return EXIT_SUCCESS;
}