Implement basic board types/functions

This commit is contained in:
Sidharth Kulkarni 2026-05-02 18:00:08 -07:00
parent 2b64fe2a0b
commit 0fe8205ed9
Signed by: skulk
SSH key fingerprint: SHA256:Jby+S9d1WmwqnXIrngHgccYNHz+cYquxN1zm3ym3Kbg
8 changed files with 140 additions and 35 deletions

37
src/Types/GameMode.hs Normal file
View file

@ -0,0 +1,37 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeFamilies #-}
module Types.GameMode where
import Data.Bifunctor (Bifunctor (first))
import Data.Kind (Type)
import Display
import System.Random.Stateful (Random (randomR), RandomGen)
{- | The type of game that is being played.
This typeclass carries information about how to check whether a set
of elements satisfies the condition for a valid move. For example,
in the 'SumTo n' mode, a valid move is a set of elements (Ints)
that sums up to 'n'.
-}
class (Display (Tile t), Show (Tile t), Eq (Tile t), Eq t, Show t) => GameMode t where
data Tile t :: Type
check :: t -> [Tile t] -> Bool
gen :: (RandomGen g) => t -> g -> (Tile t, g)
data SumTo = MkSumTo Int
deriving (Show, Eq)
instance GameMode SumTo where
data Tile SumTo = IntTile Int deriving (Eq, Show)
check (MkSumTo n) [] = n == 0
check (MkSumTo n) lst = n == sum (map toInt lst)
where
toInt (IntTile a) = a
gen (MkSumTo n) = first IntTile . randomR (1, n)
instance Display (Tile SumTo) where
display (IntTile x) = display x