move-validation.ts 13.6 KB
Newer Older
Gerson Sunyé's avatar
Gerson Sunyé committed
1
2
3
4
import { Chessboard, isEmpty, Square, squareAtPosition } from "./chessboard";
import { Move } from "./movements";
import { equals, left, right, top, bottom } from "./position";

5
6
7
8
9
10
11
12
// Fonction de validation des moves

function isValidEat(board : Chessboard, move : Move): boolean {
    let start : Square = squareAtPosition(board, move.from!);
    let destination : Square = squareAtPosition(board, move.to!);
    return (start.piece!.isWhite !== destination.piece!.isWhite || !start.piece!.isWhite !== !destination.piece!.isWhite)
}

Gerson Sunyé's avatar
Gerson Sunyé committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
 * Checks whether a Black Pawn can perform a given move.
 * A pawn can move forward to the unoccupied square immediately in front of 
 * it on the same file, or on its first move it can advance two squares along 
 * the same file, provided both squares are unoccupied (black dots in the 
 * diagram); or the pawn can capture an opponent's piece on a square diagonally 
 * in front of it on an adjacent file, by moving to that square (black "x"s). 
 * 
 * 
 * @param board The chessboard of the current game
 * @param move 
 */
export function blackPawnMove(board: Chessboard, move: Move): boolean {

    if (equals(move.to!, bottom(move.from!))) {
        //console.log("Single forward");
        return isEmpty(board, move.to!);
    }

    if (move.from!.rank === 6 && equals(move.to!, bottom(bottom(move.from!)))) {
        //console.log("Double forward");
        return isEmpty(board, bottom(move.from!)) && isEmpty(board, move.to!);
    }

    if (equals(move.to!, left(bottom(move.from!))) || equals(move.to!, right(bottom(move.from!)))) {
        let destination: Square = squareAtPosition(board, move.to!);
        return !(destination.isEmpty || !destination.piece!.isWhite)
    }

    return false;
}

/**
 * A pawn can move forward to the unoccupied square immediately in front of 
 * it on the same file, or on its first move it can advance two squares along 
 * the same file, provided both squares are unoccupied (black dots in 
 * the diagram); or the pawn can capture an opponent's piece on a square diagonally 
 * in front of it on an adjacent file, by moving to that square (black "x"s). 
 * 
 * 
 * @param board The chessboard of the current game
 * @param move 
 */
export function whitePawnMove(board: Chessboard, move: Move): boolean {

    if (equals(move.to!, top(move.from!))) {
        return isEmpty(board, move.to!);
    }

    if (move.from!.rank === 1 && equals(move.to!, top(top(move.from!)))) {
        return isEmpty(board, top(move.from!)) && isEmpty(board, move.to!);
    }

    if (equals(move.to!, left(top(move.from!))) || equals(move.to!, right(top(move.from!)))) {
        let destination: Square = squareAtPosition(board, move.to!);
        return !(destination.isEmpty || destination.piece!.isWhite)
    }

    return false;
}

/**
 * Checks whether a King can perform a given move.
 * The king moves one square in any direction. 
 * 
 * @param board The chessboard of the current game
 * @param move 
 */
export function kingMove(board: Chessboard, move: Move): boolean {
82
83
84
85
86
87
88
89
90
91
92

    // Mouvement normal & mangeage de pièce

    if (equals(move.to!, bottom(move.from!)) || equals(move.to!, right(move.from!)) || equals(move.to!, top(move.from!)) || equals(move.to!, left(move.from!))
    || equals(move.to!, left(top(move.from!))) || equals(move.to!, right(top(move.from!))) || equals(move.to!, left(bottom(move.from!))) || equals(move.to!, right(bottom(move.from!)))) {
        
        let destination: Square = squareAtPosition(board, move.to!);
        return (destination.isEmpty || isValidEat(board,move))
    }

    return false;
Gerson Sunyé's avatar
Gerson Sunyé committed
93
94
95
96
97
98
99
100
101
102
103
}

/**
 * Checks whether a Queen can perform a given move.
 * The queen combines the power of a rook and bishop and can move any 
 * number of squares along a rank, file, or diagonal, but cannot leap over other pieces.
 * 
 * @param board The chessboard of the current game
 * @param move 
 */
export function queenMove(board: Chessboard, move: Move): boolean {
104
105
106


    return false;
Gerson Sunyé's avatar
Gerson Sunyé committed
107
108
109
}

/**
110
 * Checks whether a Empress can perform a given move.
111
112
113
114
115
116
 * An Empress can move any number of squares along a rank or file, 
 * but cannot leap over other pieces.
 * An Empress can also move to any of the closest squares that are not on the 
 * same rank, file, or diagonal. (Thus the move forms an "L"-shape: 
 * two squares vertically and one square horizontally, or two 
 * squares horizontally and one square vertically.) 
Gerson Sunyé's avatar
Gerson Sunyé committed
117
118
119
120
 * 
 * @param board The chessboard of the current game
 * @param move 
 */
121
export function empressMove(board: Chessboard, move: Move): boolean {
122
/* Comme la Tour mais avec en plus les mouvements du chevalier */
Stewan Lheureux's avatar
Stewan Lheureux committed
123

124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Mouvements du Chevalier

    // Mouvement vers le bas
    if (equals(move.to!, left(bottom(bottom(move.from!)))) || equals(move.to!, right(bottom(bottom(move.from!))))) {
        let destination: Square = squareAtPosition(board, move.to!);
        return (destination.isEmpty || isValidEat(board,move))
    }
    // Mouvement vers le haut
    if (equals(move.to!, left(top(top(move.from!)))) || equals(move.to!, right(top(top(move.from!))))) {
        let destination: Square = squareAtPosition(board, move.to!);
        return (destination.isEmpty || isValidEat(board,move))
    }
    // Mouvement vers la gauche
    if (equals(move.to!, bottom(left(left(move.from!)))) || equals(move.to!, top(left(left(move.from!))))) {
        let destination: Square = squareAtPosition(board, move.to!);
        return (destination.isEmpty || isValidEat(board,move))
    }
    // Mouvement vers la droite
    if (equals(move.to!, bottom(right(right(move.from!)))) || equals(move.to!, top(right(right(move.from!))))) {
        let destination: Square = squareAtPosition(board, move.to!);
        return (destination.isEmpty || isValidEat(board,move))
    }
Stewan Lheureux's avatar
Stewan Lheureux committed
146

147
148
149
150
// Mouvements de la Tour haut et bas

 if (move.from!.file == move.to!.file) { // Le déplacement se fait-il sur la même colonne ?

Stewan Lheureux's avatar
Stewan Lheureux committed
151
    let moveRange = Math.abs(move.from!.rank-move.to!.rank!); // combien de cases on veut traverser en haut ou en bas ?
152
    let moveFromTop = move.from!;
Stewan Lheureux's avatar
Stewan Lheureux committed
153
    let moveFromBottom = move.from!;
154
155
156
157

    for (let i=1; i<=moveRange; i++) {

        // Si on va en haut
Stewan Lheureux's avatar
Stewan Lheureux committed
158
       if (move.from!.rank!-move.to!.rank! < 0) {
159

Stewan Lheureux's avatar
Stewan Lheureux committed
160
            if (equals(move.to!, top(moveFromTop))) {
161
162
163
164
165
166
167
168
169
170
                let destination: Square = squareAtPosition(board, move.to!);
                return (destination.isEmpty || isValidEat(board,move));
            }
            
            else {
                let destination: Square = squareAtPosition(board, top(moveFromTop));
                if (!destination.isEmpty) {
                    return false;
                }
            }
Stewan Lheureux's avatar
Stewan Lheureux committed
171
172
173
174
        }

        // Si on va en bas
       if (move.from!.rank!-move.to!.rank! > 0) {
175

Stewan Lheureux's avatar
Stewan Lheureux committed
176
177
178
179
        if (equals(move.to!, bottom(moveFromBottom))) {
            let destination: Square = squareAtPosition(board, move.to!);
            return (destination.isEmpty || isValidEat(board,move));
        }
180
        
Stewan Lheureux's avatar
Stewan Lheureux committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
        else {
            let destination: Square = squareAtPosition(board, bottom(moveFromBottom));
            if (!destination.isEmpty) {
                return false;
            }
        }
    }
    moveFromTop = top(moveFromTop); 
    moveFromBottom = bottom(moveFromBottom); 
    }
}

 if (move.from!.rank == move.to!.rank) { // Le déplacement se fait-il sur la même ligne ?

    let moveRange = Math.abs(move.from!.file-move.to!.file!); // combien de cases on veut traverser à gauche ou à droite ?
    let moveFromRight = move.from!;
    let moveFromLeft = move.from!;

    for (let i=1; i<=moveRange; i++) {


        // Si on va à droite
        if (move.from!.file!-move.to!.file! < 0) {

            if (equals(move.to!, right(moveFromRight))) {
                let destination: Square = squareAtPosition(board, move.to!);
                return (destination.isEmpty || isValidEat(board,move));
            }
            
            else {
                let destination: Square = squareAtPosition(board, right(moveFromRight));
                if (!destination.isEmpty) {
                    return false;
                }
            }
        }

        // Si on va à gauche
        if (move.from!.file!-move.to!.file! > 0) {

            if (equals(move.to!, left(moveFromLeft))) {
                let destination: Square = squareAtPosition(board, move.to!);
                return (destination.isEmpty || isValidEat(board,move));
            }
            
            else {
                let destination: Square = squareAtPosition(board, left(moveFromLeft));
                if (!destination.isEmpty) {
                    return false;
                }
            }
        }

        moveFromRight = right(moveFromRight); 
        moveFromLeft = left(moveFromLeft);         
236
237
238
    }
 }
    return false;
Stewan Lheureux's avatar
Stewan Lheureux committed
239

Gerson Sunyé's avatar
Gerson Sunyé committed
240
241
}

