summaryrefslogtreecommitdiff
path: root/src/main.c
blob: cbdd8627c56bf9d24f8f82357203fff41dcec250 (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
#include <locale.h>
#include <stdio.h>
#include <string.h>

#include "config.h"
#include "pieces.h"


void print_board(int board[128]) {
  for (int rank = 7; rank >= 0; rank--) {
    printf("%i|", rank + 1);
    for (int file = 0; file < 8; file++) {
      int piece = board[rank * 16 + file];
      printf("%s ", pieces[piece]);
    }
    printf("\n");
  }
  printf("  a b c d e f g h\n");
}

void parse_FEN(char* FEN, int board[128]) {
  // TODO: parse everything, not position only
  int rank = 7;
  int file = 0;
  size_t lenght = strlen(FEN);

  for (int k = 0; k < lenght; k++) {
    if (FEN[k] > '0' && FEN[k] <= '8') {
      int last_file = file + FEN[k] - '0';
      for (file; file < last_file; file++) {
        board[rank * 16 + file] = EMPTY;
      }
    } else {
      switch (FEN[k]) {
        case 'r':
          board[rank * 16 + file] = BLACK | ROOK;
          break;
        case 'R':
          board[rank * 16 + file] = ROOK;
          break;
        case 'n':
          board[rank * 16 + file] = BLACK | KNIGHT;
          break;
        case 'N':
          board[rank * 16 + file] = KNIGHT;
          break;
        case 'b':
          board[rank * 16 + file] = BLACK | BISHOP;
          break;
        case 'B':
          board[rank * 16 + file] = BISHOP;
          break;
        case 'q':
          board[rank * 16 + file] = BLACK | QUEEN;
          break;
        case 'Q':
          board[rank * 16 + file] = QUEEN;
          break;
        case 'k':
          board[rank * 16 + file] = BLACK | KING;
          break;
        case 'K':
          board[rank * 16 + file] = KING;
          break;
        case 'p':
          board[rank * 16 + file] = BLACK | PAWN;
          break;
        case 'P':
          board[rank * 16 + file] = PAWN;
          break;
        case '/':
          rank--;
          file = -1; // so that it becomes 0
          break;
        case ' ':
          return;
        default:
          board[rank * 16 + file] = KNIGHT;
      }
      file++;
    }
  }
}

int notation_to_index(int file, int rank) {
  return (rank - 1) * 16 + (file - 'a');
}

void index_to_notation(int index, char notation[2]) {
  notation[0] = (index & FILE_MASK) + 'a';
  notation[1] = (index >> 4) + '1';
}

void print_move(int move[2]) {
  char move_in_notation[] = "xy XY";
  index_to_notation(move[0], move_in_notation);
  index_to_notation(move[1], move_in_notation + 3);
  printf("%s\n", move_in_notation);
};

void input_move(int move[2]) {
  char file;
  int rank;
  char file_destination;
  int rank_destination;

  scanf(" %c%i %c%i", &file, &rank, &file_destination, &rank_destination);

  move[0] = notation_to_index(file, rank);
  move[1] = notation_to_index(file_destination, rank_destination);
};

int generate_legal_moves(int origin, int* board) {
  int piece = board[origin];
  int* move;

  switch (piece & NO_COLOR) {
    case KNIGHT:
      move = knightMoves;
      break;
    case BISHOP:
      move = bishopMoves;
      break;
    case ROOK:
      move = rookMoves;
      break;
    case QUEEN:
      move = queenMoves;
      break;
    case KING:
      move = kingMoves;
      break;
    case PAWN:
      if (piece & BLACK) move = blackPawnMoves;
      else move = pawnMoves;
      break;
    default:
      puts("No piece found!");
      move = NULL;
  }

  while(*move) {
    for (int square = origin + *move; !(square & 0x88); square += *move) {
      if (board[square] != EMPTY) break;
      board[square] = VISUAL;
      if ((piece & NO_COLOR) == PAWN || piece == KNIGHT || piece == KING) break;
    }
    move++;
  }
}

int apply_move(int move[2], int* board) {
  int origin = move[0];
  int destination = move[1];

  if ((origin & 0x88) || (destination & 0x88)) return 0;

  int piece = board[origin];

  board[destination] = piece;
  board[origin] = EMPTY;

  return board[destination];
}

int main() {
  int board[128];
  parse_FEN(DEFAULT_FEN, board);
  print_board(board);

  int move[2];
  while (1) {
    input_move(move);
    if (apply_move(move, board)) {
      generate_legal_moves(move[1], board);
      print_board(board);
    }
    else puts("Invalid move!");
  }

	return 0;
}