Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. fun checkPosValid((i,j):int*int, n:int) =
  2. if (i < 0 orelse i > n-1 orelse j < 0 orelse j > n-1) then
  3. false
  4. else
  5. true
  6. ;
  7.  
  8.  
  9. fun isSquareRevealed(Revealed(_)) = true
  10. | isSquareRevealed(_) = false
  11. ;
  12.  
  13.  
  14.  
  15. fun updateRow(row:Square list, j:int) =
  16. let
  17. val square = List.nth(row, j)
  18. val newRow = List.take(row, j)
  19. in
  20. case (square) of
  21. Concealed(Mine) => newRow@[Revealed(Mine)]@List.drop(row, j+1)
  22. | Concealed(Blank) => newRow@[Revealed(Blank)]@List.drop(row, j+1)
  23. | Concealed(Digit(x)) => newRow@[Revealed(Digit(x))]@List.drop(row, j+1)
  24. | _ => raise IllegalArgumentException
  25. end
  26. ;
  27.  
  28.  
  29. fun updateGrid(grid:Square list list, (i,j):int*int) =
  30. List.take(grid, i)@[updateRow(List.nth(grid, i), j)]@List.drop(grid, i+1)
  31. ;
  32.  
  33.  
  34. fun revealSquare(mss:MineSweeperState, pos: int*int):MineSweeperState =
  35. if (checkPosValid(pos, length(#1(mss))) = false) then
  36. raise IllegalArgumentException
  37. else
  38. let
  39. val grid = updateGrid(#1(mss), pos)
  40. val n = length(grid)
  41. val status = #2(mss)
  42. val square = List.nth(List.nth(grid, #1(pos)), #2(pos))
  43.  
  44. fun revealProjections(grid:Square list list, (i,j):int*int, index:int) =
  45. case (index) of
  46. (*Right*)
  47. 1 => if (j < n-1 andalso not(isSquareRevealed(List.nth(List.nth(grid, i), j+1)))) then
  48. revealProjections(#1(revealSquare((grid,OnGoing),(i,j+1))), (i,j), index+1)
  49. else
  50. revealProjections(grid, (i,j), index+1)
  51. (*RightDown*)
  52. | 2 => if (i < n-1 andalso j < n-1 andalso not(isSquareRevealed(List.nth(List.nth(grid, i+1), j+1)))) then
  53. revealProjections(#1(revealSquare((grid,OnGoing),(i+1,j+1))), (i,j), index+1)
  54. else
  55. revealProjections(grid, (i,j), index+1)
  56. (*Down*)
  57. | 3 => if (i < n-1 andalso not(isSquareRevealed(List.nth(List.nth(grid, i+1), j)))) then
  58. revealProjections(#1(revealSquare((grid,OnGoing),(i+1,j))), (i,j), index+1)
  59. else
  60. revealProjections(grid, (i,j), index+1)
  61. (*LeftDown*)
  62. | 4 => if (i < n-1 andalso j > 0 andalso not(isSquareRevealed(List.nth(List.nth(grid, i+1), j-1)))) then
  63. revealProjections(#1(revealSquare((grid,OnGoing),(i+1,j-1))), (i,j), index+1)
  64. else
  65. revealProjections(grid, (i,j), index+1)
  66. (*Left*)
  67. | 5 => if (j > 0 andalso not(isSquareRevealed(List.nth(List.nth(grid, i), j-1)))) then
  68. revealProjections(#1(revealSquare((grid,OnGoing),(i,j-1))), (i,j), index+1)
  69. else
  70. revealProjections(grid, (i,j), index+1)
  71. (*LeftUp*)
  72. | 6 => if (i > 0 andalso j > 0 andalso not(isSquareRevealed(List.nth(List.nth(grid, i-1), j-1)))) then
  73. revealProjections(#1(revealSquare((grid,OnGoing),(i-1,j-1))), (i,j), index+1)
  74. else
  75. revealProjections(grid, (i,j), index+1)
  76. (*Up*)
  77. | 7 => if (i > 0 andalso not(isSquareRevealed(List.nth(List.nth(grid, i-1), j)))) then
  78. revealProjections(#1(revealSquare((grid,OnGoing),(i-1,j))), (i,j), index+1)
  79. else
  80. revealProjections(grid, (i,j), index+1)
  81. (*RightUp*)
  82. | 8 => if (i > 0 andalso j < n-1 andalso not(isSquareRevealed(List.nth(List.nth(grid, i-1), j+1)))) then
  83. (#1(revealSquare((grid,OnGoing),(i-1,j+1))))
  84. else
  85. (grid)
  86. | _ => grid
  87. in
  88. case (square) of
  89. Revealed(Mine) => (grid, Failure)
  90. | Revealed(Blank) => let
  91. val newGrid = revealProjections(grid, pos, 1)
  92. in
  93. if determineStatusOf(newGrid) then
  94. (newGrid, Success)
  95. else
  96. (newGrid, OnGoing)
  97. end
  98. | _ => if (determineStatusOf(grid)) then
  99. (grid, Success)
  100. else
  101. (grid, OnGoing)
  102. end
  103. ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement