1
Fork 0

fsharp(ygosim): feat: made sure player states are resolved

Signed-off-by: prescientmoon <git@moonythm.dev>
This commit is contained in:
Matei Adriel 2019-12-12 16:34:21 +02:00 committed by prescientmoon
parent b3f374162e
commit d29343da17
Signed by: prescientmoon
SSH key fingerprint: SHA256:UUF9JT2s8Xfyv76b8ZuVL7XrmimH4o49p4b+iexbVH4
2 changed files with 53 additions and 11 deletions

View file

@ -33,7 +33,7 @@ module Player =
type PlayerState = type PlayerState =
| InGame | InGame
| Won | Won of reason: string
| Lost of reason: string | Lost of reason: string
type Player<'s> = type Player<'s> =
@ -104,6 +104,9 @@ module Board =
let inline currentPlayerDeck f board = (currentPlayer << Player.deck) f board let inline currentPlayerDeck f board = (currentPlayer << Player.deck) f board
let inline currentPlayerHand f board = (currentPlayer << Player.hand) f board let inline currentPlayerHand f board = (currentPlayer << Player.hand) f board
let inline firstPlayer f board = (players << _1) f board
let inline secondPlayer f board = (players << _2) f board
type Card = Card.Card<Board> type Card = Card.Card<Board>
type CardInstance = Card.CardInstance<Board> type CardInstance = Card.CardInstance<Board>
@ -115,7 +118,7 @@ module Board =
type Action = Effect.Action<Board> type Action = Effect.Action<Board>
let emptyBoard = let emptyBoard =
{ players = (Player.initialPlayer 8000 0, Player.initialPlayer 8000 1) { players = (initialPlayer 8000 0, initialPlayer 8000 1)
moment = 0, Draw } moment = 0, Draw }
module Game = module Game =
@ -126,7 +129,7 @@ module Game =
type Log = type Log =
| CardToHand of string | CardToHand of string
| NewPhase of Phase | NewPhase of Phase
| StateChanged of PlayerState | StateChanged of PlayerState * PlayerState
type Client = Log -> int type Client = Log -> int
@ -138,7 +141,7 @@ module Game =
let draw (board: Board) = let draw (board: Board) =
match board ^. Board.currentPlayerDeck with match board ^. Board.currentPlayerDeck with
| [] -> board |> Board.currentPlayerState .-> PlayerState.Lost "deckout" | [] -> board |> Board.currentPlayerState .-> Lost "deckout"
| card :: deck -> | card :: deck ->
let hand = card :: (board ^. Board.currentPlayerHand) let hand = card :: (board ^. Board.currentPlayerHand)
@ -150,7 +153,9 @@ module Game =
let processPhase client board = let processPhase client board =
match board ^. Board.phase with match board ^. Board.phase with
| Draw -> draw board | Draw ->
if canDrawCard board <| board ^. Board.currentPlayer then draw board
else board
| _ -> board | _ -> board
let switchPhases (client: Client) board = let switchPhases (client: Client) board =
@ -162,15 +167,41 @@ module Game =
newBoard newBoard
let getPlayerStates board =
(board ^. (Board.firstPlayer << Player.state), board ^. (Board.secondPlayer << Player.state))
let resolvePlayerStates (p1, p2) =
let s1, s2 = p1.state, p2.state
match s1 with
| Lost reason ->
match s2 with
| InGame -> p1, p2 |> Player.state .-> Won reason
| _ -> p1, p2
| Won reason ->
match s2 with
| InGame -> p1, p2 |> Player.state .-> Lost reason
| _ -> p1, p2
| InGame ->
match s2 with
| InGame -> p1, p2
| Won reason -> p1 |> Player.state .-> Lost reason, p2
| Lost reason -> p1 |> Player.state .-> Won reason, p2
let resolveBoardState board = over Board.players resolvePlayerStates board
let rec game board (client: Client) = let rec game board (client: Client) =
let newBoard = let newBoard =
(processPhase client) (processPhase client)
>> (switchPhases client) >> resolveBoardState
<| board <| board
let currentState = newBoard ^. Board.currentPlayerState let currentState = newBoard ^. Board.currentPlayerState
if currentState <> InGame then if currentState <> InGame then
client <| StateChanged currentState |> ignore let newStates = getPlayerStates newBoard
failwith "end of game" client <| StateChanged newStates |> ignore
else else
game newBoard client game <| switchPhases client newBoard <| client

View file

@ -1,9 +1,16 @@
module Main = module Main =
open FSharpPlus.Lens open FSharpPlus.Lens
open Board.Player
open Board.Board open Board.Board
open Board.Game open Board.Game
open Card open Card
let printState state =
match state with
| Lost reason -> sprintf "lost because did: %s" reason
| Won reason -> sprintf "won because opponent did: %s" reason
| InGame -> "still playing"
[<EntryPoint>] [<EntryPoint>]
let main _ = let main _ =
let sampleCard = Card.Spell ({name= "sampleCard"; text="something"; effects = []}, {spellType = Card.ContinuosSpell}) let sampleCard = Card.Spell ({name= "sampleCard"; text="something"; effects = []}, {spellType = Card.ContinuosSpell})
@ -11,11 +18,15 @@
let client action = let client action =
match action with match action with
| StateChanged newState -> printfn "The new state you got is: %A" newState | StateChanged (s1, s2) ->
printfn "Player 1: %s" <| printState s1
printfn "Player 2: %s" <| printState s2
| NewPhase phase -> printfn "New phse: %A" phase | NewPhase phase -> printfn "New phse: %A" phase
| _ -> printfn "Something unkown happened" | _ -> printfn "Something unkown happened"
0 0
game board client game board client
0
0 // return integer code