Skip to content

Commit a8c0961

Browse files
author
TheDevConnor
committed
Update make files and the chess engine
1 parent e6fb22a commit a8c0961

8 files changed

Lines changed: 255 additions & 236 deletions

File tree

tests/chess_engine/board.lx

Lines changed: 18 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
11
@module "board"
22

33
@use "string" as string
4+
@use "helper" as helper
45
@use "piece" as piece
56

6-
pub const Move -> struct {
7-
from_col: int,
8-
from_row: int,
9-
to_col: int,
10-
to_row: int
11-
};
12-
13-
pub const Board -> struct {
14-
starting_fen: *char,
15-
length_fen: int,
16-
squares: *char,
17-
};
18-
197
#returns_ownership
208
pub const init_board -> fn (bd: *Board) *Board {
219
bd.squares = cast<*char>(alloc(8 * sizeof<*char>));
@@ -58,41 +46,21 @@ pub const parse_move -> fn (mv: *Move, move: *char) *Move {
5846
return mv;
5947
}
6048

61-
pub const is_path_clear -> fn (bd: *Board, mv: *Move) int {
62-
let dc: int = mv.to_col - mv.from_col;
63-
let dr: int = mv.to_row - mv.from_row;
64-
65-
let step_c: int;
66-
if (dc == 0) {
67-
step_c = 0;
68-
} elif (dc > 0) {
69-
step_c = 1;
70-
} else {
71-
step_c = -1;
72-
}
73-
74-
let step_r: int;
75-
if (dr == 0) {
76-
step_r = 0;
77-
} elif (dr > 0) {
78-
step_r = 1;
79-
} else {
80-
step_r = -1;
81-
}
82-
83-
let curr_col: int = mv.from_col + step_c;
84-
let curr_row: int = mv.from_row + step_r;
85-
86-
loop (curr_col != mv.to_col || curr_row != mv.to_row) {
87-
if (bd.squares[curr_row * 8 + curr_col] != '.') {
88-
return 0;
89-
}
49+
const is_check -> fn (bd: *Board, king_color: int, king_row: int, king_col: int) int {
50+
// 1. Locate the King (already provided by king_row, king_col)
9051

91-
curr_col = curr_col + step_c;
92-
curr_row = curr_row + step_r;
93-
}
94-
95-
return 1;
52+
// 2. Iterate Through Opponent's Pieces
53+
// (Loop through all squares, identify opponent's pieces)
54+
55+
// 3. Check for Attacks
56+
// (Inside the loop, for each opponent's piece,
57+
// call a helper function like 'can_piece_attack_square'
58+
// to see if it attacks king_row, king_col)
59+
60+
// 4. Return Check Status
61+
// (If any piece attacks, return true; otherwise, return false after the loop)
62+
63+
return 0;
9664
}
9765

9866
pub const update_board -> fn (bd: *Board, mv: *Move, white_to_move: *int, is_valid: *int) void {
@@ -152,42 +120,18 @@ pub const update_board -> fn (bd: *Board, mv: *Move, white_to_move: *int, is_val
152120

153121
// Special handling for pawns
154122
if (piece_char == 'P' || piece_char == 'p') {
155-
let is_valid_pawn: int = piece::validate_pawn_move(rule, dc, dr, mv.from_row,
156-
target, is_white_piece);
157-
if (is_valid_pawn == 0) {
123+
if (piece::validate_pawn_move(rule, dc, dr, mv.from_row, target, is_white_piece) == 0 ||
124+
piece::validate_pawn_path(bd, mv, dr, dc) == 0) {
158125
output("Error: Illegal pawn move.\n");
159126
*is_valid = 1;
160127
return;
161128
}
162-
163-
// Check middle square for two-square move
164-
if (dc == 0 && (dr == -2 || dr == 2)) {
165-
let mid_row: int = (mv.from_row + mv.to_row) / 2;
166-
let mid: char = bd.squares[mid_row * 8 + mv.from_col];
167-
if (mid != '.') {
168-
output("Error: Path is blocked.\n");
169-
*is_valid = 1;
170-
return;
171-
}
172-
}
173129
} else {
174-
// Other pieces use standard move validation
175-
let can_move: int = piece::can_move_direction(rule, dc, dr);
176-
if (can_move == 0) {
130+
if (piece::is_move_legal(rule, bd, mv) == 0) {
177131
output("Error: Illegal move for this piece.\n");
178132
*is_valid = 1;
179133
return;
180134
}
181-
182-
// Check path is clear for sliding pieces
183-
if (rule.can_slide == 1 && rule.can_jump == 0) {
184-
let path_clear: int = is_path_clear(bd, mv);
185-
if (path_clear == 0) {
186-
output("Error: Path is blocked.\n");
187-
*is_valid = 1;
188-
return;
189-
}
190-
}
191135
}
192136

193137
// Move the piece

tests/chess_engine/helper.lx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
@module "helper"
2+
3+
pub const Move -> struct {
4+
from_col: int,
5+
from_row: int,
6+
to_col: int,
7+
to_row: int
8+
};
9+
10+
pub const Board -> struct {
11+
starting_fen: *char,
12+
length_fen: int,
13+
squares: *char,
14+
};

tests/chess_engine/main.lx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
@use "terminal" as term
44
@use "string" as string
5+
@use "helper" as helper
56
@use "termfx" as termfx
67
@use "memory" as mem
78
@use "board" as board

tests/chess_engine/piece.lx

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
@module "piece"
22

3+
@use "helper" as helper
4+
35
pub const PieceRules -> struct {
46
piece_type: char,
57
can_jump: int,
@@ -111,6 +113,52 @@ pub const validate_pawn_move -> fn (rule: *PieceRules, dc: int, dr: int,
111113
return 0;
112114
}
113115

116+
pub const validate_pawn_path -> fn (bd: *Board, mv: *Move, dr: int, dc: int) int {
117+
if (dc == 0 && (dr == -2 || dr == 2)) {
118+
let mid_row: int = (mv.from_row + mv.to_row) / 2;
119+
let mid: char = bd.squares[mid_row * 8 + mv.from_col];
120+
if (mid != '.') { return 0; }
121+
}
122+
return 1;
123+
}
124+
125+
const is_path_clear -> fn (bd: *Board, mv: *Move) int {
126+
let dc: int = mv.to_col - mv.from_col;
127+
let dr: int = mv.to_row - mv.from_row;
128+
129+
let step_c: int;
130+
if (dc == 0) {
131+
step_c = 0;
132+
} elif (dc > 0) {
133+
step_c = 1;
134+
} else {
135+
step_c = -1;
136+
}
137+
138+
let step_r: int;
139+
if (dr == 0) {
140+
step_r = 0;
141+
} elif (dr > 0) {
142+
step_r = 1;
143+
} else {
144+
step_r = -1;
145+
}
146+
147+
let curr_col: int = mv.from_col + step_c;
148+
let curr_row: int = mv.from_row + step_r;
149+
150+
loop (curr_col != mv.to_col || curr_row != mv.to_row) {
151+
if (bd.squares[curr_row * 8 + curr_col] != '.') {
152+
return 0;
153+
}
154+
155+
curr_col = curr_col + step_c;
156+
curr_row = curr_row + step_r;
157+
}
158+
159+
return 1;
160+
}
161+
114162
pub const can_move_direction -> fn (rule: *PieceRules, dc: int, dr: int) int {
115163
if (rule.can_slide == 1) {
116164
let step_c: int;
@@ -147,3 +195,14 @@ pub const can_move_direction -> fn (rule: *PieceRules, dc: int, dr: int) int {
147195
}
148196
return 0;
149197
}
198+
199+
pub const is_move_legal -> fn (rule: *PieceRules, bd: *Board, mv: *Move) int {
200+
let dc: int = mv.to_col - mv.from_col;
201+
let dr: int = mv.to_row - mv.from_row;
202+
if (can_move_direction(rule, dc, dr) == 0) { return 0; }
203+
if (rule.can_slide == 1 && rule.can_jump == 0) {
204+
if (is_path_clear(bd, mv) == 0) { return 0; }
205+
}
206+
return 1;
207+
}
208+
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,13 @@ pub const main -> fn () int {
118118
horizontal_offset = -2 * cube_width;
119119
print_cube();
120120

121-
cube_width = 10.0;
122-
horizontal_offset = 1 * cube_width;
123-
print_cube();
121+
//cube_width = 10.0;
122+
//horizontal_offset = 1 * cube_width;
123+
//print_cube();
124124

125-
cube_width = 5.0;
126-
horizontal_offset = 8 * cube_width;
127-
print_cube();
125+
//cube_width = 5.0;
126+
//horizontal_offset = 8 * cube_width;
127+
//print_cube();
128128

129129
output(fx::CURSOR_HOME);
130130
loop [k: int = 0](k < width * height) : (++k) {

tests/rotating_cube/build_cube.mk

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Makefile for Luma Chess Engine
2+
# ./luma tests/3d_spinning_cube.lx -name 3d -l std/math.lx std/termfx.lx std/string.lx std/memory.lx
3+
LUMA = ./../../luma
4+
NAME = spinning_cube
5+
6+
MAIN = 3d_spinning_cube.lx
7+
SPINNING_SRCS = $(filter-out $(MAIN), $(wildcard $/*.lx))
8+
9+
STD_LIBS = ../../std/termfx.lx \
10+
../../std/string.lx \
11+
../../std/math.lx \
12+
../../std/memory.lx
13+
14+
ALL_SRCS = $(MAIN) $(CHESS_SRCS) $(STD_LIBS)
15+
16+
.PHONY: all
17+
all: $(NAME)
18+
19+
# Build chess engine
20+
$(NAME): $(ALL_SRCS)
21+
@echo "Building spinning cube..."
22+
$(LUMA) $(MAIN) -name $(NAME) -l $(SPINNING_SRCS) $(STD_LIBS)
23+
@echo "Build complete: ./$(NAME)"
24+
25+
.PHONY: run
26+
run: $(NAME)
27+
@echo "Starting spinning_cube..."
28+
./$(NAME)
29+
30+
.PHONY: valgrind
31+
valgrind: $(NAME)
32+
@echo "Running spinning cube with valgrind..."
33+
valgrind --leak-check=full --show-leak-kinds=all ./$(NAME)
34+
35+
.PHONY: clean
36+
clean:
37+
@echo "Cleaning build artifacts..."
38+
rm -f $(NAME)
39+
rm -f obj/*.o
40+
41+
.PHONY: rebuild
42+
rebuild: clean all
43+
44+
# Show what files will be compiled
45+
.PHONY: list
46+
list:
47+
@echo "Main file:"
48+
@echo " $(MAIN)"
49+
@echo ""
50+
@echo "Tetris sources:"
51+
@for src in $(SPINNING_SRCS); do echo " $$src"; done
52+
@echo ""
53+
@echo "Standard library:"
54+
@for lib in $(STD_LIBS); do echo " $$lib"; done
55+
56+
# Help target
57+
.PHONY: help
58+
help:
59+
@echo "Luma 3d Spinning Cube Build System"
60+
@echo ""
61+
@echo "Build Targets:"
62+
@echo " all - Build Spinning Cube (default)"
63+
@echo " rebuild - Clean and rebuild"
64+
@echo ""
65+
@echo "Run Targets:"
66+
@echo " run - Build and run"
67+
@echo " valgrind - Run with memory leak detection"
68+
@echo ""
69+
@echo "Development:"
70+
@echo " list - Show all source files"
71+
@echo " clean - Remove build artifacts"
72+
@echo ""
73+
@echo "Examples:"
74+
@echo " make # Build"
75+
@echo " make run # Build and play"
76+
@echo " make list # Show what will be compiled"
77+
78+

0 commit comments

Comments
 (0)