finished refactor, compiles and runs as before now

This commit is contained in:
Shaun Kerr 2019-05-22 13:06:18 +12:00
parent 7988712983
commit 4a81233848
9 changed files with 85 additions and 94 deletions

View File

@ -7,9 +7,9 @@ tA's Rules Engine for the Neon Genesis Evangelion Trading Card Game.
Currently only runs in GHCI Currently only runs in GHCI
```haskell ```haskell
:load Connect.hs Print.hs Init.hs :load Leads/Groups.hs ComBoard/Printer.hs ComBoard/Init.hs
import Print import ComBoard.Printer
import Init import ComBoard.Init
``` ```
To print the representation of the initial board state To print the representation of the initial board state
@ -17,32 +17,37 @@ To print the representation of the initial board state
printState initBS printState initBS
``` ```
`conHurt` and `conAttract` are currently the only implemented mechanics `hurt` and `attract` are currently the only implemented main mechanics
```haskell ```haskell
printState $ conHurt Asuka Gendo $ conAttract Shinji Asuka initBS printState $ (attract Ritsuko Misato)
. (hurt Shinji Gendo)
. (attract Asuka Shinji)
. (hurt Rei Ritsuko)
$ initBS
``` ```
returns: returns:
```plaintext ```plaintext
== BOARD STATE == == BOARD STATE ==
- Groups - - Groups -
A ~ [ Shinji, Asuka ] <-> BCDE A ~ [ Ritsuko, Misato ] <-> BD
B ~ [ Rei ] <-> AE B ~ [ Shinji, Asuka ] <-> ACD
C ~ [ Misato ] <-> AD C ~ [ Rei ] <-> BD
D ~ [ Ritsuko ] <-> ACE D ~ [ Gendo ] <-> ABC
E ~ [ Gendo ] <-> ABD
- Downed - - Downed -
~ Gendo ~ Gendo
~ Ritsuko
- GameLog - - GameLog -
Shinji attracted Asuka, Asuka's group joined Shinji's group Rei hurts Ritsuko, Ritsuko is downed!
Asuka hurt Gendo, Gendo is downed Shinji attracts Asuka, both groups are joined
Shinji hurts Gendo, Gendo is downed!
Ritsuko attracts Misato, both groups are joined
================= =================
``` ```
### Dependencies ### Dependencies
`Data.List` and `Data.Either` `Data.List` and `Maybe (Data.Either)`
## Authors ## Authors

View File

@ -33,22 +33,22 @@ isDowned c bs = c `elem` d
-- Return true if character is not grouped -- Return true if character is not grouped
isAlone :: MainCharacter -> BoardState -> Bool isAlone :: MainCharacter -> BoardState -> Bool
isAlone c bs = curFrens c g == [] isAlone c bs = curFrens c bs == []
where g = (ntGroups . bsNeoTokyo) bs where g = (ntGroups . bsNeoTokyo) bs
-- Return true if X is adjacent to Y -- Return true if X is adjacent to Y
isAdjacent :: MainCharacter -> MainCharacter -> BoardState -> Bool isAdjacent :: MainCharacter -> MainCharacter -> BoardState -> Bool
isAdjacent x y bs = y `elem` (getAdj x g) isAdjacent x y bs = y `elem` (getAdj x bs)
where g = (ntGroups . bsNeoTokyo) bs where g = (ntGroups . bsNeoTokyo) bs
-- All characters a X is adjacent to -- All characters a X is adjacent to
getAdj :: MainCharacter -> BoardState -> [MainCharacter] getAdj :: MainCharacter -> BoardState -> [MainCharacter]
getAdj x bs = curFrens x g ++ (foldr (++) [] (adjGroups x g)) getAdj x bs = curFrens x bs ++ (foldr (++) [] (adjGroups x bs))
where g = (ntGroups . bsNeoTokyo) bs where g = (ntGroups . bsNeoTokyo) bs
-- The characters currently grouped with x -- The characters currently grouped with x
curFrens :: MainCharacter -> BoardState -> [MainCharacter] curFrens :: MainCharacter -> BoardState -> [MainCharacter]
curFrens x bs = filter (/= x) $ charsGroup x g curFrens x bs = filter (/= x) $ charsGroup x bs
where g = (ntGroups . bsNeoTokyo) bs where g = (ntGroups . bsNeoTokyo) bs
-- The group that character x belongs to -- The group that character x belongs to
@ -63,13 +63,12 @@ otherGroups x bs = filter (\y -> not $ x `elem` y) g
-- The groups a character is adaject to -- The groups a character is adaject to
adjGroups :: MainCharacter -> BoardState -> Groups adjGroups :: MainCharacter -> BoardState -> Groups
adjGroups x bs = filter (\y -> any (\z -> z `elem` adjFrens) y) (otherGroups x g) adjGroups x bs = filter (\y -> any (\z -> z `elem` adjFrens) y) (otherGroups x bs)
where where
adjFrens = uniq $ foldr (++) [] (map defFrens $ charsGroup x g) adjFrens = uniq $ foldr (++) [] (map defFrens $ charsGroup x bs)
g = (ntGroups . bsNeoTokyo) bs g = (ntGroups . bsNeoTokyo) bs
-- The groups a group is adjacent to -- The groups a group is adjacent to
neighborGroups :: Group -> BoardState -> Groups neighborGroups :: Group -> BoardState -> Groups
neighborGroups g bs = uniq . concat $ map (\x -> adjGroups x gs) g neighborGroups g bs = uniq . concat $ map (\x -> adjGroups x bs) g
where g = (ntGroups . bsNeoTokyo) bs

View File

@ -6,7 +6,7 @@ module Cogs.Players where
-- ==== -- ====
import ComBoard.Types import ComBoard.Types
import Util import Toolbox.Utils
-- Returns whether CardSym is in PlayerId's hand -- Returns whether CardSym is in PlayerId's hand
isInHand :: CardSym -> PlayerId -> BoardState -> Bool isInHand :: CardSym -> PlayerId -> BoardState -> Bool

View File

@ -4,8 +4,8 @@ module ComBoard.Printer where
-- Prints the state of the ComBoard -- Prints the state of the ComBoard
-- ==== -- ====
import Types import ComBoard.Types
import Groups import Cogs.Groups
import Data.List import Data.List
printState :: BoardState -> IO () printState :: BoardState -> IO ()
@ -16,7 +16,7 @@ printState bs = do
map map
(\(gid, x) -> [gid] ++ " ~ [ " ++ concatGroup x ++ " ] <-> " (\(gid, x) -> [gid] ++ " ~ [ " ++ concatGroup x ++ " ] <-> "
++ ++
(map fst $ filter (\(_,y) -> y `elem` (neighborGroups x groups)) zipGroups) (map fst $ filter (\(_,y) -> y `elem` (neighborGroups x bs)) zipGroups)
) zipGroups ) zipGroups
putStrLn " - Downed -" putStrLn " - Downed -"
mapM_ putStrLn $ map mapM_ putStrLn $ map
@ -28,7 +28,7 @@ printState bs = do
Left m -> m Left m -> m
Right e -> "<ERROR> : " ++ e) Right e -> "<ERROR> : " ++ e)
(bsGameLog bs) (bsGameLog bs)
putStrLn " =================" putStrLn "================="
where where
groups = (ntGroups . bsNeoTokyo) bs groups = (ntGroups . bsNeoTokyo) bs
concatGroup x = concat $ intersperse ", " (map show x) concatGroup x = concat $ intersperse ", " (map show x)

View File

