Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fun checkPosValid((i,j):int*int, n:int) =
- if (i < 0 orelse i > n-1 orelse j < 0 orelse j > n-1) then
- false
- else
- true
- ;
- fun isSquareRevealed(Revealed(_)) = true
- | isSquareRevealed(_) = false
- ;
- fun updateRow(row:Square list, j:int) =
- let
- val square = List.nth(row, j)
- val newRow = List.take(row, j)
- in
- case (square) of
- Concealed(Mine) => newRow@[Revealed(Mine)]@List.drop(row, j+1)
- | Concealed(Blank) => newRow@[Revealed(Blank)]@List.drop(row, j+1)
- | Concealed(Digit(x)) => newRow@[Revealed(Digit(x))]@List.drop(row, j+1)
- | _ => raise IllegalArgumentException
- end
- ;
- fun updateGrid(grid:Square list list, (i,j):int*int) =
- List.take(grid, i)@[updateRow(List.nth(grid, i), j)]@List.drop(grid, i+1)
- ;
- fun revealSquare(mss:MineSweeperState, pos: int*int):MineSweeperState =
- if (checkPosValid(pos, length(#1(mss))) = false) then
- raise IllegalArgumentException
- else
- let
- val grid = updateGrid(#1(mss), pos)
- val n = length(grid)
- val status = #2(mss)
- val square = List.nth(List.nth(grid, #1(pos)), #2(pos))
- fun revealProjections(grid:Square list list, (i,j):int*int, index:int) =
- case (index) of
- (*Right*)
- 1 => if (j < n-1 andalso not(isSquareRevealed(List.nth(List.nth(grid, i), j+1)))) then
- revealProjections(#1(revealSquare((grid,OnGoing),(i,j+1))), (i,j), index+1)
- else
- revealProjections(grid, (i,j), index+1)
- (*RightDown*)
- | 2 => if (i < n-1 andalso j < n-1 andalso not(isSquareRevealed(List.nth(List.nth(grid, i+1), j+1)))) then
- revealProjections(#1(revealSquare((grid,OnGoing),(i+1,j+1))), (i,j), index+1)
- else
- revealProjections(grid, (i,j), index+1)
- (*Down*)
- | 3 => if (i < n-1 andalso not(isSquareRevealed(List.nth(List.nth(grid, i+1), j)))) then
- revealProjections(#1(revealSquare((grid,OnGoing),(i+1,j))), (i,j), index+1)
- else
- revealProjections(grid, (i,j), index+1)
- (*LeftDown*)
- | 4 => if (i < n-1 andalso j > 0 andalso not(isSquareRevealed(List.nth(List.nth(grid, i+1), j-1)))) then
- revealProjections(#1(revealSquare((grid,OnGoing),(i+1,j-1))), (i,j), index+1)
- else
- revealProjections(grid, (i,j), index+1)
- (*Left*)
- | 5 => if (j > 0 andalso not(isSquareRevealed(List.nth(List.nth(grid, i), j-1)))) then
- revealProjections(#1(revealSquare((grid,OnGoing),(i,j-1))), (i,j), index+1)
- else
- revealProjections(grid, (i,j), index+1)
- (*LeftUp*)
- | 6 => if (i > 0 andalso j > 0 andalso not(isSquareRevealed(List.nth(List.nth(grid, i-1), j-1)))) then
- revealProjections(#1(revealSquare((grid,OnGoing),(i-1,j-1))), (i,j), index+1)
- else
- revealProjections(grid, (i,j), index+1)
- (*Up*)
- | 7 => if (i > 0 andalso not(isSquareRevealed(List.nth(List.nth(grid, i-1), j)))) then
- revealProjections(#1(revealSquare((grid,OnGoing),(i-1,j))), (i,j), index+1)
- else
- revealProjections(grid, (i,j), index+1)
- (*RightUp*)
- | 8 => if (i > 0 andalso j < n-1 andalso not(isSquareRevealed(List.nth(List.nth(grid, i-1), j+1)))) then
- (#1(revealSquare((grid,OnGoing),(i-1,j+1))))
- else
- (grid)
- | _ => grid
- in
- case (square) of
- Revealed(Mine) => (grid, Failure)
- | Revealed(Blank) => let
- val newGrid = revealProjections(grid, pos, 1)
- in
- if determineStatusOf(newGrid) then
- (newGrid, Success)
- else
- (newGrid, OnGoing)
- end
- | _ => if (determineStatusOf(grid)) then
- (grid, Success)
- else
- (grid, OnGoing)
- end
- ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement