Permalink
Browse files

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...
1 parent 1594b6e commit fdc146207e2d149ed1ada726a4c09b800f1008a5 @malcolmt committed Oct 30, 2011
Showing with 15 additions and 15 deletions.
  1. +13 −1 src/ChessTools/Board/Internal.hs
  2. +2 −14 test/TestBoard.hs
@@ -24,6 +24,7 @@ data BoardSize = BoardSize {
boardVertBuffer :: Int -- ^ Vertical buffer size for
-- jumping pieces.
}
+ deriving (Show, Eq)
-- | The coordinates of a cell on the board.
--
@@ -35,7 +36,18 @@ data BoardSize = BoardSize {
-- traditional external representation of a game's cells. Thus, whilst Shogi
-- games are recorded with the rank before the column (for example, \"/7f/\"),
-- this module and its callers still use column-first ordering.
-newtype Square = Square (Int, Int) deriving (Show, Eq, Ord)
+newtype Square = Square (Int, Int) deriving (Show, Eq)
+
+-- There are times when it is convenient to be able to sort a list of squares,
+-- so we impose a somewhat arbitrary ordering on them. This is not the
+-- automatically derived version, because we want to sort by ranks ahead of
+-- files.
+instance Ord Square where
+ compare (Square (x1, y1)) (Square (x2, y2)) =
+ case y1 `compare` y2 of
+ LT -> LT
+ GT -> GT
+ EQ -> x1 `compare` x2
-- | A rapid lookup (/O(1)/) data structure for computing various values based
-- on two squares on the board. These could be distances between the squares in
View
@@ -9,10 +9,6 @@ import ChessTools.Board
import ChessTools.Board.Internal
-instance Show BoardSize where
- show (BoardSize h v vbuf) =
- "BoardSize " ++ unwords (map show [h, v, vbuf])
-
instance Arbitrary BoardSize where
arbitrary = sized $ \n -> do
let n' = n + 2
@@ -79,17 +75,9 @@ prop_square_to_index_inverse = forAll boardAndSquareGen $ \(b, sq) ->
-- As squares move from lower left ("a1" in western chess) to upper right (h8),
-- the index into the lookup table should increase.
prop_index_increases_with_square = forAll boardAndTwoSquareGen $ \(b, s1, s2) ->
- let Square (c1, r1) = s1
- Square (c2, r2) = s2
- idx1 = squareToIndex b s1
+ let idx1 = squareToIndex b s1
idx2 = squareToIndex b s2
- in case r1 `compare` r2 of
- GT -> idx1 > idx2
- LT -> idx1 < idx2
- EQ -> case c1 `compare` c2 of
- GT -> idx1 > idx2
- LT -> idx1 < idx2
- EQ -> True
+ in s1 `compare` s2 == idx1 `compare` idx2
-- The board array size should be computed correctly (this is the
-- representation of the board of pieces, not a lookup array, which is smaller).

0 comments on commit fdc1462

Please sign in to comment.