-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathooboardgame.ml
1700 lines (1258 loc) · 39 KB
/
ooboardgame.ml
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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(*
* Author : Giorgio Marinelli
* Year : 2011
*
* Description : A simple chess engine
*
*)
(* some exceptions and data types *)
exception None_is_none
exception Square_is_empty
exception Piece_not_on_board
exception Bad_arguments
exception Unknown_rules
exception Unknown_piece
exception Unknown_color
exception Unknown_command
exception Wrong_color
exception New_game
exception Continue
exception Quit
type equality = Same | Different
type validity = Valid | Invalid
type reachability = Reachable | Not_reachable
exception Boundedness
type boundedness = Within_bounds | Out_of_bounds
exception Emptiness
type emptiness = Empty | Not_empty
exception Wrong_move
type typeOfMove = Improper_move | Simple_move | Capture_move | Special_move | In_passing
type lengthOfmove = Steps of int | Adjacent_square | Same_square
(* two simple functions to work with 'a option type *)
let (<--) x = match x with (Some y) -> y | None -> raise None_is_none
and (-->) x = Some x
(* addition and subtraction of integer pairs *)
let (++) (x0, y0) (x1, y1) = (x0 + x1, y0 + y1)
and (--) (x0, y0) (x1, y1) = (x0 - x1, y0 - y1)
(* remove blanks from a string *)
let removeBlanks x =
let x = Str.global_replace (Str.regexp "^[ \t]+\\|[ \t]+$") "" x in
let x = Str.global_replace (Str.regexp "[ \t][ \t]+") " " x in
let x = String.lowercase_ascii x in x
(* return a string of a pair *)
let string_of_position (i, j) f g = (f i) ^ (g j)
class type
['a] piece =
object
method getColor : string
method getType : string
method getMarker : string
method getPosition : int * int
method isOnBoard : unit -> bool
method setupOnBoard : 'a board -> int * int -> unit
method removeFromBoard : unit -> unit
method checkMove : int * int -> typeOfMove
method pieceCouldBeCaptured : int * int -> bool
method getMoved : unit -> bool
method setMoved : unit -> unit
method unsetMoved : unit -> unit
end
and
['a] board =
object
method makePiece : int * int -> string -> string -> unit
method getPiece : int * int -> 'a piece
method grabPiece : int * int -> 'a piece
method putPiece : int * int -> 'a piece -> unit
method emptySquareInPosition : int * int -> unit
method squareIsEmpty : int * int -> emptiness
method isItValidPosition : int * int -> boundedness
method checkColorInPosition : int * int -> string -> equality
method setBoardStatus : emptiness -> unit
method getBoardStatus : unit -> emptiness
method setPiecesList : 'a piece list -> unit
method getPiecesList : unit -> 'a piece list
method getHistory : unit -> 'a history
method setHistory : 'a history -> unit
method print : unit -> unit
end
and
['a] rules =
object
method getBoardSize : unit -> int * int
method getNumberOfPlayers : unit -> int
method getFirstPlayer : unit -> string
method getBoard : unit -> 'a board
method setupBoard : 'a board -> 'a history -> unit
method getMove : 'a board -> string -> string -> 'a move
method translatePositions : string -> 'a piece list -> string -> (int * int) list
method translatePromotion : string -> string
end
and
['a] move =
object
method doIt : unit -> unit
method undoIt : unit -> string
method getTypeOfMove : unit -> typeOfMove
method isCaptureMove : unit -> bool
method getCapturedPiece : unit -> 'a piece
method checkThisMove : string -> string -> int * int -> int * int -> bool
method print : unit -> unit
end
and
['a] captured =
object
method addPiece : 'a piece -> unit
method delLastOne : unit -> unit
method howManyPieces : unit -> int
end
and
['a] history =
object
method getLastMove : unit -> 'a move
method addMove : 'a move -> unit
method undoMove : int -> string
method howManyMoves : unit -> int
method howManyCapturedPieces : unit -> int
method print : int -> unit
end
and
['a] game =
object
method init : unit -> unit
method play : unit -> unit
end
class virtual ['a] commonGame =
object
method virtual private restart : unit -> unit
method virtual private print : unit -> unit
method virtual private history : int -> unit
method virtual private undo : int -> unit
method virtual private move : string -> unit
end
class virtual ['a] commonPiece pieceColor pieceType pieceMarker =
object (self : 'a #piece)
method getColor = pieceColor
method getType = pieceType
method getMarker = pieceMarker
val mutable pieceBoard : 'a board option = None
method private getBoard =
(<--) pieceBoard (* could raise None_is_none *)
method private setBoard =
fun newBoard -> pieceBoard <- ((-->) newBoard)
val mutable piecePosition : (int * int) option = None
method getPosition =
(<--) piecePosition (* could raise None_is_none *)
method private setPosition =
fun newPosition -> piecePosition <- ((-->) newPosition)
method isOnBoard () =
if pieceBoard = None then false else true
method setupOnBoard newBoard newPosition =
self#setBoard newBoard ;
self#setPosition newPosition
method removeFromBoard () =
pieceBoard <- None ;
piecePosition <- None
end
class virtual ['a] commonBoard =
object (self : 'a #board)
val virtual xsize : int
val virtual ysize : int
val virtual plane : 'a piece option array array
val virtual shouldBeEmpty : bool
val mutable emptyStatus = Empty
val mutable piecesOnBoard : 'a piece list = []
method setPiecesList piecesList =
piecesOnBoard <- piecesList
method getPiecesList () = piecesOnBoard
val mutable curHistory : 'a history option = None
method getHistory () =
(<--) curHistory (* could raise None_is_none *)
method setHistory newHistory =
curHistory <- ((-->) newHistory)
method getPiece (i, j) =
try
let curPiece = (<--) plane.(i).(j) in
(* could raise None_is_none *)
curPiece
with
| None_is_none -> raise Square_is_empty
method grabPiece (i, j) =
let curPiece = self#getPiece (i, j) in
(* could raise Square_is_empty *)
self#emptySquareInPosition (i, j) ;
curPiece#removeFromBoard () ;
curPiece
method putPiece (i, j) curPiece =
curPiece#setupOnBoard (self :> 'a board) (i, j) ;
plane.(i).(j) <- (-->) curPiece
method emptySquareInPosition (i, j) =
plane.(i).(j) <- None
method squareIsEmpty (i, j) =
try
let _ = self#getPiece (i, j) in
(* could raise Square_is_empty *)
Not_empty
with
| Square_is_empty -> Empty
method isItValidPosition (i, j) =
if 0 <= i && i < xsize && 0 <= j && j < ysize then
Within_bounds
else
Out_of_bounds
method checkColorInPosition (i, j) expectedColor =
let pieceColor = (self#getPiece (i, j))#getColor in
(* could raise Square_is_empty *)
if expectedColor = pieceColor then
Same
else
Different
method setBoardStatus newStatus =
emptyStatus <- newStatus
method getBoardStatus () = emptyStatus
method print () =
let rec concatenateString n s =
if n > 0 then s ^ concatenateString (n - 1) s else ""
in
let markerString (i, j) =
try
let curMarker = (self#getPiece (i, j))#getMarker in
curMarker
with
| Square_is_empty -> " "
in
let rec print (i, j) =
if j >= 0 then
if i < xsize then
(
print_string ("| " ^ (markerString (i, j)) ^ " ") ;
print (i + 1, j)
)
else
(
print_string "|\n" ;
print_string ((concatenateString xsize "+---") ^ "+\n") ;
print (0, j - 1)
)
in
print_string ((concatenateString xsize "+---") ^ "+\n") ;
print (0, ysize - 1)
end
class virtual ['a] commonRules =
object (self : 'a #rules)
val virtual boardSize : int * int
val virtual numberOfPlayers : int
val virtual firstPlayer : string
val virtual emptyBoard : bool
val virtual pieceColors : string list
val virtual pieceTypes : string list
method private getPieceColors = pieceColors
method private getPieceTypes = pieceTypes
method getBoardSize () = boardSize
method getNumberOfPlayers () = numberOfPlayers
method getFirstPlayer () = firstPlayer
method virtual private getPieceMarker : string * string -> string
method virtual private getPiecePositions : string * string -> (int * int) list
method virtual private buildPiece : string -> string -> 'a piece
method virtual private buildMove : 'a board -> string -> string -> 'a move
method getBoard () : 'a board =
let size = self#getBoardSize () in
object (selfBoard)
inherit ['a] commonBoard
val xsize = fst size
val ysize = snd size
val plane = Array.make_matrix (fst size) (snd size) None
val shouldBeEmpty = emptyBoard
method makePiece piecePosition pieceColor pieceType =
let newPiece = self#buildPiece pieceColor pieceType in
selfBoard#putPiece piecePosition newPiece
end
method setupBoard (theBoard : 'a board) (theHistory : 'a history) =
let buildPiecesList () =
(
let thirdFun x y z resultList =
let i = fst z and j = snd z in
let newPiece = self#buildPiece x y in
match theBoard#squareIsEmpty (i, j) with
| Empty ->
theBoard#putPiece (i, j) newPiece ;
newPiece :: resultList
| Not_empty -> raise Emptiness
in
let secondFun x y resultList =
List.fold_right (thirdFun x y) (self#getPiecePositions (x, y)) resultList
in
let firstFun x resultList =
List.fold_right (secondFun x) self#getPieceTypes resultList
in
List.fold_right firstFun self#getPieceColors []
)
in
let piecesList = buildPiecesList () in
(* could raise Emptiness *)
if List.length piecesList <> 0 then
(
theBoard#setHistory theHistory ;
theBoard#setBoardStatus Not_empty ;
theBoard#setPiecesList piecesList
)
method getMove theBoard thePositions thePlayer =
self#buildMove theBoard thePositions thePlayer
end
class chess : [chess] rules =
object (self)
inherit [chess] commonRules
val boardSize = (8, 8)
val numberOfPlayers = 2
val firstPlayer = "White"
val emptyBoard = false
val pieceColors = ["White"; "Black"]
val pieceTypes = ["King"; "Queen"; "Rook"; "Bishop"; "Knight"; "Pawn"]
method private getPieceMarker (pieceColor, pieceType) =
let getMarker x =
match x with
| "King" -> "K"
| "Queen" -> "Q"
| "Rook" -> "R"
| "Bishop" -> "B"
| "Knight" -> "N"
| "Pawn" -> "P"
| _ -> raise Unknown_piece
in
match pieceColor with
| "White" -> String.lowercase_ascii (getMarker pieceType)
| "Black" -> String.uppercase_ascii (getMarker pieceType)
| _ -> raise Unknown_color
method private getPieceType x =
match x with
| "K" -> "King"
| "Q" -> "Queen"
| "R" -> "Rook"
| "B" -> "Bishop"
| "N" -> "Knight"
| "P" -> "Pawn"
| _ -> raise Unknown_piece
method private getPiecePositions x =
match x with
| ("White", "King") -> [(4, 0)]
| ("White", "Queen") -> [(3, 0)]
| ("White", "Rook") -> [(0, 0); (7, 0)]
| ("White", "Bishop") -> [(2, 0); (5, 0)]
| ("White", "Knight") -> [(1, 0); (6, 0)]
| ("White", "Pawn") -> [(0, 1); (1, 1); (2, 1); (3, 1);
(4, 1); (5, 1); (6, 1); (7, 1)]
| ("Black", "King") -> [(4, 7)]
| ("Black", "Queen") -> [(3, 7)]
| ("Black", "Rook") -> [(0, 7); (7, 7)]
| ("Black", "Bishop") -> [(2, 7); (5, 7)]
| ("Black", "Knight") -> [(1, 7); (6, 7)]
| ("Black", "Pawn") -> [(0, 6); (1, 6); (2, 6); (3, 6);
(4, 6); (5, 6); (6, 6); (7, 6)]
| _ -> raise Unknown_piece
method private getCheckDestFun x empty initial passing =
match x with
| (_, "King") ->
(
fun (dx, dy) ->
if (abs dx = 0 || abs dx = 1) && (abs dy = 0 || abs dy = 1) then
(self#getCheckDestFun ("", "Queen") empty initial passing) (dx, dy)
else
Invalid
)
| (_, "Queen") ->
(
fun (dx, dy) ->
if self#getCheckDestFun ("", "Rook")
empty initial passing (dx, dy) = Valid
|| self#getCheckDestFun ("", "Bishop")
empty initial passing (dx, dy) = Valid
then Valid else Invalid
)
| (_, "Rook") ->
(
fun (dx, dy) ->
if (dx = 0 && dy <> 0) || (dx <> 0 && dy = 0)
then Valid else Invalid
)
| (_, "Bishop") ->
(
fun (dx, dy) ->
if abs dx = abs dy
then Valid else Invalid
)
| (_, "Knight") ->
(
fun (dx, dy) ->
if (abs dx = 1 && abs dy = 2) || (abs dx = 2 && abs dy = 1)
then Valid else Invalid
)
| ("White", "Pawn") ->
(
fun (dx, dy) ->
let pawnValidMove (dx, dy) =
match (empty, initial, passing) with
| (Empty, true, _) -> (dx = 0 && (dy = 1 || dy = 2))
| (Empty, false, false) -> (dx = 0 && dy = 1)
| (Empty, false, true)
| (Not_empty, _, _) -> ((dx = -1 || dx = 1) && dy = 1)
in
if pawnValidMove (dx, dy)
then Valid else Invalid
)
| ("Black", "Pawn") ->
(
fun (dx, dy) ->
let pawnValidMove (dx, dy) =
match (empty, initial, passing) with
| (Empty, true, _) -> (dx = 0 && (dy = -1 || dy = -2))
| (Empty, false, false) -> (dx = 0 && dy = -1)
| (Empty, false, true)
| (Not_empty, _, _) -> ((dx = -1 || dx = 1) && dy = -1)
in
if pawnValidMove (dx, dy)
then Valid else Invalid
)
| _ -> raise Unknown_piece
method private intOfBoardFile x =
match x with
| "a" -> 0 | "b" -> 1 | "c" -> 2 | "d" -> 3
| "e" -> 4 | "f" -> 5 | "g" -> 6 | "h" -> 7
| _ -> raise Bad_arguments
method private boardFileOfInt x =
match x with
| 0 -> "a" | 1 -> "b" | 2 -> "c" | 3 -> "d"
| 4 -> "e" | 5 -> "f" | 6 -> "g" | 7 -> "h"
| _ -> raise Bad_arguments
method private buildPiece pieceColor pieceType =
let pieceMarker = self#getPieceMarker (pieceColor, pieceType) in
let pieceCheckDest = self#getCheckDestFun (pieceColor, pieceType) in
object (selfPiece)
inherit [chess] commonPiece pieceColor pieceType pieceMarker
val mutable movedPiece = false
method setMoved () = movedPiece <- true
method unsetMoved () = movedPiece <- false
method getMoved () = movedPiece
method private directionIsEmpty pStart pEnd =
let getDirection (i, j) =
let reduceNumber x =
match x with
| 0 -> 0
| _ -> (x / (abs x))
in
(reduceNumber i, reduceNumber j)
in
let rec allSquaresAreEmpty (i, j) curDirection n =
let curPosition = (i, j) ++ curDirection in
match n with
| 1 -> Empty
| n ->
match selfPiece#getBoard#squareIsEmpty curPosition with
| Empty -> allSquaresAreEmpty curPosition curDirection (n - 1)
| Not_empty -> Not_empty
in
let howManySteps (i, j) =
let steps = max (abs i) (abs j) in
match steps with
| 0 -> Same_square
| 1 -> Adjacent_square
| n -> Steps n
in
let pDiff = pEnd -- pStart in
let direction = getDirection pDiff
and steps = howManySteps pDiff
in
match steps with
| Same_square -> Not_reachable
| Adjacent_square -> Reachable
| Steps n ->
if allSquaresAreEmpty pStart direction n = Empty
|| pieceType = "Knight"
then
Reachable
else
Not_reachable
method private checkInPassing pStart pEnd =
try
(
let pDiff = pEnd -- pStart
and lastMove = (selfPiece#getBoard#getHistory ())#getLastMove ()
in
let opposingPawnStart = pEnd ++ (0, snd pDiff)
and opposingPawnEnd = pEnd -- (0, snd pDiff)
in
match (selfPiece#getColor, selfPiece#getType) with
| ("White", "Pawn") ->
lastMove#checkThisMove
"Black" "Pawn" opposingPawnStart opposingPawnEnd
| ("Black", "Pawn") ->
lastMove#checkThisMove
"White" "Pawn" opposingPawnStart opposingPawnEnd
| _ -> false
)
with
| Emptiness -> false
method private destinationIsValid pStart pEnd =
let pDiff = pEnd -- pStart
and initialOrNot = not (selfPiece#getMoved ())
and emptyOrNot = selfPiece#getBoard#squareIsEmpty pEnd
and passingOrNot = selfPiece#checkInPassing pStart pEnd
in
pieceCheckDest emptyOrNot initialOrNot passingOrNot pDiff
method pieceCouldBeCaptured position =
let piecesOnBoard =
List.fold_right
(
fun curPiece listResult ->
if curPiece#isOnBoard () then
curPiece :: listResult
else
listResult
) (selfPiece#getBoard#getPiecesList ()) []
in
let searchOpponentPieces =
List.fold_right
(
fun curPiece listResult ->
if curPiece#getColor <> selfPiece#getColor
&& curPiece#checkMove position = Capture_move then
curPiece :: listResult
else
listResult
) piecesOnBoard []
in
if List.length searchOpponentPieces <> 0 then
true
else
false
method checkMove pEnd =
let pStart = selfPiece#getPosition in
let emptyOrNot = selfPiece#getBoard#squareIsEmpty pEnd in
let validOrNot = selfPiece#destinationIsValid pStart pEnd in
let reachableOrNot =
if validOrNot = Valid then
selfPiece#directionIsEmpty pStart pEnd
else
Not_reachable
in
let passingOrNot = selfPiece#checkInPassing pStart pEnd in
match (emptyOrNot, validOrNot, reachableOrNot) with
| (Empty, Valid, Reachable) ->
if not passingOrNot then
Simple_move
else
In_passing
| (Not_empty, Valid, Reachable) ->
let destPiece = selfPiece#getBoard#getPiece pEnd in
(* could NOT raise Square_is_empty *)
if destPiece#getColor <> selfPiece#getColor then
Capture_move
else if destPiece#getType = "King" && selfPiece#getType = "Rook"
&& not (destPiece#getMoved ())
&& not (selfPiece#getMoved ())
&& not (selfPiece#pieceCouldBeCaptured pStart)
&& not (selfPiece#pieceCouldBeCaptured pEnd)
then
Special_move
else
Improper_move
| (_, Invalid, _) -> Improper_move
| (_, _, Not_reachable) -> Improper_move
end
method translatePositions x setOfPieces curPlayer =
(* a move must be written in short algebraic notation *)
let translatePoint x =
if Str.string_match (Str.regexp "^[a-h][1-8]$") x 0 then
let p = (self#intOfBoardFile (String.sub x 0 1), ((int_of_string (String.sub x 1 1)) - 1)) in
p
else
raise Bad_arguments
in
let translateDoublePoint x =
if Str.string_match (Str.regexp "^[a-h][1-8][a-h][1-8]$") x 0 then
let startEnd = Str.global_replace (Str.regexp "-") "" x in
let pStart = translatePoint (String.sub startEnd 0 2) in
let pEnd = translatePoint (String.sub startEnd 2 2) in
[pStart; pEnd]
else
raise Bad_arguments
in
let isPawn x =
if Str.string_match (Str.regexp "^[KQNRB]") x 0 then
false
else
true
in
let searchPiece setOfPieces pieceColor pieceType pEnd =
let filterPieceFun curPiece resultList =
if curPiece#getColor = pieceColor
&& curPiece#getType = pieceType
&& curPiece#isOnBoard ()
&& curPiece#checkMove pEnd <> Improper_move
then curPiece :: resultList
else resultList
in
List.fold_right filterPieceFun setOfPieces []
in
let searchUnambiguously setOfPieces pieceType pEnd fstSnd halfStart =
let piecesToMove = searchPiece setOfPieces curPlayer pieceType pEnd in
let filterPieceFun curPiece resultList =
if fstSnd (curPiece#getPosition) = halfStart then
curPiece :: resultList
else
resultList
in
List.fold_right filterPieceFun piecesToMove []
in
let printAmbiguousMoves listOfPieces =
print_endline "This move is ambiguous! Available pieces are:" ;
List.iter
(
fun x ->
let pieceColor = x#getColor
and pieceType = x#getType
and piecePosition = string_of_position
x#getPosition
(self#boardFileOfInt)
(fun x -> string_of_int (x + 1))
in
print_endline
(
pieceColor ^ " " ^
pieceType ^ " in " ^
piecePosition
)
)
listOfPieces
in
let x = Str.global_replace (Str.regexp "[x+]") "" x in
let pieceType = if isPawn x then "Pawn" else self#getPieceType (String.sub x 0 1)
and x =
if isPawn x || (Str.string_match (Str.regexp "^O") x 0) then
x
else
(String.sub x 1 ((String.length x) - 1))
in
if Str.string_match (Str.regexp "^[a-h][1-8]\\(=[KQNRB]\\)?$") x 0 then
let pEnd = translatePoint (String.sub x 0 2) in
let pieceToMove =
searchPiece setOfPieces curPlayer pieceType pEnd
in
match List.length pieceToMove with
| 1 -> let pStart = (List.hd pieceToMove)#getPosition in
[pStart; pEnd]
| 0 -> raise Wrong_move
| _ ->
(
printAmbiguousMoves pieceToMove ;
raise Wrong_move
)
else if Str.string_match (Str.regexp "^[a-h][a-h][1-8]$") x 0 then
let pieceFile = self#intOfBoardFile (String.sub x 0 1)
and pEnd = translatePoint (String.sub x 1 2)
in
let pieceToMove =
searchUnambiguously setOfPieces pieceType pEnd (fst) pieceFile
in
match List.length pieceToMove with
| 1 -> let pStart = (List.hd pieceToMove)#getPosition in
[pStart; pEnd]
| 0 -> raise Wrong_move
| _ ->
(
printAmbiguousMoves pieceToMove ;
raise Wrong_move
)
else if Str.string_match (Str.regexp "^[1-8][a-h][1-8]$") x 0 then
let pieceRank = (int_of_string (String.sub x 0 1)) - 1
and pEnd = translatePoint (String.sub x 1 2)
in
let pieceToMove =
searchUnambiguously setOfPieces pieceType pEnd (snd) pieceRank
in
match List.length pieceToMove with
| 1 -> let pStart = (List.hd pieceToMove)#getPosition in
[pStart; pEnd]
| 0 -> raise Wrong_move
| _ ->
(
printAmbiguousMoves pieceToMove ;
raise Wrong_move
)
else if Str.string_match (Str.regexp "^[a-h][1-8][a-h][1-8]$") x 0 then
translateDoublePoint x
else if Str.string_match (Str.regexp "^O-O-O$") x 0 then
let kingPosition =
List.hd (self#getPiecePositions (curPlayer, "King"))
in
let rookPosition =
(* let searchQueensideRook = *)
List.hd (self#getPiecePositions (curPlayer, "Rook"))
in
[rookPosition; kingPosition]
else if Str.string_match (Str.regexp "^O-O$") x 0 then
let kingPosition =
List.hd (self#getPiecePositions (curPlayer, "King"))
in
let rookPosition =
List.hd (List.tl (self#getPiecePositions (curPlayer, "Rook")))
in
[rookPosition; kingPosition]
else
raise Bad_arguments
method translatePromotion x =
let x = Str.global_replace (Str.regexp "[x+]") "" x in
if Str.string_match (Str.regexp "^[a-h][1-8]=[KQNRB]$") x 0 then
let pieceMarker = (String.sub x 3 1) in
self#getPieceType pieceMarker
else
""
method private buildMove theBoard thePositions thePlayer =
object (selfMove)
val curBoard = theBoard
method private getBoard = curBoard
val mutable pStart = (0, 0)
val mutable pEnd = (0, 0)
val mutable moveResult = Improper_move
method getTypeOfMove () = moveResult
method isCaptureMove () =
match moveResult with
| Capture_move | In_passing -> true
| Simple_move | Special_move | Improper_move -> false
val mutable curCapturedPiece : 'a piece option = None
method getCapturedPiece () = (<--) curCapturedPiece
val mutable curPiece = None
method private getCurPiece () = (<--) curPiece
val mutable firstPieceMove = false
method private setFirstMove () = firstPieceMove <- true
method private getFirstMove () = firstPieceMove
initializer
let piecesList = curBoard#getPiecesList () in
let translatedPositions = self#translatePositions thePositions piecesList thePlayer in
pStart <- List.hd translatedPositions ;
pEnd <- List.hd (List.tl translatedPositions) ;
if selfMove#getBoard#isItValidPosition pStart = Out_of_bounds
|| selfMove#getBoard#isItValidPosition pEnd = Out_of_bounds
then
raise Boundedness
else if selfMove#getBoard#checkColorInPosition pStart thePlayer = Different
then
raise Wrong_color
else
curPiece <- (-->) (selfMove#getBoard#getPiece pStart)