I'm making a simple Tic-Tac-Toe game to learn Haskell. Each cell in the board has either a player value (X or O) or a positional value if the square is empty (letters A-I, such that:
A|B|C
D|E|F
G|H|I
)
To start, I've created a simple function that takes an empty board (which looks like the one above) and a String (a letter "A" through "I"), and returns a board where the letter entered is replaced with an "X". So if I were to call newBoard blankBoard "C"
, I would expect this to return:
A|B|X
D|E|F
G|H|I
If I want to keep the Square
data type as it is, how could I make this code work:
data Square = A | B | C | D | E | F | G | H | I | X | O deriving (Read)
instance Show Square where
show A = "A"
show B = "B"
show C = "C"
show D = "D"
show E = "E"
show F = "F"
show G = "G"
show H = "H"
show I = "I"
type Board = [[Square]]
blankBoard :: Board
blankBoard = [[A,B,C],[D,E,F],[G,H,I]]
newBoard :: Board -> String -> Board
newBoard board letter = [[ if show sq == letter then X else sq | sq <- row ]| row <- board]
I'm getting this error, and not sure why:
[[*** Exception: myfunctions.hs:(386,3)-(394,14): Non-exhaustive patterns in function show
deriving Show
for your Square? – JHobern