I'm stuck at doing my homework. I have to write a function that has 2 [String]. List string contains 3 chars:
- 1st is Chess piece (e.g. 'K' - King, 'Q' - Queen, 'R' - Rook, 'B' - Bishops, 'N' - Knights, 'P' - Pawns)
- 2nd specifies column ('a' - 'h')
- 3rd is the row number ('1' - '8')
First list of string is for BLACK pieces, the second is for WHITE pieces. Free fields of the boards are represented by '.'. Black chess pieces will be upper cased, white will be lower cased.
Printing 1-8 and a-h on board isn't necessary.
This is required function type:
chess :: [String] -> [String] -> [String]
we have this function for print
pp :: Result -> IO ()
pp x = putStr (concat (map (++"\n") x))
This is IO example:
Prelude> pp( chess["Ke1","Ra1","Rh1","Pa2","Be5"] ["Ke8","Ra8","Rh8","Pa7","Qd8","Bc8","Nb8"])
8rnbqk..r
7p.......
6........
5....B...
4........
3........
2P.......
1R...K..R
abcdefgh
What have I tried: eg.
chess :: [String] -> [String] -> [String]
chess _ [] = []
chess [] _ = []
chess ((x1:x2:x3:_):xs) ((y1:y2:y3:y3s):ys)
| 'a' == x2 && '1' == x3 = [x1] : chess xs ys
| 'a' == y2 && '1' == y3 = [y1] : chess xs ys
| 'b' == x2 && '1' == x3 = [x1] : chess xs ys
| 'c' == x2 && '1' == x3 = [x1] : chess xs ys
| 'd' == x2 && '1' == x3 = [x1] : chess xs ys
| 'e' == x2 && '1' == x3 = [x1] : chess xs ys
| 'e' == x2 && '1' == x3 = [x1] : chess xs ys
| 'g' == x2 && '1' == x3 = [x1] : chess xs ys
| otherwise = ['.'] : chess xs ys
Input was: chess["Ke1","Ra1","Rh1","Pa2","Be1"] ["Kb1","Ra8","Rh8","Pa7","Qd8","Bc8","Na1"] Output was: ["K","R",".",".","B"]
One more..
chess :: [String] -> [String] -> [String]
chess _ [] = []
chess [] _ = []
chess ((x1:x2:x3:_):xs) ((y1:y2:y3:y3s):ys)
| (x2 == 'a' && x3 == ['1']) = [x1] : chess (xs) (ys)
| (x2 == 'a' && x3 == ['2']) = [x1] : chess (xs) (ys)
| (x2 == 'a' && x3 == ['3']) = [x1] : chess (xs) (ys)
| (x2 == 'a' && x3 == ['4']) = [x1] : chess (xs) (ys)
| (x2 == 'a' && x3 == ['5']) = [x1] : chess (xs) (ys)
| (x2 == 'a' && x3 == ['6']) = [x1] : chess (xs) (ys)
| (x2 == 'a' && x3 == ['7']) = [x1] : chess (xs) (ys)
| (x2 == 'a' && x3 == ['8']) = [x1] : chess (xs) (ys)
| otherwise = chess (xs) (ys)
Input: chess["Ke1","Ra1","Rh1","Pa2","Be1"] ["Ke8","Ra8","Rh8","Pa7","Qd8","Bc8","Nb8"] Output: K R R
None of them work as I wanted. I've tried checking one row first, then each column (a-h) - this is right I think, because I need to print something like ["K...Q...", "P......."] - each element is one row. What if I check eg. black, and if there's not black, there still can be white, so I need to check second string for white pieces before printing '.'. Please help, I'm confused. I've tried to code like 4 functions, but they took more than 4 hours. Thank you