Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Minor tweaks to some class instances.

BoardSize now derives Show and Eq, since there's little point in hiding
its internals.

Square has a better "fake" total ordering (ranks before files). In
passing, this allowed me to move the same logic out of one of the tests.
  • Loading branch information...
commit fdc146207e2d149ed1ada726a4c09b800f1008a5 1 parent 1594b6e
Malcolm Tredinnick authored October 30, 2011
14  src/ChessTools/Board/Internal.hs
@@ -24,6 +24,7 @@ data BoardSize = BoardSize {
24 24
                     boardVertBuffer :: Int   -- ^ Vertical buffer size for
25 25
                                              --   jumping pieces.
26 26
                     }
  27
+                    deriving (Show, Eq)
27 28
 
28 29
 -- | The coordinates of a cell on the board.
29 30
 --
@@ -35,7 +36,18 @@ data BoardSize = BoardSize {
35 36
 -- traditional external representation of a game's cells. Thus, whilst Shogi
36 37
 -- games are recorded with the rank before the column (for example, \"/7f/\"),
37 38
 -- this module and its callers still use column-first ordering.
38  
-newtype Square = Square (Int, Int) deriving (Show, Eq, Ord)
  39
+newtype Square = Square (Int, Int) deriving (Show, Eq)
  40
+
  41
+-- There are times when it is convenient to be able to sort a list of squares,
  42
+-- so we impose a somewhat arbitrary ordering on them. This is not the
  43
+-- automatically derived version, because we want to sort by ranks ahead of
  44
+-- files.
  45
+instance Ord Square where
  46
+    compare (Square (x1, y1)) (Square (x2, y2)) =
  47
+        case y1 `compare` y2 of
  48
+            LT -> LT
  49
+            GT -> GT
  50
+            EQ -> x1 `compare` x2
39 51
 
40 52
 -- | A rapid lookup (/O(1)/) data structure for computing various values based
41 53
 -- on two squares on the board. These could be distances between the squares in
16  test/TestBoard.hs
@@ -9,10 +9,6 @@ import ChessTools.Board
9 9
 import ChessTools.Board.Internal
10 10
 
11 11
 
12  
-instance Show BoardSize where
13  
-    show (BoardSize h v vbuf) =
14  
-            "BoardSize " ++ unwords (map show [h, v, vbuf])
15  
-
16 12
 instance Arbitrary BoardSize where
17 13
     arbitrary = sized $ \n -> do
18 14
         let n' = n + 2
@@ -79,17 +75,9 @@ prop_square_to_index_inverse = forAll boardAndSquareGen $ \(b, sq) ->
79 75
 -- As squares move from lower left ("a1" in western chess) to upper right (h8),
80 76
 -- the index into the lookup table should increase.
81 77
 prop_index_increases_with_square = forAll boardAndTwoSquareGen $ \(b, s1, s2) ->
82  
-    let Square (c1, r1) = s1
83  
-        Square (c2, r2) = s2
84  
-        idx1 = squareToIndex b s1
  78
+    let idx1 = squareToIndex b s1
85 79
         idx2 = squareToIndex b s2
86  
-    in case r1 `compare` r2 of
87  
-        GT -> idx1 > idx2
88  
-        LT -> idx1 < idx2
89  
-        EQ -> case c1 `compare` c2 of
90  
-                GT -> idx1 > idx2
91  
-                LT -> idx1 < idx2
92  
-                EQ -> True
  80
+    in s1 `compare` s2 == idx1 `compare` idx2
93 81
 
94 82
 -- The board array size should be computed correctly (this is the
95 83
 -- representation of the board of pieces, not a lookup array, which is smaller).

0 notes on commit fdc1462

Please sign in to comment.
Something went wrong with that request. Please try again.