Skip to content
Snippets Groups Projects
Commit 105a58d1 authored by Unai Zalakain's avatar Unai Zalakain
Browse files

Display and parse board coordinates

parent 31d0ddb8
No related branches found
No related tags found
No related merge requests found
......@@ -9,10 +9,9 @@ import Data.List
import System.IO (hFlush, stdout)
import Text.Printf (printf)
import Text.Parsec (parse, (<|>))
import Text.Parsec.Char (string, spaces)
import Text.Parsec.Char (string, spaces, oneOf)
import Text.Parsec.String (Parser)
import Text.Parsec.Combinator (choice)
import Text.ParserCombinators.Parsec.Number (nat)
import qualified Data.Bimap as Bimap
import qualified Data.Map.Strict as Map
import qualified System.Console.ANSI as ANSI
......@@ -36,26 +35,20 @@ instance Displayable (Maybe Piece) where
(Just (Snorkel Red)) -> concat [snorkelColour ANSI.Red, "R", reset]
(Just (Snorkel Yellow)) -> concat [snorkelColour ANSI.Yellow, "Y", reset]
(Just (Snorkel Cyan)) -> concat [snorkelColour ANSI.Cyan, "C", reset]
(Just Stone) -> "O"
(Just Stone) -> "*"
Nothing -> " "
yCoords = ['a'..'z']
xCoords = ['A'..'Z']
instance Displayable Game where
{-._________________.-}
{-| . . . . . . . . |-}
{-| . . . . . . . . |-}
{-| . . . . . . . . |-}
{-| . . . . . . . . |-}
{-| . . . . . . . . |-}
{-| . . . . . . . . |-}
{-| . . . . . . . . |-}
{-| . . . . . . . . |-}
{-'-----------------'-}
display g = header ++ "\n" ++ intercalate "\n" [line y | y <- [0..height-1]] ++ "\n" ++ footer
display g = intercalate "\n" $ [headerCoords, header] ++ [line y | y <- [0..height-1]] ++ [footer]
where (width, height) = g&boardSize
header = "." ++ replicate (width*2-1) '_' ++ "."
footer = "'" ++ replicate (width*2-1) '-' ++ "'"
headerCoords = " " ++ intersperse ' ' (take width xCoords)
header = " ." ++ replicate (width*2-1) '_' ++ "."
footer = " '" ++ replicate (width*2-1) '-' ++ "'"
piece x y = display (B.getPiece g (x, y))
line y = "|" ++ intercalate "." [piece x y | x <- [0..width-1]] ++ "|"
line y = yCoords !! y : " |" ++ intercalate "." [piece x y | x <- [0..width-1]] ++ "|"
playerRepr :: Bimap.Bimap Player String
......@@ -64,12 +57,13 @@ playerRepr = Bimap.fromList [(p, show p) | p <- [Green ..]]
moveParser :: Parser (Maybe Position)
moveParser = do spaces
x <- nat
x <- oneOf xCoords
spaces
y <- nat
y <- oneOf yCoords
spaces
-- TODO: QUIT
return $ Just (x, y)
case (x `elemIndex` xCoords, y `elemIndex` yCoords) of
(Just x, Just y) -> return $ Just (x, y)
_ -> fail "Introduce move in the form of X y"
quitParser :: Parser (Maybe Position)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment