<commit_before>-- / What is in the hand during a round
module App.Hand where

import Data.Maybe
import App.Board as Board
import App.Board (class HasBoard)
import Data.Function (apply)
import Data.List (List(..), (:), concat, foldr)
import Data.Tuple (Tuple(..))
import Data.Unfoldable (class Unfoldable, unfoldr)
import Prelude ((>>>), (<<<), ($), map)

data State = Hand
  { player :: Board.Player
  , seeds :: Board.Cell
  , pitRef :: Board.PitRef
  , board :: Board.State
  }

instance hasBoardHand :: HasBoard State where
  getBoard (Hand h) = h.board

init :: Board.Player -> Board.PitRef -> Board.State -> State
init player pitRef board = Hand
  { player: player
  , seeds: 0
  , pitRef: pitRef
  , board: board
  }

opponent :: State -> Board.Player
opponent (Hand { player }) = Board.opponentOf player

unfoldTurns :: State -> List (State -> State)
unfoldTurns = concat <<< unfoldr' nextTurns

nextTurns :: State -> Tuple (List (State -> State)) (Maybe State)
nextTurns (Hand state@{ player, seeds, pitRef, board }) =
  go seeds
     (Board.lookup pitRef board)
     (Board.lookup (Board.nextRef pitRef) board)
     (Board.lookup ((Board.nextRef <<< Board.nextRef) pitRef) board)
     state
    where continue xs = Tuple xs (Just $ applyTurns xs $ Hand state)
          end xs = Tuple xs Nothing
          go 0 0 0 _ state' =
            end $ Nil
          go 0 0 s _ state' =
            end $ advance : capture : Nil
          go _ _ _ _ state' =
            end $ Nil -- TODO: signal not reachable state

applyTurns :: List (State -> State) -> State -> State
applyTurns turns s = foldr apply s turns

advance :: State -> State
advance state =
  state -- TODO

capture :: State -> State
capture (Hand state@{ player, seeds, pitRef, board }) =
  Hand $ state { board = board' }
  where board' = ( Board.clear pitRef >>> Board.store player seeds' ) board
        seeds'  = Board.lookup pitRef board
-- | A version of unfoldr that allows a elements in end cause.
unfoldr' :: forall a b t. Unfoldable t
        => (b -> Tuple a (Maybe b)) -> b -> t a
unfoldr' f = unfoldr (map f) <<< Just
<commit_msg>newtype is suitable here<commit_after>-- / What is in the hand during a round
module App.Hand where

import Data.Maybe
import App.Board as Board
import App.Board (class HasBoard)
import Data.Function (apply)
import Data.List (List(..), (:), concat, foldr)
import Data.Tuple (Tuple(..))
import Data.Unfoldable (class Unfoldable, unfoldr)
import Prelude ((>>>), (<<<), ($), map)

newtype State = State
  { player :: Board.Player
  , seeds :: Board.Cell
  , pitRef :: Board.PitRef
  , board :: Board.State
  }

instance hasBoardHand :: HasBoard State where
  getBoard (State h) = h.board

init :: Board.Player -> Board.PitRef -> Board.State -> State
init player pitRef board = State
  { player: player
  , seeds: 0
  , pitRef: pitRef
  , board: board
  }

opponent :: State -> Board.Player
opponent (State { player }) = Board.opponentOf player

unfoldTurns :: State -> List (State -> State)
unfoldTurns = concat <<< unfoldr' nextTurns

nextTurns :: State -> Tuple (List (State -> State)) (Maybe State)
nextTurns (State state@{ player, seeds, pitRef, board }) =
  go seeds
     (Board.lookup pitRef board)
     (Board.lookup (Board.nextRef pitRef) board)
     (Board.lookup ((Board.nextRef <<< Board.nextRef) pitRef) board)
     state
    where continue xs = Tuple xs (Just $ applyTurns xs $ State state)
          end xs = Tuple xs Nothing
          go 0 0 0 _ state' =
            end $ Nil
          go 0 0 s _ state' =
            end $ advance : capture : Nil
          go _ _ _ _ state' =
            end $ Nil -- TODO: signal not reachable state

applyTurns :: List (State -> State) -> State -> State
applyTurns turns s = foldr apply s turns

advance :: State -> State
advance state =
  state -- TODO

capture :: State -> State
capture (State state@{ player, seeds, pitRef, board }) =
  State $ state { board = board' }
  where board' = ( Board.clear pitRef >>> Board.store player seeds' ) board
        seeds'  = Board.lookup pitRef board
-- | A version of unfoldr that allows a elements in end cause.
unfoldr' :: forall a b t. Unfoldable t
        => (b -> Tuple a (Maybe b)) -> b -> t a
unfoldr' f = unfoldr (map f) <<< Just

{'fraction_non_alphanumeric': 0.12452830188679245, 'fraction_numerical': 0.002830188679245283, 'mean_word_length': 2.827617328519856, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4388445', 'n_tokens_mistral': 1354, 'n_tokens_neox': 1317, 'n_words': 491}