-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
113 lines (99 loc) · 2.83 KB
/
utils.c
File metadata and controls
113 lines (99 loc) · 2.83 KB
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
#include <stdio.h>
#include "main.h"
#include "utils.h"
#include "moveHandler.h"
void printBoard(Board *p_board)
{
printf("__|Turn: %s|__\n", p_board->turn == WHITE ? "White" : "Black");
for (int8_t rank = 7; rank >= 0; rank--)
{
char line[18];
char *square = line;
for (int8_t file = 0; file < 8; file++)
{
uint8_t piece = p_board->board[file + rank * 8];
if (piece == EMPTY)
{
// Create black and white empty squares
if ((file + rank) % 2 == 0)
{
*square = ' ';
}
else
{
*square = ' ';
}
}
else
{
// Select the type of the non empty square
switch (piece & TYPE_MASK)
{
case PAWN:
*square = 'p';
break;
case ROOK:
*square = 'r';
break;
case KNIGHT:
*square = 'n';
break;
case BISHOP:
*square = 'b';
break;
case KING:
*square = 'k';
break;
case QUEEN:
*square = 'q';
break;
}
// Capitalize the letters of the white pieces
if ((piece & COLOR_MASK) == WHITE)
{
*square = *square + ('A' - 'a');
}
}
square++;
*square = '|';
square++;
}
*square = '\0';
printf("|%s\n", line);
}
}
void printMove(Move *p_move)
{
uint8_t f_rank = p_move->from / 8;
uint8_t f_file = p_move->from % 8;
uint8_t t_rank = p_move->to / 8;
uint8_t t_file = p_move->to % 8;
printf("%c%d -> %c%d\n", 'a' + f_file, f_rank + 1, 'a' + t_file, t_rank + 1);
}
void printMoveList(ArrayList *p_list)
{
for(uint16_t i = 0; i < p_list->elements; i++)
{
printf("(%d)\t", i);
printMove(&p_list->array[i]);
}
}
// Lists all the available moves and prompts the user to select a move
Move selectMove(Board *p_board)
{
ArrayList *p_legalMoves = getLegalMoves(p_board);
printMoveList(p_legalMoves);
unsigned int index = p_legalMoves->elements;
while (index >= p_legalMoves->elements)
{
printf("Select move: \n");
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-result"
scanf("%u", &index);
fgetc(stdin); // Empty the stdin buffer
#pragma GCC diagnostic pop
}
Move move = p_legalMoves->array[index];
freeMoveList(p_legalMoves);
return move;
}