You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
3.5 KiB

9 years ago
9 years ago
  1. import Datatypes
  2. import VM
  3. import Data.Hashable (hash, Hashable(..))
  4. import Data.Set (Set)
  5. import qualified Data.Set as Set
  6. import Test.QuickCheck
  7. test :: (Cell -> Cell) -> [Bool]
  8. test f = [f (0,3) == (2,5), f (1,3) == (3, 6), f (2,3) == (3,7), f (3, 3) == (4,8), f (2, 4) == (2,7)]
  9. test2 :: (Cell -> Cell) -> [Bool]
  10. test2 f = [f (2,5) == (3,6), f (3,6) == (2, 7), f (2,7) == (1,7), f (1, 7) == (1,6), f (1, 6) == (1,5), f (1, 5) == (2, 5)]
  11. deepCheck p = quickCheckWith (stdArgs { maxSuccess = 10000 }) p
  12. testTutto = do
  13. deepCheck (\pivot cell -> let r2 = rotateCell pivot in (r2 . r2 . r2 . r2 . r2 . r2) cell == cell)
  14. deepCheck (\pivot cell -> cell == counterRotateCell pivot (rotateCell pivot cell))
  15. deepCheck (\pivot cell -> cell == counterRotateCell2 pivot (rotateCell pivot cell))
  16. checkForNothing::[Maybe(Cell, Fallimento)] -> Maybe(Cell, Fallimento)
  17. checkForNothing (Nothing:xs) = checkForNothing xs
  18. checkForNothing ((Just v):xs) = Just v
  19. checkForNothing [] = Nothing
  20. testTutteRotazioni = checkForNothing [
  21. testDoppieRotazioni (2,3) [(2,0), (4,1), (5,4), (3,6), (0,5), (0,2), (2,0)],
  22. testDoppieRotazioni (2,4) [(2,1), (4,3), (4,6), (1,7), (-1,5), (0,2), (2,1)],
  23. testDoppieRotazioni (1,5) [(1,3), (3,4), (3,6), (1,7), (0,6), (0,4), (1,3)]
  24. ]
  25. testCentering = deepCheck (\up um um1 -> let b = Board 5 10 (Set.fromList [])
  26. u = Unit (Set.fromList (um1:um)) up
  27. c = centerUnit u b
  28. bw = 5
  29. xCoords = Set.map (\(x, y) -> x) (unitMembers c)
  30. yCoords = Set.map (\(x, y) -> y) (unitMembers c)
  31. unitLeft = Set.findMin xCoords
  32. unitRight = Set.findMax xCoords
  33. unitTop = Set.findMin yCoords
  34. deltaLeft = unitLeft
  35. deltaRight = bw - unitRight - 1
  36. in (deltaLeft == deltaRight || (deltaRight - deltaLeft) == 1) && unitTop == 0)
  37. testGame = let board = Board 5 10 (Set.fromList [])
  38. unit = Unit (Set.fromList [(2, 8)]) (2, 8)
  39. game = Game board [unit] (Set.fromList [hash unit])
  40. in game
  41. data Fallimento = Rotazione | ControRotazione deriving Show
  42. testDoppieRotazioni :: Cell -> [Cell] -> Maybe (Cell, Fallimento)
  43. testDoppieRotazioni pivot cells =
  44. case testRotazioni pivot cells of
  45. Just v -> Just (v, Rotazione)
  46. Nothing -> case testControRotazioni pivot (reverse cells) of
  47. Just w -> Just (w, ControRotazione)
  48. Nothing -> Nothing
  49. -- restituisce eventualmente il primo valore per cui fallisce
  50. testRotazioni :: Cell -> [Cell] -> Maybe Cell
  51. testRotazioni _ [] = Nothing
  52. testRotazioni _ [x] = Nothing
  53. testRotazioni pivot (x:y:xs) = if rotateCell pivot x == y
  54. then testRotazioni pivot (y:xs)
  55. else Just x
  56. -- restituisce eventualmente il primo valore per cui fallisce
  57. testControRotazioni :: Cell -> [Cell] -> Maybe Cell
  58. testControRotazioni _ [] = Nothing
  59. testControRotazioni _ [x] = Nothing
  60. testControRotazioni pivot (x:y:xs) = if counterRotateCell pivot x == y
  61. then testControRotazioni pivot (y:xs)
  62. else Just x