quoridor-hs 0.1.1.0 → 0.1.1.2
raw patch · 5 files changed
+48/−45 lines, 5 filesdep ~quoridor-hsPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
Dependency ranges changed: quoridor-hs
API changes (from Hackage documentation)
- Quoridor: coerceTurn :: (Monad m, Functor m) => Turn -> Game m Turn
+ Quoridor: coerceTurn :: (Monad m, Functor m) => Turn -> Game m (Maybe Turn)
- Quoridor: getCurrentValidMoves :: Monad m => Game m [Cell]
+ Quoridor: getCurrentValidMoves :: (Monad m, Functor m) => Game m [Cell]
Files
- quoridor-hs.cabal +3/−6
- src/Quoridor.hs +23/−21
- src/Quoridor/Cmdline/Parse.hs +8/−16
- src/Quoridor/Cmdline/Render.hs +1/−1
- src/Quoridor/Helpers.hs +13/−1
quoridor-hs.cabal view
@@ -1,5 +1,5 @@ name: quoridor-hs-version: 0.1.1.0+version: 0.1.1.2 synopsis: A Quoridor implementation in Haskell homepage: https://github.com/talw/quoridor-hs license: BSD3@@ -58,15 +58,12 @@ -- hs-source-dirs: default-language: Haskell2010 GHC-Options: -Wall- --Werror- -fno-warn-unused-do-bind- -fno-warn-type-defaults executable quoridor-exec hs-source-dirs: quoridor-exec main-is: Main.hs build-depends: base >=4.7 && <4.8,- quoridor-hs ==0.1.1.0+ quoridor-hs ==0.1.1.2 default-language: Haskell2010 GHC-Options: -Wall -threaded@@ -76,7 +73,7 @@ main-is: Tests.hs build-depends: base >=4.7 && <4.8, HUnit ==1.2.*,- quoridor-hs ==0.1.1.0,+ quoridor-hs ==0.1.1.2, mtl >=2.1 && <2.2 default-language: Haskell2010 type: exitcode-stdio-1.0
src/Quoridor.hs view
@@ -9,12 +9,12 @@ runReaderT) import Control.Monad.State (MonadIO, MonadState, MonadTrans, StateT, evalState, get, gets, lift, modify, put,- runStateT, void)+ runStateT, void, liftM3) import Data.List (find, sort) import qualified Data.Map as M import qualified Data.Set as S -import Quoridor.Helpers (andP, rotateList, unsafeLookup)+import Quoridor.Helpers -- | A tile on the board. -- Direction of X and Y axis are right and down respectively.@@ -146,7 +146,7 @@ distance (y,x) (y',x') = abs (y' - y) + abs (x' - x) isAdj :: Cell -> Cell -> Bool-isAdj = ((1 ==) .) . distance+isAdj = (1 ==) .: distance -- | Returns adjacent cells that are within the ranger of the board getAdj :: Int -> Cell -> [Cell]@@ -166,7 +166,7 @@ -- | Equivalent to, given cells a and b (a,b) -- is the space between them open for movement? isHalfGateSpaceClear :: HalfGate -> HalfGates -> Bool-isHalfGateSpaceClear = (not .) . S.member . align+isHalfGateSpaceClear = not .: S.member . align isGateSpaceClear :: Gate -> HalfGates -> Bool isGateSpaceClear (h1, h2) =@@ -207,11 +207,11 @@ -- that it is a shortcut of, using the integral index that -- is the index of the shortcut character in the list of -- 'validMovesChars'-coerceTurn :: (Monad m, Functor m) => Turn -> Game m Turn+coerceTurn :: (Monad m, Functor m) => Turn -> Game m (Maybe Turn) coerceTurn (ShortCutMove i) = do vmSorted <- sort <$> getCurrentValidMoves- return $ Move $ vmSorted !! i-coerceTurn t = return t+ return $ Move <$> vmSorted `safeAt` i+coerceTurn t = return $ Just t -- | Gets a list of possible cells which -- the current player can move to.@@ -314,19 +314,21 @@ -- that the GameState did not change. makeTurn :: (Monad m, Functor m) => Turn -> Game m (Maybe Turn) makeTurn t = do- t' <- coerceTurn t- wasValid <- isValidTurn t'- if wasValid- then do actTurn t'- checkAndSetWinner- changeCurrPlayer- return $ Just t'- else return Nothing+ mt <- coerceTurn t+ case mt of+ Nothing -> return Nothing+ Just t' -> do+ wasValid <- isValidTurn t'+ if wasValid+ then do actTurn t'+ checkAndSetWinner+ changeCurrPlayer+ return $ Just t'+ else return Nothing -- | A Game monad wrapper for the unmonadic 'getValidMoves'-getCurrentValidMoves :: Monad m => Game m [Cell]-getCurrentValidMoves = do- bs <- reader boardSize- gs <- get- let cell = pos $ currP gs- return $ getValidMoves cell bs gs+getCurrentValidMoves :: (Monad m, Functor m) => Game m [Cell]+getCurrentValidMoves = liftM3 getValidMoves posCurrP+ (reader boardSize)+ get+ where posCurrP = gets $ pos . currP
src/Quoridor/Cmdline/Parse.hs view
@@ -3,7 +3,7 @@ parseMessage ) where -import Control.Applicative (pure)+import Control.Applicative (pure, (<*), (<*>), (*>)) import Data.Char (toUpper) import Data.Functor ((<$>)) import Data.List (elemIndex)@@ -16,6 +16,7 @@ import Quoridor import Quoridor.Cmdline.Messages (validMovesChars) import Quoridor.Cmdline.Network.Common (Message (TurnMsg, ChatMsg))+import Quoridor.Helpers type Parse = Parsec String () @@ -32,10 +33,7 @@ asToken $ manyTill anyChar eof pTurn :: Parse Turn-pTurn = do- res <- pMove <|> pShortCutMove <|> pPutGate- eof- return res+pTurn = (pMove <|> pShortCutMove <|> pPutGate) <* eof -- m y x pMove :: Parse Turn@@ -46,23 +44,17 @@ -- one of 'validMovesChars', translated -- into their index. pShortCutMove :: Parse Turn-pShortCutMove = do- c <- oneOf validMovesChars- return $ ShortCutMove $ translate c- where translate c = fromJust $ elemIndex c validMovesChars+pShortCutMove = ShortCutMove . translate <$> oneOf validMovesChars+ where translate c = fromJust $ elemIndex c validMovesChars -- g y x h|v pPutGate :: Parse Turn pPutGate = do char 'g'- c <- pCell- PutGate . gateUpperLeft c <$> pDirection+ PutGate .: gateUpperLeft <$> pCell <*> pDirection pCell :: Parse Cell-pCell = do- y <- pInt- x <- pInt- return (y,x)+pCell = (,) <$> pInt <*> pInt pDirection :: Parse Direction pDirection = cToDirection <$> asToken (oneOf "hv")@@ -72,7 +64,7 @@ pInt = (read :: String -> Int) <$> asToken (many1 digit) asToken :: Parse a -> Parse a-asToken p = spaces >> p+asToken p = spaces *> p parseShowErrMsgs :: Parse a -> String -> Either String a parseShowErrMsgs p str = func $ parse p "" str
src/Quoridor/Cmdline/Render.hs view
@@ -66,7 +66,7 @@ -- to set the terminal color. putColoredStr can be used to apply -- those actions automatically runRenderColor :: GameState -> GameConfig -> ValidMoves -> (String, [CA.Color])-runRenderColor = ((addColor .) .) . runRender+runRenderColor x y z = addColor $ runRender x y z -- | Given an input such as the output of runRenderColor, writes the -- game board along with some basic info, to the screen, applying
src/Quoridor/Helpers.hs view
@@ -15,4 +15,16 @@ rotateList (x:xs) = xs ++ [x] unsafeLookup :: Ord k => k -> M.Map k a -> a-unsafeLookup = (fromJust .) . M.lookup+unsafeLookup = fromJust .: M.lookup++infixr 9 .:+(.:) :: (c -> d) -> (a -> b -> c) -> a -> b -> d+f .: g = \x y -> f $ g x y++safeAt :: [a] -> Int -> Maybe a+safeAt list targetInd = go list targetInd 0+ where+ go [] _ _ = Nothing+ go (x:xs) i n+ | i == n = Just x+ | otherwise = go xs i (n+1)