@ -11,7 +11,7 @@ import Cogs.Players
-- Sets the groups in NeoTokyo -- Sets the groups in NeoTokyo
setGroups :: Groups -> BoardState -> BoardState setGroups :: Groups -> BoardState -> BoardState
setGroups gs bs = bs setGroups gs bs = bs
{ bsNeoTokyo = (bsNeoTokyo bs) { nsGroups = gs } } { bsNeoTokyo = (bsNeoTokyo bs) { ntGroups = gs } }
-- Sets the downed in NeoTokyo -- Sets the downed in NeoTokyo
setDowned :: Downed -> BoardState -> BoardState setDowned :: Downed -> BoardState -> BoardState
@ -24,8 +24,17 @@ setPlayerLib pl bs = bs { bsPlayerLib = pl }
-- Sets a players state -- Sets a players state
setPlayer :: PlayerId -> PlayerState -> BoardState -> BoardState setPlayer :: PlayerId -> PlayerState -> BoardState -> BoardState
setPlayer pi ps bs = setPlayerLib $ map setPlayer pi ps bs = setPlayerLib (map
(\(pid,state) -> if (pid == pi) then ps else state) pl (\(pid,state) -> if (pid == pi) then (pid,ps) else (pid,state)) pl) bs
where where
pl = (bsPlayerLib bs) pl = (bsPlayerLib bs)
-- Adds a message to the game log
addGameMessage :: String -> BoardState -> BoardState
addGameMessage m bs = bs
{ bsGameLog = (bsGameLog bs) ++ [ Left m ] }
-- Adds an error to the game log
addGameError :: String -> BoardState -> BoardState
addGameError e bs = bs
{ bsGameLog = (bsGameLog bs) ++ [ Right e ] }

View File

@ -1,57 +0,0 @@
module Leads.Groups where
-- == Leads - Groups ==
-- Functions of type :: Arg1... -> BoardState -> BoardState
-- Changes state of groups
import ComBoard.Types
import Cogs.Groups
-- X hurts Y
conHurt :: MainCharacter -> MainCharacter -> BoardState -> BoardState
conHurt charX charY oldBoardState
| not $ isAdjacent charX charY oldGroups = oldBoardState
{ bsGameLog = (bsGameLog oldBoardState)
++ [ Right (show charX ++ " is not adjacent to " ++ show charY ++ ", could not hurt") ]
}
| isAlone charY oldGroups = oldBoardState
{ bsGameLog = (bsGameLog oldBoardState)
++ [ Left (show charX ++ " hurt " ++ show charY ++ ", " ++ show charY ++ " is downed") ]
, bsNeoTokyo = (bsNeoTokyo oldBoardState)
{ ntPutCards = filter (\x -> ((fst x) /= Left charX)) oldPut
, ntDowned = downChar charY oldDowned
}
}
| otherwise = oldBoardState
{ bsGameLog = (bsGameLog oldBoardState)
++ [ Left (show charX ++ " hurt " ++ show charY ++ ", " ++ show charY ++ " left their group") ]
, bsNeoTokyo = (bsNeoTokyo oldBoardState)
{ ntGroups = removeFromGroup charY oldGroups }
}
where
oldDowned = (ntDowned . bsNeoTokyo) oldBoardState
oldGroups = (ntGroups . bsNeoTokyo) oldBoardState
oldPut = (ntPutCards . bsNeoTokyo) oldBoardState
-- X attracts Y
conAttract :: MainCharacter -> MainCharacter -> BoardState -> BoardState
conAttract charX charY oldBoardState
| not $ isAdjacent charX charY oldGroups = oldBoardState
{ bsGameLog = (bsGameLog oldBoardState)
++ [ Right (show charX ++ " is not adjacent to " ++ show charY ++ ", could not attract") ]
}
| isDowned charX oldDowned = oldBoardState
{ bsGameLog = (bsGameLog oldBoardState)
++ [ Left (show charX ++ " attracted " ++ show charY ++ ", " ++ show charY ++ " is ready") ]
, bsNeoTokyo = (bsNeoTokyo oldBoardState)
{ ntDowned = readyChar charY oldDowned }
}
| otherwise = oldBoardState
{ bsGameLog = (bsGameLog oldBoardState)
++ [ Left (show charX ++ " attracted " ++ show charY ++ ", " ++ show charY ++ "'s group joined " ++ show charX ++ "'s group") ]
, bsNeoTokyo = (bsNeoTokyo oldBoardState)
{ ntGroups = joinGroups charX charY oldGroups }
}
where
oldDowned = (ntDowned . bsNeoTokyo) oldBoardState
oldGroups = (ntGroups . bsNeoTokyo) oldBoardState

