@@ -112,6 +112,80 @@ export class MoveGenerator {
112
112
return moves ;
113
113
}
114
114
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
+
115
189
/**
116
190
* Check if a square index is valid (0-63)
117
191
* @param {number } square - Square index to validate
@@ -135,9 +209,18 @@ export class MoveGenerator {
135
209
return Math . abs ( fromFile - toFile ) === 1 ;
136
210
}
137
211
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 ;
141
224
}
142
225
143
226
generateKnightMoves ( _piece , _position ) {
@@ -159,4 +242,4 @@ export class MoveGenerator {
159
242
// TODO: Implement king move generation
160
243
return [ ] ;
161
244
}
162
- }
245
+ }
0 commit comments