-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpieces.fs
38 lines (36 loc) · 1.52 KB
/
pieces.fs
1
2
3
4
5
6
7
8
9
10
11
12
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
module Pieces
open Chess
/// <summary> A king is a chessPiece which moves 1 square in any direction. </summary>
/// <param name = "col"> The color black or white </param>
/// <returns> A king object. </returns>
type king(col : Color) =
inherit chessPiece(col)
override this.nameOfType = "king"
// king has runs of 1 in 8 directions: (N, NE, E, SE, S, SW, W, NW)
override this.candiateRelativeMoves =
[[(-1,0)];[(-1,1)];[(0,1)];[(1,1)];
[(1,0)];[(1,-1)];[(0,-1)];[(-1,-1)]]
/// <summary> A rook is a chessPiece which moves horisontally and vertically. </summary>
/// <param name = "col"> The color black or white </param>
/// <returns> A rook object. </returns>
type rook(col : Color) =
inherit chessPiece(col)
// rook can move horisontally and vertically
// Make a list of relative coordinate lists. We consider the
// current position and try all combinations of relative moves
// (1,0); (2,0) ... (7,0); (-1,0); (-2,0); ...; (0,-7).
// Some will be out of board, but will be assumed removed as
// illegal moves.
// A list of functions for relative moves
let indToRel = [
fun elm -> (elm,0); // South by elm
fun elm -> (-elm,0); // North by elm
fun elm -> (0,elm); // West by elm
fun elm -> (0,-elm) // East by elm
]
// For each function in indToRel, we calculate List.map f [1..7].
// swap converts (List.map fct indices) to (List.map indices fct).
let swap f a b = f b a
override this.candiateRelativeMoves =
List.map (swap List.map [1..7]) indToRel
override this.nameOfType = "rook"