-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathU1.hs
More file actions
70 lines (57 loc) · 2.07 KB
/
U1.hs
File metadata and controls
70 lines (57 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
module U1
where
import Data.Char
import Data.List
type BoardField = (Int, Int, Char)
type Board = [BoardField]
validate :: String -> Bool
validate str = validateBoard $ parseBoard str
validateBoard :: Board -> Bool
validateBoard board = not(areThereDuplicateFiels board) &&
if x == o
then True
else if x > o && x - 1 == o
then True
else if o > x && o - 1 == o
then True
else False
where x = countFieldsX(board)
o = countFieldsO(board)
areThereDuplicateFiels :: Board -> Bool
areThereDuplicateFiels board = length board /= length(nubBy areFieldsInSamePos board)
areFieldsInSamePos :: BoardField -> BoardField -> Bool
areFieldsInSamePos (x1, y1, _) (x2, y2, _) = if (x1 == x2 && y1 == y2) then True else False
countFieldsX :: Board -> Int
countFieldsX board = length(filter (\(_, _, x) -> x == 'x') board)
countFieldsO :: Board -> Int
countFieldsO board = length(filter (\(_, _, o) -> o == 'o') board)
parseBoard :: String -> Board
parseBoard str = parseBoardFields $ splitEvery 7 $ removeNonEssentialStuff $ removeL str
removeL :: String -> String
removeL str =
let
parenth1 = takeWhile (/= '(') str
rest1 = drop (length parenth1 + 1) str
parenth2 = takeWhile (/= '(') rest1
rest2 = drop (length parenth2) rest1
--- Remove last parenthese
parenth3 = takeWhile (/= ')') (reverse rest2)
withoutLast = reverse (drop (length parenth3 + 1) (reverse rest2))
in withoutLast
removeNonEssentialStuff :: String -> String
removeNonEssentialStuff str = filter (/= '(') $ filter (/= ')') $ filter (/= '"') $ filter (/= '\\') $ filter (/= ' ') str
-- Split at 7 --
splitEvery _ [] = []
splitEvery n list = first : (splitEvery n rest)
where
(first, rest) = splitAt n list
parseBoardField :: String -> BoardField
parseBoardField str =
let
x = digitToInt $ str !! 2
y = digitToInt $ str !! 4
v = str !! 6
in (x, y, v)
parseBoardFields :: [String] -> Board
parseBoardFields [] = []
parseBoardFields (x:xs) = parseBoardField(x) : parseBoardFields xs