summaryrefslogtreecommitdiff
path: root/testsuite/tests/ghc-regress/programs/andy_cherry/InterpUtils.hs
blob: 820163e8fd0226e2d678f24954e72d682ff24628 (plain)
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

 module InterpUtils where

 import GenUtils
 import DataTypes
 import Data.Array -- 1.3





 findCastleKMove brd = (castleK,makeACastleK brd)
 findCastleQMove brd = (castleQ,makeACastleQ brd)

 findAPawnMove
       :: ExBoardPos 
       -> ExBoardPos 
       -> Maybe Piece 
       -> Board
       -> (String,Board)





 findAPawnMove move_src move_dest queen brd@(Board arr mv _) 
       = debug (move_txt,new_brd)
   where

       move_colour = getMoveColour mv

       debug   = {- trace (
               {- userFormat brd ++ -}
               userFormat (getMoveColour mv) ++ 
               -- " (" ++ userFormat absmove ++ ")" ++
               "\nALL   :" ++ unwords (map userFormat all_moves) ++
               "\n") -} id 




       correct_src = concat (map (getAllMovesFor brd) currPieces)

       currPieces  =
               [ (Pawn,x,y) |
                       (x,y) <- start_range,
                       r <- [arr ! (x,y)],
                       lookupSquare move_colour r == Friendly,
                       (Just Pawn) <- [getSquarePiece r]]



       start_range
          = case (move_src,move_dest) of
               ((Just f,Just r),_) -> [(f,r)]
               ((Just f,_),_) -> [(f,r) | r <- [2..7]]
               -- no capture !
               (_,(Just f,_)) -> [(f,r) | r <- [2..7]]
               _ -> error "strange pawn move:"

       the_correct_move = if (length correct_move /= 1)
                          then error ("\nAmbiguous move:"
               ++ show (unwords (map userFormat correct_move))
               ++ ":" ++ {- userFormat absmove ++ -} "\n"
               ++ userFormat brd)
               else head correct_move

       correct_move = 
               filter (sameQueening queen.extractSpecialFromPlayMove)
               (filter (compExBPandBP move_dest.extractDestFromPlayMove)
                       correct_src)
       sameQueening (Just p) (Queening p') = p == p'
       sameQueening Nothing  (Queening p') = Queen == p'
       sameQueening _ _ = True

       move_txt = createShortMove the_correct_move "" brd
       corr_txt = 
           userFormatBoardPos 
               (extractSrcFromPlayMove the_correct_move) ++
           userFormatBoardPos
               (extractDestFromPlayMove the_correct_move) 
               {- queening ?? -}
       new_brd = makeAMove brd the_correct_move





 findAMove
       :: Piece
       -> ExBoardPos 
       -> ExBoardPos 
       -> Board
       -> (String,Board)

 findAMove move_piece move_src move_dest brd@(Board arr mv _) 
       = debug (move_txt,new_brd)
   where



       move_colour = getMoveColour mv

       debug   = {- trace (
               {- userFormat brd ++ -}
               userFormat (getMoveColour mv) ++ 
               " (" ++ {- userFormat absmove ++ -} ")" ++
               "\nALL   :" ++ unwords (map userFormat all_moves) ++
               "\nDEST  :" ++ unwords (map userFormat correct_dest) ++
               "\nSRC   :" ++ unwords (map userFormat correct_move) ++
               "\n") -} id 
               




       all_moves = allValidMoves brd move_piece (const True)



       correct_dest = filter
               (compExBPandBP move_dest.extractDestFromPlayMove)
                       all_moves
       correct_move = filter
               (compExBPandBP move_src.extractSrcFromPlayMove)
                       correct_dest
       the_correct_move = if (length correct_move /= 1)
                          then error ("\nAmbiguous move:"
               ++ show (unwords (map userFormat correct_move))
               ++ ":" {- ++ userFormat absmove -} ++ "\n"
               ++ userFormat brd)
               else head correct_move
       disamb = case move_dest of
                 (Just _,Nothing) -> ""        -- fg => fxg4, no disambig.
                 _ -> disAmb
                    (extractSrcFromPlayMove the_correct_move)
                    (map (extractSrcFromPlayMove) correct_dest)

       move_txt = createShortMove the_correct_move disamb brd
       corr_txt = 
           userFormatBoardPos 
               (extractSrcFromPlayMove the_correct_move) ++
           userFormatBoardPos
               (extractDestFromPlayMove the_correct_move) 
               {- queening -}
       new_brd = makeAMove brd the_correct_move
 --partain: findAMove _ _ _ brd = error ("strange move: ")

 allValidMoves :: Board -> Piece -> (ChessFile -> Bool) -> [PlayMove]
 allValidMoves brd piece corr_file
   = concat (map (getAllMovesFor brd) (getCurrPieces brd piece corr_file)) 

 getCurrPieces 
       :: Board 
       -> Piece 
       -> (ChessFile -> Bool)
       -> [(Piece,ChessFile,ChessRank)]
 getCurrPieces (Board arr (MoveNumber _ col) _) pc corr_file =
       [ (p,x,y) |
               ((x,y), r) <- assocs arr,
               lookupSquare col r == Friendly,
               (Just p) <- [getSquarePiece r],
               p == pc,
               corr_file x
                ]






 getAllMovesFor :: Board -> (Piece,Int,Int) -> [PlayMove]



 getAllMovesFor brd (Rook,x,y) = 
       [ mkPlayMove Rook (x,y) (x',y')
         | (x',y') <- (
               movePiece 0    1 brd x y ++
               movePiece 0 (-1) brd x y ++
               movePiece 1    0 brd x y ++
               movePiece (-1) 0 brd x y) ]
 getAllMovesFor brd (Bishop,x,y) = 
       [ mkPlayMove Bishop (x,y) (x',y')
         | (x',y') <- (
               movePiece 1      1  brd x y ++
               movePiece 1    (-1) brd x y ++
               movePiece (-1)   1  brd x y ++
               movePiece (-1) (-1) brd x y) ]
 getAllMovesFor brd (Queen,x,y) = 
       [ mkPlayMove Queen (x,y) (x',y')
         | (x',y') <- (
               movePiece 0    1    brd x y ++
               movePiece 0 (-1)    brd x y ++
               movePiece 1    0    brd x y ++
               movePiece (-1) 0    brd x y ++
               movePiece 1      1  brd x y ++
               movePiece 1    (-1) brd x y ++
               movePiece (-1)   1  brd x y ++
               movePiece (-1) (-1) brd x y) ]



 getAllMovesFor brd (Knight,x,y) =
       [ mkPlayMove Knight (x,y) (x',y')
           | (xd,yd) <- concat 
                       [ [(d1,d2 * 2),(d1 * 2,d2)]
                               | d1 <- [1,-1], d2 <- [1,-1]],
               x' <- [xd + x],
               y' <- [yd + y],
               case lookupBoard brd (x',y') of
                 Vacant -> True
                 Friendly -> False
                 Baddy -> True
                 OffBoard -> False]

 getAllMovesFor brd (King,x,y) =
       [ mkPlayMove King (x,y) (x',y')
           | (xd,yd) <- [(1,1),(1,0),(1,-1),(0,1),
                         (0,-1),(-1,1),(-1,0),(-1,-1)],
               x' <- [xd + x],
               y' <- [yd + y],
               case lookupBoard brd (x',y') of
                 Vacant -> True
                 Friendly -> False
                 Baddy -> True
                 OffBoard -> False]




 getAllMovesFor brd@(Board _ (MoveNumber _ col) may_ep) (Pawn,x,y) 
       = real_pawn_moves
   where
       pawn_moves = 
               case lookupBoard brd (x,y+del) of
                 Friendly -> []
                 Baddy -> []
                 Vacant -> (mkPlayMove Pawn (x,y) (x,y+del) :
                    if y /= sta then [] else
                    case lookupBoard brd (x,y+del*2) of
                       Friendly -> []
                       Baddy -> []
                       Vacant -> 
                         [ PlayMove Pawn (x,y) (x,y+del*2) BigPawnMove])
       left_pc = case lookupBoard brd (x-1,y+del) of
                        Baddy -> [mkPlayMove Pawn (x,y) (x-1,y+del) ]
                        _ -> []
       right_pc = case lookupBoard brd (x+1,y+del) of
                        Baddy -> [mkPlayMove Pawn (x,y) (x+1,y+del) ]
                        _ -> []
       all_pawn_moves = pawn_moves ++ left_pc ++ right_pc 
       real_pawn_moves = en_passant ++
               (if y + del == qn       -- if can queens
               then concat [ let fn = PlayMove Pawn f t . Queening
                             in
                               [ fn Queen,
                                 fn Rook,
                                 fn Bishop,
                                 fn Knight ]
                               | PlayMove _ f t _ <- all_pawn_moves ]
                 else all_pawn_moves)
       en_passant = 
           case (y == ep,may_ep) of
               (True,Just f) | f == x+1 || f == x-1 
                 -> [PlayMove Pawn (x,y) (f,y+del) EnPassant]
               _ -> []
       del,sta,qn,ep :: Int
       (del,sta,qn,ep) -- delta (direction), start, Queening and E.P. Rank
           = case col of
               White -> (1,2,8,5)
               Black -> (-1,7,1,4)

 movePiece xd yd brd x y = 
       case lookupBoard brd (x',y') of
         OffBoard -> []
         Friendly -> []
         Baddy    -> [(x',y')]
         Vacant   ->  (x',y') : movePiece xd yd brd x' y'
       where
           x' = x + xd
           y' = y + yd 




 makeAMove :: Board -> PlayMove -> Board
 makeAMove board@(Board brd mv@(MoveNumber _ col) _)
       move@(PlayMove piece pos pos' NothingSpecial)  =
       Board (brd //  [ pos =: VacantSq,
                       pos' =: mkColBoardSq col piece ])
                       (incMove mv) Nothing
 makeAMove board@(Board brd mv@(MoveNumber _ col) _)
       move@(PlayMove piece pos@(f,_) pos' BigPawnMove)  =
       Board (brd //  [ pos =: VacantSq,
                       pos' =: mkColBoardSq col piece ])
                       (incMove mv) (Just f)
 makeAMove board@(Board brd mv@(MoveNumber _ col) _)
       move@(PlayMove piece pos@(f,_) pos' (Queening q))  =
       Board (brd //  [ pos =: VacantSq,
                       pos' =: mkColBoardSq col q])
                       (incMove mv) (Just f)
 makeAMove board@(Board brd mv@(MoveNumber _ col) _)   -- ASSERT ?
       move@(PlayMove piece (f,_) (f',_) EnPassant) =
       Board (brd // [ (f,st) =: VacantSq,
                       (f',fn) =: mkColBoardSq col Pawn,
                       (f',st) =: VacantSq ])
                       (incMove mv) Nothing
   where (st,fn) = case col of
                     White -> (5,6)
                     Black -> (4,3)

 makeACastleK (Board brd mv@(MoveNumber _ White) _) =
       Board (brd //
             [ (5,1) =: VacantSq,
               (6,1) =: mkColBoardSq White Rook,
               (7,1) =: mkColBoardSq White King,
               (8,1) =: VacantSq ]) (incMove mv) Nothing
 makeACastleK (Board brd mv@(MoveNumber _ Black) _) =

       Board (brd //
             [ (5,8) =: VacantSq,
               (6,8) =: mkColBoardSq Black Rook,
               (7,8) =: mkColBoardSq Black King,
               (8,8) =: VacantSq ]) (incMove mv) Nothing
 makeACastleQ (Board brd mv@(MoveNumber _ White) _) =
       Board (brd //
             [ (5,1) =: VacantSq,
               (4,1) =: mkColBoardSq White Rook,
               (3,1) =: mkColBoardSq White King,
               (1,1) =: VacantSq ]) (incMove mv) Nothing
 makeACastleQ (Board brd mv@(MoveNumber _ Black) _) =
       Board (brd //
             [ (5,8) =: VacantSq,
               (4,8) =: mkColBoardSq Black Rook,
               (3,8) =: mkColBoardSq Black King,
               (1,8) =: VacantSq ]) (incMove mv) Nothing

 disAmb _ [_] = ""
 disAmb (a,b) t@[(n,m),(x,y)] 
       | n == x    = userFormatRank b
       | otherwise = userFormatFile a
 disAmb src lst = error ("PANIC: cant disambiguate: " ++ show src ++ show lst)

 createShortMove :: PlayMove -> String -> Board -> String
 createShortMove (PlayMove Pawn (f,r) dest q) "" brd = 
       (if lookupBoard brd dest == Baddy || EnPassant == q
        then userFormatFile f ++ "x" ++ userFormatBoardPos dest
        else userFormatBoardPos dest) ++
       case q of
         Queening p -> "=" ++ userFormat p
         _ -> ""
 createShortMove (PlayMove p _ dest _) extra brd =
       userFormat p ++ extra ++ capt ++ userFormatBoardPos dest
   where
       capt = if lookupBoard brd dest == Baddy
              then "x"
              else ""

 getEPStart :: Colour -> ChessFile
 getEPStart White = 5
 getEPStart Black = 4

 getEPEnd :: Colour -> ChessFile
 getEPEnd White = 6
 getEPEnd Black = 3

 getHomeRank :: Colour -> ChessRank
 getHomeRank White = 1
 getHomeRank Black = 8