Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 30th, 2012  |  syntax: None  |  size: 1.25 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Creating a compound iterator in F#
  2. seq {
  3.     for y1 = 0 to BOARDSIZE-1 do
  4.         for x1 = 0 to BOARDSIZE-1 do
  5.              for dy = -2 to 2 do
  6.                  for dx = -2 to 2 do
  7.                      let x2 = x1 + dx;
  8.                      let y2 = y1 + dy;
  9.                      let currentMove = new MoveStruct(x1, y1, x2, y2);
  10.                      if (currentMove.SomeCondition = true) then
  11.                              yield currentMove;
  12.    }
  13.        
  14. let AllCells =
  15.     seq {
  16.         for y=0 to BOARDSIZE-1 do
  17.             for x=0 to BOARDSIZE-1 do
  18.                 yield (x,y);
  19.     };
  20.        
  21. let LegalMovesAround(x1,y1) =
  22.     seq {
  23.       if board.[x1, y1] = WHITE then
  24.         for dy = -2 to 2 do
  25.           for dx = -2 to 2 do
  26.                 let x2 = x1 + dx;
  27.                 let y2 = y1 + dy;
  28.                 let currentMove = new MoveStruct(x1, y1, x2, y2);
  29.                 if (currentMove.DetermineMoveType <> MoveType.ILLEGAL
  30.                     && board.[x2, y2] = NONE) then
  31.                         yield currentMove;
  32.      }
  33.        
  34. let validMoves =
  35.     AllCells
  36.     |> Seq.collect LegalMovesAround
  37.     |> Seq.distinct
  38.        
  39. let allLegalMoves = seq {
  40.   for cell in AllCells do
  41.     yield! LegalMovesAround cell
  42. }
  43.        
  44. seq {
  45.     for x,y in Allcells do
  46.         yield! LMA(x,y)
  47. }