@@ -117,8 +117,8 @@ pub const update_board -> fn (bd: *Board, mv: *Move, white_to_move: *int, is_val
117117
118118 // Determine piece color
119119 // FIXME: Need to fix when cast the result of a bool it returns -1 and 0 instead of 1 and 0
120- let is_white_piece: int = -cast<int> (piece_char >= 'A' && piece_char <= 'Z');
121- let is_black_piece: int = -cast<int> (piece_char >= 'a' && piece_char <= 'z');
120+ let is_white_piece: int = piece::is_white_piece (piece_char);
121+ let is_black_piece: int = piece::is_black_piece (piece_char);
122122
123123 // Validate turn (fixed)
124124 if (*white_to_move == 1 && is_white_piece == 0) {
@@ -133,19 +133,10 @@ pub const update_board -> fn (bd: *Board, mv: *Move, white_to_move: *int, is_val
133133 }
134134
135135 // Check capture legality
136- if (target != '.') {
137- let target_is_white: int = -cast<int>(target >= 'A' && target <= 'Z');
138- let target_is_black: int = -cast<int>(target >= 'a' && target <= 'z');
139-
140- // Disallow capturing your own piece
141- if ((is_white_piece == 1 && target_is_white == 1) ||
142- (is_black_piece == 1 && target_is_black == 1)) {
143- output("Error: Cannot capture your own piece.\n");
144- *is_valid = 1;
145- return;
146- }
147-
148- output("Captured piece: ", target, "\n");
136+ if (piece::can_capture(target, is_white_piece, is_black_piece) == 0) {
137+ output("Error: Cannot capture your own piece.\n");
138+ *is_valid = 1;
139+ return;
149140 }
150141
151142 // Validate piece movement rules
@@ -171,12 +162,7 @@ pub const update_board -> fn (bd: *Board, mv: *Move, white_to_move: *int, is_val
171162
172163 // Check middle square for two-square move
173164 if (dc == 0 && (dr == -2 || dr == 2)) {
174- let mid_row: int;
175- if (dr == -2) {
176- mid_row = mv.from_row - 1;
177- } else {
178- mid_row = mv.from_row + 1;
179- }
165+ let mid_row: int = (mv.from_row + mv.to_row) / 2;
180166 let mid: char = bd.squares[mid_row * 8 + mv.from_col];
181167 if (mid != '.') {
182168 output("Error: Path is blocked.\n");
@@ -215,7 +201,7 @@ pub const update_board -> fn (bd: *Board, mv: *Move, white_to_move: *int, is_val
215201 *white_to_move = 1;
216202 }
217203
218- // Mark move as valid (0 = valid in your logic )
204+ // Mark move as valid (0)
219205 *is_valid = 0;
220206}
221207
@@ -228,43 +214,48 @@ pub const print_board -> fn (bd: *Board, moves: **char, move_count: int) void {
228214 }
229215 output("\n");
230216
231- loop [i: int = 0](i < 8) : (++i) {
232- output(8 - i, " ║ ");
233- loop [j: int = 0](j < 8) : (++j) {
234- let piece: *char;
235- switch (bd.squares[i * 8 + j]) {
236- 'P' -> { piece = "♟"; }
237- 'N' -> { piece = "♞"; }
238- 'B' -> { piece = "♝"; }
239- 'R' -> { piece = "♜"; }
240- 'Q' -> { piece = "♛"; }
241- 'K' -> { piece = "♚"; }
242- 'p' -> { piece = "♙"; }
243- 'n' -> { piece = "♘"; }
244- 'b' -> { piece = "♗"; }
245- 'r' -> { piece = "♖"; }
246- 'q' -> { piece = "♕"; }
247- 'k' -> { piece = "♔"; }
248- '.' -> { piece = "·"; }
217+ let total_rows: int = (move_count + 1) / 2;
218+ if (total_rows < 8) { total_rows = 8; }
219+
220+ loop [i: int = 0](i < total_rows) : (++i) {
221+ if (i < 8) {
222+ output(8 - i, " ║ ");
223+ loop [j: int = 0](j < 8) : (++j) {
224+ let piece: *char;
225+ switch (bd.squares[i * 8 + j]) {
226+ 'P' -> { piece = "♟"; }
227+ 'N' -> { piece = "♞"; }
228+ 'B' -> { piece = "♝"; }
229+ 'R' -> { piece = "♜"; }
230+ 'Q' -> { piece = "♛"; }
231+ 'K' -> { piece = "♚"; }
232+ 'p' -> { piece = "♙"; }
233+ 'n' -> { piece = "♘"; }
234+ 'b' -> { piece = "♗"; }
235+ 'r' -> { piece = "♖"; }
236+ 'q' -> { piece = "♕"; }
237+ 'k' -> { piece = "♔"; }
238+ '.' -> { piece = "·"; }
239+ }
240+ output(piece, " ");
249241 }
250- output(piece, " ");
242+ output("║");
243+ } else {
244+ output(" ");
251245 }
252- output("║");
253-
254- // Display moves on the right (2 per row: white and black)
246+
255247 let move_pair_index: int = i * 2;
256248 if (move_pair_index < move_count) {
257249 let move_num: int = move_pair_index / 2 + 1;
258250 output(" ", move_num, ". ", moves[move_pair_index]);
259-
260- // Show black's response if it exists
261251 if (move_pair_index + 1 < move_count) {
262252 output(" ", moves[move_pair_index + 1]);
263253 }
264254 }
265-
266255 output("\n");
267256 }
257+
268258 output(" ╚═════════════════╝\n");
269259 output(" a b c d e f g h\n\n");
270260}
261+
0 commit comments