As an assignment we were given these data types, and I need to make a function that get a row ([Cell]) and returns Player X, Player O or Nothing depending on the row.
module TicTacToe where
import Data.Maybe
data Player = X | O deriving Eq
data Cell = E | P Player deriving Eq
data State = Running | GameOver (Maybe Player) deriving Eq
type Size = Int
type Board = [[Cell]]
type Game = (Size, Board, Player, State)
whoWonOnRow :: [Cell] -> Maybe Player
whoWonOnRow c
| head c == E && all (== head c) c = Nothing
| (head c == P X || head c == P O) && all (== head c) c = Just (head c)
| otherwise = Nothing
My understanding is that the data type Cell can have 2 values: E or P which is a Player data type. Now I don't understand what the problem is because head c will be either E or a Player, so I compare it to E and and a P Player X and Y.
If the first Cell is empty (E) than we know that nobody won and, we can return Nothing. If it's a Player than we check that every other Cell is also that players. If so X or O wins, otherwise Nothing.
head, tail, !!, length, ==when it makes sense. Compare your attempt with Willem's below. Recall thathead,tail,!!can make your program crash if the list is empty, while (exhaustive) pattern matching is safe.