-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable.c
More file actions
148 lines (126 loc) · 5.25 KB
/
table.c
File metadata and controls
148 lines (126 loc) · 5.25 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
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
#include "table.h"
#include "error_type.h"
#include "piece.h"
#include "piece_type.h"
#include "coordinate.h"
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <stdio.h>
table* init_table() {
int i, j;
piece** pieces = (piece**) malloc(8 * sizeof(piece*));
for(i = 0; i < 8; i++) {
pieces[i] = (piece*)malloc(8 * sizeof(piece));
}
for(i = 0; i < 8; ++i) {
for(j = 0; j < 8; ++j) {
if(i == 1) {
// black pawn line
pieces[i][j].COLOR = COLOR_BLACK;
pieces[i][j].PIECE_TYPE = PAWN;
} else if(i == 0 || i == 7) {
pieces[i][j].COLOR = i == 0 ? COLOR_BLACK : COLOR_WHITE;
switch(j) {
case 0:
case 7:
pieces[i][j].PIECE_TYPE = ROOK;
break;
case 1:
case 6:
pieces[i][j].PIECE_TYPE = KNIGHT;
break;
case 2:
case 5:
pieces[i][j].PIECE_TYPE = BISHOP;
break;
case 3:
pieces[i][j].PIECE_TYPE = QUEEN;
break;
case 4:
pieces[i][j].PIECE_TYPE = KING;
break;
}
} else if(i == 6) {
// white pawn line
pieces[i][j].COLOR = COLOR_WHITE;
pieces[i][j].PIECE_TYPE = PAWN;
} else {
pieces[i][j].COLOR = COLOR_NONE;
pieces[i][j].PIECE_TYPE = TYPE_NONE;
}
}
}
table* table = malloc(sizeof(table));
table->LAST_COLOR = COLOR_NONE;
table->blocks = pieces;
return table;
};
bool is_empty_way(table* table, coordinate coord_way, coordinate coord_start, coordinate coord_final) {
if(!((coord_way.x == 0 || (coord_start.x - coord_final.x) % coord_way.x == 0) && (coord_way.y == 0 || (coord_start.y - coord_final.y) % coord_way.y == 0))){
exit(ERROR_INVALID_STEP);
}
coordinate coord_temp;
coord_temp.x = coord_start.x;
coord_temp.y = coord_start.y;
bool is_arrived = false;
while(!is_arrived) {
coord_temp.x += coord_way.x;
coord_temp.y += coord_way.y;
if(coord_temp.x == coord_final.x && coord_temp.y == coord_final.y) {
return true;
}
if(table->blocks[coord_temp.x][coord_temp.y].PIECE_TYPE != TYPE_NONE) {
return false;
}
}
return true;
}
bool is_valid_move(table* table, coordinate coord_start, coordinate coord_final) {
if(coord_start.x < 0 || coord_start.x > 7 || coord_start.y < 0 || coord_start.y > 7 || coord_final.x < 0 || coord_final.x > 7 || coord_final.y < 0 || coord_final.y > 7) {
exit(ERROR_OUT_OF_TABLE);
}
if(coord_start.x == coord_final.x && coord_start.y == coord_final.y){
return false;
}
coordinate coord_way;
int delta_x = coord_final.x - coord_start.x;
coord_way.x = delta_x == 0 ? 0 : 1;
int delta_y = coord_final.y - coord_start.y;
coord_way.y = delta_y == 0 ? 0 : 1;
if(!is_empty_way(table, coord_way, coord_start, coord_final)) {
return false;
}
switch(table->blocks[coord_start.x][coord_start.y].PIECE_TYPE) {
case KING:
// TODO check if wont be in hit
return((abs(coord_start.x-coord_final.x) == 0 || abs(coord_start.x-coord_final.x) == 0) && (abs(coord_start.y-coord_final.y) == 0 || abs(coord_start.y-coord_final.y) == 1));
case QUEEN:
return abs(coord_start.x-coord_final.x) == abs(coord_start.y-coord_final.y) || abs(coord_start.x-coord_final.x) == 0 || abs(coord_start.y-coord_final.y) == 0;
case ROOK:
return abs(coord_start.x-coord_final.x) == 0 || abs(coord_start.y-coord_final.y) == 0;
case BISHOP:
return abs(coord_start.x-coord_final.x) == abs(coord_start.y-coord_final.y);
break;
case KNIGHT:
return (abs(coord_start.x-coord_final.x) == 2 && abs(coord_start.y-coord_final.y) == 1) || (abs(coord_start.x-coord_final.x) == 1 && abs(coord_start.y-coord_final.y) == 2);
case PAWN:
return (abs(coord_start.x-coord_final.x) == 1 && abs(coord_start.y-coord_final.y) == 0 && table->blocks[coord_final.x][coord_final.y].PIECE_TYPE == TYPE_NONE) || (abs(coord_start.x-coord_final.x) == abs(coord_start.y-coord_final.y) == 1 && table->blocks[coord_final.x][coord_final.y].PIECE_TYPE != TYPE_NONE);
case TYPE_NONE:
exit(ERROR_INVALID_PIECE_TYPE);
break;
}
return false;
}
table* move(table* table, coordinate coord_start, coordinate coord_final) {
piece current_piece = table->blocks[coord_start.x][coord_start.y];
if(current_piece.COLOR == COLOR_NONE) {
exit(ERROR_INVALID_STEP);
}
if(is_valid_move(table, coord_start, coord_final)) {
table->blocks[coord_final.x][coord_start.y] = table->blocks[coord_start.x][coord_start.y];
table->blocks[coord_start.x][coord_start.y].COLOR = COLOR_NONE;
table->blocks[coord_start.x][coord_start.y].PIECE_TYPE = TYPE_NONE;
}
return table;
}