Snusqx
Snusqx

Reputation: 45

Haskel how can I turn this list of string into a list of lists?

The string looks like this:

",w84,w41,w56,w170,w56,w41,w84,/,,w24,w40,w17,w40,w48,,/ ,,,w16,w16,w16,,,/,,,,,,,,/,,,,,,,,/,,,,,,,,/,,,b1,b1,b1,,,/ ,,b3,b130,b17,b130,b129,,/,b69,b146,b131,b170,b131,b146,b69,"

But it should look like this

[[Empty,Piece White 84,Piece White 41,Piece White 56,Piece White 170,Piece White 56,Piece White 41,Piece White 84,Empty],[Empty,Empty,Piece White 24,Piece White 40,Piece White 17,Piece White 40,Piece White 48,Empty,Empty],[Empty,Empty,Empty,Piece White 16,Piece White 16,Piece White 16,Empty,Empty,Empty],[Empty,Empty,Empty,Empty,Empty,Empty,Empty,Empty,Empty],[Empty,Empty,Empty,Empty,Empty,Empty,Empty,Empty,Empty],[Empty,Empty,Empty,Empty,Empty,Empty,Empty,Empty,Empty],[Empty,Empty,Empty,Piece Black 1,Piece Black 1,Piece Black 1,Empty,Empty,Empty],[Empty,Empty,Piece Black 3,Piece Black 130,Piece Black 17,Piece Black 130,Piece Black 129,Empty,Empty],[Empty,Piece Black 69,Piece Black 146,Piece Black 131,Piece Black 170,Piece Black 131,Piece Black 146,Piece Black 69,Empty]]

The list my code creates looks like this:

[["Empty,Piece White 84,Piece White 41,Piece White 56,Piece White 170,Piece White 56,Piece White 41,Piece White 84,Empty"], ["Empty,Empty,Piece White 24,Piece White 40,Piece White 17,Piece White 40,Piece White 48,Empty,Empty"], ["Empty,Empty,Empty,Piece White 16,Piece White 16,Piece White 16,Empty,Empty,Empty"], ["Empty,Empty,Empty,Empty,Empty,Empty,Empty,Empty,Empty"]


data Player = Black | White deriving Show
data Cell = Piece Player Int | Empty deriving Show
data Pos = Pos { col :: Char, row :: Int } deriving Show
type Board = [[Cell]]

I have these data types.

I am almost done with this task all I need is to get rid of the quotation marks.

This is my code so far:

buildBoard x =  rec(help3(wop(helper (replaceO x))))


wop (x:xs) = splitOn "/" (x:xs)

help3 (x:xs) = map (\x -> [x])(x:xs)

rec (x:xs) = map(\x -> [recH(x)])(x:xs)

recH (x:xs) = checkComma(x)

helper (x:y:xs) 
            |x == ',' && y == ',' = x:'E':'m':'p':'t':'y':helper(y:xs)
            |otherwise = x:helper (y:xs)
helper [] = []
helper [x] = [x]

checkComma (x:xs) = if head (x:xs) == ',' then checkComma('E':'m':'p':'t':'y':',':xs) else if last (x:xs) == ',' then reverse(turnAr(reverse(x:xs))) else (x:xs)

turnAr (x:xs) = 'y':'t':'p':'m':'E':',':xs

replaceO [] = []
replaceO (x:xs) = 
     if x == 'w' 
      then 'P':'i':'e':'c':'e':' ':'W':'h':'i':'t':'e':' ': replaceO xs 
     else if x == 'b'
      then 'P':'i':'e':'c':'e':' ':'B':'l':'a':'c':'k':' ': replaceO xs
     else if x == 'E'
      then 'E':'m':'p':'t':'y':' ': replaceO xs
     else x : replaceO xs
     
    

Upvotes: 1

Views: 139

Answers (1)

Noughtmare
Noughtmare

Reputation: 10695

Just getting rid of the quotation marks is not something you can easily do.

I think you'll have to make some significant changes to your code. I'd recommend doing the splitOn "/" first and then further splitOn "," which yields a list of lists of strings where each of the strings represents a cell.

Then you can pretty easily write a function parseCell :: String -> Cell to parse those inner cells. This function will be a bit like your replaceO function but it should also handle all empty cells and actually parsing the integers (you can use the read function for that).

Upvotes: 1

Related Questions