242

Gerson Sunyé's avatar
Gerson Sunyé committed
243
/**
244
245
 * Checks whether a Princess can perform a given move.
 * A princess can move any number of squares diagonally, 
Gerson Sunyé's avatar
Gerson Sunyé committed
246
 * but cannot leap over other pieces.
247
248
249
250
 * A princess can also move to any of the closest squares that are not on the 
 * same rank, file, or diagonal. (Thus the move forms an "L"-shape: 
 * two squares vertically and one square horizontally, or two 
 * squares horizontally and one square vertically.) 
Gerson Sunyé's avatar
Gerson Sunyé committed
251
252
253
254
 * 
 * @param board The chessboard of the current game
 * @param move 
 */
255
export function princessMove(board: Chessboard, move: Move): boolean {
Stewan Lheureux's avatar
Stewan Lheureux committed
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346

// Mouvements du Fou

if (Math.abs(move.to!.rank - move.from!.rank) == Math.abs(move.to!.file - move.from!.file)) {

    let moveRange = Math.abs(move.from!.rank-move.to!.rank);
    let moveFrom11pm = move.from!;
    let moveFrom8pm = move.from!;
    let moveFrom5pm = move.from!;
    let moveFrom2pm = move.from!;


    for (let i=1; i<=moveRange; i++) {

        // Si on va en haut à gauche (11pm)

        if (move.to!.file > move.from!.file && move.to!.rank < move.from!.rank) {

            if (equals(move.to!, left(top(moveFrom11pm)))) {
                let destination: Square = squareAtPosition(board, move.to!);
                return (destination.isEmpty || isValidEat(board,move));
            }
            
            else {
                let destination: Square = squareAtPosition(board, left(top(moveFrom11pm)));
                if (!destination.isEmpty) {
                    return false;
                }
            }
        }

        // Si on va en haut à droite (2pm)

        if (move.to!.file > move.from!.file && move.to!.rank > move.from!.rank) {

            if (equals(move.to!, right(top(moveFrom2pm)))) {
                let destination: Square = squareAtPosition(board, move.to!);
                return (destination.isEmpty || isValidEat(board,move));
            }
        
            else {
                let destination: Square = squareAtPosition(board, right(top(moveFrom2pm)));
                if (!destination.isEmpty) {
                    return false;
                }
            }
        }


        // Si on va en bas à gauche (8pm)

        if (move.to!.file < move.from!.file && move.to!.rank < move.from!.rank) {

            if (equals(move.to!, left(bottom(moveFrom8pm)))) {
                let destination: Square = squareAtPosition(board, move.to!);
                return (destination.isEmpty || isValidEat(board,move));
            }
            
            else {
                let destination: Square = squareAtPosition(board, left(bottom(moveFrom8pm)));
                if (!destination.isEmpty) {
                    return false;
                }
            }
        }

        // Si on va en bas à droite (5pm)

        if (move.to!.file < move.from!.file && move.to!.rank > move.from!.rank) {

        if (equals(move.to!, right(bottom(moveFrom5pm)))) {
            let destination: Square = squareAtPosition(board, move.to!);
            return (destination.isEmpty || isValidEat(board,move));
        }
        
        else {
            let destination: Square = squareAtPosition(board, right(bottom(moveFrom5pm)));
            if (!destination.isEmpty) {
                return false;
            }
        }
    }

    moveFrom11pm = left(top(moveFrom11pm)); 
    moveFrom8pm = left(bottom(moveFrom8pm));
    moveFrom5pm = right(bottom(moveFrom5pm));
    moveFrom2pm = right(top(moveFrom2pm));

    }
}
    return false;
Gerson Sunyé's avatar
Gerson Sunyé committed
347
348
349
}

/**
350
351
352
 * Checks whether a Camel can perform a given move.
 * The Camel move forms an "L"-shape: 
 * three squares vertically and one square horizontally, or three 
Gerson Sunyé's avatar
Gerson Sunyé committed
353
354
 * squares horizontally and one square vertically.) 
 * 
sunye's avatar
sunye committed
355
 * The camel can leap over other pieces.
Gerson Sunyé's avatar
Gerson Sunyé committed
356
357
358
359
 * 
 * @param board The chessboard of the current game
 * @param move 
 */
360
export function camelMove(board: Chessboard, move: Move): boolean {
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/* Comme Chevalier mais avec une case de plus */

    // Mouvement vers le bas

    if (equals(move.to!, left(bottom(bottom(bottom(move.from!))))) || equals(move.to!, right(bottom(bottom(bottom(move.from!)))))) {
        let destination: Square = squareAtPosition(board, move.to!);
        return (destination.isEmpty || isValidEat(board,move))
    }

    // Mouvement vers le haut

    if (equals(move.to!, left(top(top(top(move.from!))))) || equals(move.to!, right(top(top(top(move.from!)))))) {
        let destination: Square = squareAtPosition(board, move.to!);
        return (destination.isEmpty || isValidEat(board,move))
    }

    // Mouvement vers la gauche

    if (equals(move.to!, bottom(left(left(left(move.from!))))) || equals(move.to!, top(left(left(left(move.from!)))))) {
        let destination: Square = squareAtPosition(board, move.to!);
        return (destination.isEmpty || isValidEat(board,move))
    }

    // Mouvement vers la droite

    if (equals(move.to!, bottom(right(right(right(move.from!))))) || equals(move.to!, top(right(right(right(move.from!)))))) {
        let destination: Square = squareAtPosition(board, move.to!);
        return (destination.isEmpty || isValidEat(board,move))
    }

    return false;
}