Skip to content

Commit e6fb22a

Browse files
author
TheDevConnor
committed
Cleaned up the chess code a bit
1 parent a321c56 commit e6fb22a

3 files changed

Lines changed: 57 additions & 48 deletions

File tree

tests/chess_engine/board.lx

Lines changed: 38 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -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+

tests/chess_engine/main.lx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
@use "memory" as mem
77
@use "board" as board
88

9-
// TODO: add in castle and en passant logic
9+
// TODO: add in castle, en passant logic, check logic
1010

1111
pub const main -> fn () int {
1212
let bd: Board;

tests/chess_engine/piece.lx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ const RULES: [PieceRules; 6] = [
3737
}
3838
];
3939

40+
pub const is_white_piece -> fn (ch: char) int { return -cast<int>(ch >= 'A' && ch <= 'Z'); }
41+
pub const is_black_piece -> fn (ch: char) int { return -cast<int>(ch >= 'a' && ch <= 'z'); }
42+
4043
// Get rules for a specific piece
4144
pub const get_rules -> fn (piece: char) *PieceRules {
4245
let piece_upper: char = piece;
@@ -53,6 +56,21 @@ pub const get_rules -> fn (piece: char) *PieceRules {
5356
return cast<*PieceRules>(0);
5457
}
5558

59+
pub const can_capture -> fn (target: char, white_piece: int, black_piece: int) int {
60+
if (target != '.') {
61+
let target_is_white: int = is_white_piece(target);
62+
let target_is_black: int = is_black_piece(target);
63+
64+
// Disallow capturing your own piece
65+
if ((white_piece == 1 && target_is_white == 1) ||
66+
(black_piece == 1 && target_is_black == 1)) {
67+
return 0;
68+
}
69+
}
70+
71+
return 1;
72+
}
73+
5674
// Special pawn validation function (pawns have unique rules)
5775
pub const validate_pawn_move -> fn (rule: *PieceRules, dc: int, dr: int,
5876
from_row: int, target: char, is_white: int) int {

0 commit comments

Comments
 (0)