haskellscrabble 1.2.2 → 1.3.2
raw patch · 5 files changed
+42/−7 lines, 5 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
- Wordify.Rules.FormedWord: instance GHC.Classes.Eq Wordify.Rules.FormedWord.Direction
+ Wordify.Rules.Board: emptySquaresFrom :: Board -> Pos -> Int -> Direction -> [Pos]
+ Wordify.Rules.LetterBag: bagLetters :: LetterBag -> Map Char Tile
+ Wordify.Rules.Pos: Horizontal :: Direction
+ Wordify.Rules.Pos: Vertical :: Direction
+ Wordify.Rules.Pos: data Direction
+ Wordify.Rules.Pos: instance GHC.Classes.Eq Wordify.Rules.Pos.Direction
Files
- haskellscrabble.cabal +1/−1
- src/Wordify/Rules/Board.hs +19/−3
- src/Wordify/Rules/FormedWord.hs +0/−1
- src/Wordify/Rules/LetterBag.hs +19/−2
- src/Wordify/Rules/Pos.hs +3/−0
haskellscrabble.cabal view
@@ -2,7 +2,7 @@ -- documentation, see http://haskell.org/cabal/users-guide/ name: haskellscrabble-version: 1.2.2+version: 1.3.2 synopsis: A scrabble library capturing the core game logic of scrabble. description: A scrabble library which enforces legal transitions between moves. Intended to facilitate the development of a playable game. homepage: http://www.github.com/happy0/haskellscrabble
src/Wordify/Rules/Board.hs view
@@ -3,6 +3,7 @@ allSquares, placeTile, occupiedSquareAt,+ emptySquaresFrom, lettersAbove, lettersBelow, lettersLeft,@@ -20,7 +21,8 @@ import Wordify.Rules.Board.Internal import Control.Applicative import Data.List.Split- import Data.List+ import qualified Data.List as L+ import Data.Foldable as F instance Show Board where show = prettyPrint@@ -70,17 +72,31 @@ lettersRight :: Board -> Pos -> Seq (Pos,Square) lettersRight board pos = walkFrom board pos right + {- | Finds the empty square positions horizontally or vertically from a given position,+ skipping any squares that are occupied by a tile+ -}+ emptySquaresFrom :: Board -> Pos -> Int -> Direction -> [Pos]+ emptySquaresFrom board startPos numSquares direction =+ let changing = [constant .. 15]+ in L.take numSquares $ + L.filter (isJust . unoccupiedSquareAt board) $+ mapMaybe posAt $ zipDirection (repeat constant) changing+ where+ constant = if direction == Horizontal then yPos startPos else xPos startPos+ zipDirection = if (direction == Horizontal) then flip L.zip else L.zip+ + {- | Pretty prints a board to a human readable string representation. Helpful for development. -} prettyPrint :: Board -> String prettyPrint board = rowsWithLabels ++ columnLabelSeparator ++ columnLabels where- rows = transpose . chunksOf 15 . map (squareToString . snd) . allSquares+ rows = L.transpose . chunksOf 15 . map (squareToString . snd) . allSquares rowsWithLabels = concatMap (\(rowNo, row) -> (rowStr rowNo) ++ concat row ++ "\n") . Prelude.zip [1 .. ] $ (rows board) rowStr :: Int -> String rowStr number = if number < 10 then ((show number) ++ " | ") else (show number) ++ "| " columnLabelSeparator = " " ++ (Prelude.take (15 * 5) $ repeat '-') ++ "\n"- columnLabels = " " ++ (concat $ Prelude.take (15 * 2) . intersperse " " . map ( : []) $ ['A' .. ])+ columnLabels = " " ++ (concat $ Prelude.take (15 * 2) . L.intersperse " " . map ( : []) $ ['A' .. ]) squareToString square = case (tileIfOccupied square) of
src/Wordify/Rules/FormedWord.hs view
@@ -39,7 +39,6 @@ type FormedWord = Seq (Pos, Square) type PlacedSquares = Map Pos Square- data Direction = Horizontal | Vertical deriving Eq {- | Pretty prints the places a given formed word intersects with letters that were already on the board
src/Wordify/Rules/LetterBag.hs view
@@ -3,6 +3,7 @@ tiles, bagFromTiles, makeBagUsingGenerator,+ bagLetters, takeLetters, exchangeLetters, shuffleBag,@@ -18,13 +19,14 @@ import Wordify.Rules.ScrabbleError import Text.ParserCombinators.Parsec import Data.Char+import qualified Data.Map as M import Wordify.Rules.LetterBag.Internal import System.IO import Data.Array.ST import Control.Monad.ST import Data.STRef--+import qualified Data.Set as S+import qualified Data.Maybe as Mb {- | Creates a letter bag from a file where each line contains a space delimited letter character, letter value, and letter distribution.@@ -58,6 +60,21 @@ -} bagFromTiles :: [Tile] -> IO LetterBag bagFromTiles bagTiles = newStdGen >>= return . LetterBag bagTiles (length bagTiles)++{-|+ Maps each letter in the letter bag to the tile for that letter.+ Ignores blank letters. + -}+bagLetters :: LetterBag -> M.Map Char Tile+bagLetters letterBag = + let maybeLetters = Mb.mapMaybe pairIfTileHasLetter $ tiles letterBag+ in M.fromList maybeLetters+ where+ pairIfTileHasLetter :: Tile -> Maybe (Char, Tile)+ pairIfTileHasLetter tile =+ case (tileLetter tile) of+ Just lettr -> Just (lettr, tile)+ _ -> Nothing {- | Takes 'n' numbers from a letter bag, yielding 'Nothing'
src/Wordify/Rules/Pos.hs view
@@ -1,5 +1,6 @@ module Wordify.Rules.Pos (Pos,+ Direction(Horizontal, Vertical), posAt, above, below,@@ -15,6 +16,8 @@ import qualified Data.Map as Map import Wordify.Rules.Pos.Internal import Data.Maybe++ data Direction = Horizontal | Vertical deriving Eq posMin :: Int posMin = 1