Skip to content

Commit a71726f

Browse files
committed
✨ Implement Rook Pseudo-Legal Move Generation (Issue #2)
- Add comprehensive rook move generation with sliding piece logic - Implement horizontal and vertical movement in 4 directions - Add obstruction detection for friendly and enemy pieces - Include proper boundary validation to prevent board wrapping - Add isValidHorizontalMove() helper method for edge case handling - Follow established move object structure from pawn implementation - Maintain comprehensive input validation with descriptive errors Resolves #2: Rook moves now fully implemented with comprehensive edge case handling
1 parent 33f8fb4 commit a71726f

File tree

1 file changed

+87
-4
lines changed

1 file changed

+87
-4
lines changed

src/core/MoveGenerator.js

Lines changed: 87 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,80 @@ export class MoveGenerator {
112112
return moves;
113113
}
114114

115+
/**
116+
* Generate pseudo-legal moves for rooks.
117+
* Handles horizontal and vertical sliding movement with obstruction detection.
118+
*
119+
* @param {Piece} piece - The rook piece
120+
* @param {number} position - Current position index (0-63)
121+
* @returns {Array} Array of move objects
122+
*/
123+
generateRookMoves(piece, position) {
124+
const moves = [];
125+
const color = piece.getColor();
126+
127+
// Validate position
128+
if (position < 0 || position >= 64) {
129+
throw new Error(`Invalid position: ${position} must be between 0 and 63`);
130+
}
131+
132+
// Rook movement directions: up, down, left, right
133+
const directions = [
134+
-8, // Up (rank decrease)
135+
8, // Down (rank increase)
136+
-1, // Left (file decrease)
137+
1 // Right (file increase)
138+
];
139+
140+
// Generate moves in each direction
141+
for (const direction of directions) {
142+
let currentSquare = position + direction;
143+
144+
// Continue sliding in this direction until obstruction or board edge
145+
while (this.isValidSquare(currentSquare)) {
146+
// Check for horizontal wrapping (left/right movement)
147+
if (direction === -1 || direction === 1) {
148+
if (!this.isValidHorizontalMove(position, currentSquare)) {
149+
break; // Stop if move would wrap around board
150+
}
151+
}
152+
153+
const targetPiece = this.board.squares[currentSquare];
154+
155+
if (!targetPiece) {
156+
// Empty square - add normal move
157+
moves.push({
158+
from: position,
159+
to: currentSquare,
160+
type: 'normal',
161+
piece: piece.getType(),
162+
color: color,
163+
});
164+
} else {
165+
// Square occupied by a piece
166+
if (targetPiece.getColor() !== color) {
167+
// Enemy piece - can capture
168+
moves.push({
169+
from: position,
170+
to: currentSquare,
171+
type: 'capture',
172+
piece: piece.getType(),
173+
color: color,
174+
captured: targetPiece.getType(),
175+
});
176+
}
177+
// Stop sliding in this direction (whether friendly or enemy piece)
178+
break;
179+
}
180+
181+
// Move to next square in this direction
182+
currentSquare += direction;
183+
}
184+
}
185+
186+
return moves;
187+
}
188+
115189
/**
116190
* Check if a square index is valid (0-63)
117191
* @param {number} square - Square index to validate
@@ -135,9 +209,18 @@ export class MoveGenerator {
135209
return Math.abs(fromFile - toFile) === 1;
136210
}
137211

138-
generateRookMoves(_piece, _position) {
139-
// TODO: Implement rook move generation
140-
return [];
212+
/**
213+
* Check if a horizontal move is valid (doesn't wrap around board edges)
214+
* @param {number} fromSquare - Starting square
215+
* @param {number} toSquare - Target square
216+
* @returns {boolean} True if valid horizontal move, false otherwise
217+
*/
218+
isValidHorizontalMove(fromSquare, toSquare) {
219+
const fromRank = Math.floor(fromSquare / 8);
220+
const toRank = Math.floor(toSquare / 8);
221+
222+
// Horizontal moves must stay on the same rank
223+
return fromRank === toRank;
141224
}
142225

143226
generateKnightMoves(_piece, _position) {
@@ -159,4 +242,4 @@ export class MoveGenerator {
159242
// TODO: Implement king move generation
160243
return [];
161244
}
162-
}
245+
}

0 commit comments

Comments
 (0)