|
1 | 1 | @module "board" |
2 | 2 |
|
3 | 3 | @use "string" as string |
| 4 | +@use "helper" as helper |
4 | 5 | @use "piece" as piece |
5 | 6 |
|
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 | | - |
19 | 7 | #returns_ownership |
20 | 8 | pub const init_board -> fn (bd: *Board) *Board { |
21 | 9 | bd.squares = cast<*char>(alloc(8 * sizeof<*char>)); |
@@ -58,41 +46,21 @@ pub const parse_move -> fn (mv: *Move, move: *char) *Move { |
58 | 46 | return mv; |
59 | 47 | } |
60 | 48 |
|
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) |
90 | 51 |
|
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; |
96 | 64 | } |
97 | 65 |
|
98 | 66 | 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 |
152 | 120 |
|
153 | 121 | // Special handling for pawns |
154 | 122 | 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) { |
158 | 125 | output("Error: Illegal pawn move.\n"); |
159 | 126 | *is_valid = 1; |
160 | 127 | return; |
161 | 128 | } |
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 | | - } |
173 | 129 | } 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) { |
177 | 131 | output("Error: Illegal move for this piece.\n"); |
178 | 132 | *is_valid = 1; |
179 | 133 | return; |
180 | 134 | } |
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 | | - } |
191 | 135 | } |
192 | 136 |
|
193 | 137 | // Move the piece |
|
0 commit comments