View File

@ -5,26 +5,28 @@ module Leads.Groups where
-- Changes state of groups -- Changes state of groups
-- ==== -- ====
import Cogs.Groups
import Leads.ComBoard import Leads.ComBoard
import ComBoard.Types import ComBoard.Types
import Toolbox.Utils import Toolbox.Utils
-- Return the groups with C removed from its current group -- Return the groups with C removed from its current group
removeFromGroup :: MainCharacter -> BoardState -> BoardState removeFromGroup :: MainCharacter -> BoardState -> BoardState
removeFromGroup c bs = setGroups ( [c] : (groupMinusChar c) : (otherGroups c g) ) bs removeFromGroup c bs = setGroups ( [c] : (groupMinusChar c) : (otherGroups c bs) ) bs
where where
groupMinusChar x = curFrens x g groupMinusChar x = curFrens x bs
g = (ntGroups . bsNeoTokyo) bs g = (ntGroups . bsNeoTokyo) bs
-- Return the groups with X grouped with Y -- Return the groups with X grouped with Y
joinGroups :: MainCharacter -> MainCharacter -> BoardState -> BoardState joinGroups :: MainCharacter -> MainCharacter -> BoardState -> BoardState
joinGroups x y bs joinGroups x y bs
| x `elem` curFrens y g = bs | x `elem` curFrens y bs = bs
| otherwise = setGroups ((groupX ++ groupY) : (otherGroups x . otherGroups y) g) bs | otherwise = setGroups ((groupX ++ groupY) : ogs) bs
where where
g = (ntGroups . bsNeoTokyo) bs g = (ntGroups . bsNeoTokyo) bs
groupX = charsGroup x g groupX = charsGroup x bs
groupY = charsGroup y g groupY = charsGroup y bs
ogs = otherGroups y (setGroups (otherGroups x bs) bs)
-- Return the Downed with the new character -- Return the Downed with the new character
downChar :: MainCharacter -> BoardState -> BoardState downChar :: MainCharacter -> BoardState -> BoardState
@ -39,3 +41,25 @@ readyChar :: MainCharacter -> BoardState -> BoardState
readyChar c bs = setDowned (filter (/= c) d) bs readyChar c bs = setDowned (filter (/= c) d) bs
where where
d = (ntDowned . bsNeoTokyo) bs d = (ntDowned . bsNeoTokyo) bs
-- Have character X attract Y
attract :: MainCharacter -> MainCharacter -> BoardState -> BoardState
attract charX charY bs
| charY `elem` d = (addGameMessage
(show charX ++ " attracts " ++ show charY ++ ", " ++ show charY ++ " is readied!"))
. (readyChar charY) $ bs
| otherwise = (addGameMessage
(show charX ++ " attracts " ++ show charY ++ ", both groups are joined"))
. (joinGroups charX charY) $ bs
where
d = (ntDowned . bsNeoTokyo) bs
-- Have character X hurt Y
hurt :: MainCharacter -> MainCharacter -> BoardState -> BoardState
hurt charX charY bs
| isAlone charY bs = (addGameMessage
(show charX ++ " hurts " ++ show charY ++ ", " ++ show charY ++ " is downed!"))
. (downChar charY) {- . (removePut charY) -} $ bs
| otherwise = (addGameMessage
(show charX ++ " hurts " ++ show charY ++ ", " ++ show charY ++ " is seperated"))
. (removeFromGroup charY) $ bs

11
src/Leads/Lines.hs Normal file
View File

@ -0,0 +1,11 @@
module Leads.Lines where
-- == Leads - Lines ==
-- Functions of type :: Arg1... -> BoardState -> BoardState
-- used to change state of line cards
-- ====
import ComBoard.Types
import Toolbox.Utils