2019-12-05 18:17:05 +01:00
|
|
|
module Board
|
|
|
|
|
2019-12-09 13:20:39 +01:00
|
|
|
open FSharpPlus.Lens
|
|
|
|
|
2019-12-06 10:51:27 +01:00
|
|
|
module Side =
|
2019-12-09 10:44:05 +01:00
|
|
|
open Card.Card
|
2019-12-05 18:17:05 +01:00
|
|
|
|
2019-12-09 10:44:05 +01:00
|
|
|
type Side<'s> =
|
|
|
|
{ field: CardInstance<'s> option
|
|
|
|
monsters: CardInstance<'s> list
|
|
|
|
spells: CardInstance<'s> list
|
|
|
|
graveyard: CardInstance<'s> list
|
|
|
|
deck: CardInstance<'s> list }
|
2019-12-05 18:25:03 +01:00
|
|
|
|
2019-12-09 13:20:39 +01:00
|
|
|
module Side =
|
|
|
|
let inline field f side = f side.field <&> fun v -> { side with field = v }
|
|
|
|
let inline monsters f side = f side.monsters <&> fun v -> { side with monsters = v }
|
|
|
|
let inline spells f side = f side.spells <&> fun v -> { side with spells = v }
|
|
|
|
let inline graveyard f side = f side.graveyard <&> fun v -> { side with graveyard = v }
|
|
|
|
let inline deck f side = f side.deck <&> fun v -> { side with deck = v }
|
|
|
|
|
2019-12-06 10:51:27 +01:00
|
|
|
let emptySide =
|
|
|
|
{ field = None
|
|
|
|
monsters = []
|
|
|
|
spells = []
|
|
|
|
graveyard = []
|
|
|
|
deck = [] }
|
|
|
|
|
|
|
|
|
|
|
|
module Player =
|
|
|
|
open Side
|
2019-12-09 10:44:05 +01:00
|
|
|
open Card.Card
|
2019-12-06 10:51:27 +01:00
|
|
|
|
2019-12-07 18:56:20 +01:00
|
|
|
type PlayerState =
|
|
|
|
| InGame
|
|
|
|
| Won
|
|
|
|
| Lost of reason: string
|
|
|
|
|
2019-12-09 10:44:05 +01:00
|
|
|
type Player<'s> =
|
2019-12-06 10:51:27 +01:00
|
|
|
{ lifePoints: int
|
2019-12-09 10:44:05 +01:00
|
|
|
side: Side<'s>
|
|
|
|
hand: CardInstance<'s> list
|
2019-12-07 18:56:20 +01:00
|
|
|
state: PlayerState }
|
2019-12-06 10:51:27 +01:00
|
|
|
|
2019-12-09 13:20:39 +01:00
|
|
|
module Player =
|
|
|
|
let inline lifePoints f player = f player.lifePoints <&> fun v -> { player with lifePoints = v }
|
|
|
|
let inline side f player = f player.side <&> fun v -> { player with side = v }
|
|
|
|
let inline hand f player = f player.hand <&> fun v -> { player with hand = v }
|
|
|
|
let inline state f player = f player.state <&> fun v -> { player with state = v }
|
|
|
|
let inline deck f player = (side << Side.deck) f player
|
|
|
|
|
2019-12-06 10:51:27 +01:00
|
|
|
let initialPlayer lp =
|
|
|
|
{ lifePoints = lp
|
2019-12-07 18:31:53 +01:00
|
|
|
side = emptySide
|
2019-12-07 18:56:20 +01:00
|
|
|
hand = []
|
|
|
|
state = InGame }
|
2019-12-06 10:51:27 +01:00
|
|
|
|
2019-12-09 10:44:05 +01:00
|
|
|
// module Side =
|
|
|
|
|
2019-12-07 18:31:53 +01:00
|
|
|
module Turn =
|
|
|
|
type Phase =
|
|
|
|
| Draw
|
|
|
|
| Standby
|
|
|
|
| Main1
|
|
|
|
| Battle
|
|
|
|
| Main2
|
|
|
|
| End
|
2019-12-06 10:51:27 +01:00
|
|
|
|
2019-12-09 13:20:39 +01:00
|
|
|
let nextPhase (turn, phase) =
|
|
|
|
match phase with
|
|
|
|
| Draw -> (turn, Standby)
|
|
|
|
| Standby -> (turn, Main1)
|
|
|
|
| Main1 -> (turn, Battle)
|
|
|
|
| Battle -> (turn, Main2)
|
|
|
|
| Main2 -> (turn, End)
|
|
|
|
| End -> (turn + 1, Draw)
|
2019-12-06 10:51:27 +01:00
|
|
|
|
|
|
|
module Board =
|
2019-12-07 18:31:53 +01:00
|
|
|
open Turn
|
2019-12-09 10:44:05 +01:00
|
|
|
open Card
|
2019-12-06 10:51:27 +01:00
|
|
|
|
2019-12-09 10:44:05 +01:00
|
|
|
type Player = Player.Player<Board>
|
|
|
|
|
|
|
|
and Board =
|
2019-12-07 18:31:53 +01:00
|
|
|
{ players: Player * Player
|
2019-12-09 13:20:39 +01:00
|
|
|
moment: int * Phase }
|
|
|
|
|
|
|
|
module Board =
|
|
|
|
let inline players f board = f board.players <&> fun v -> { board with players = v }
|
|
|
|
let inline moment f board = f board.moment <&> fun v -> { board with moment = v }
|
|
|
|
|
|
|
|
let inline turn f board = (moment << _1) f board
|
|
|
|
let inline phase f board = (moment << _2) f board
|
|
|
|
|
|
|
|
let inline currentPlayer f board =
|
|
|
|
if (view turn board) % 2 = 0 then (players << _2) f board
|
|
|
|
else (players << _1) f board
|
2019-12-07 18:31:53 +01:00
|
|
|
|
2019-12-09 10:44:05 +01:00
|
|
|
type Card = Card.Card<Board>
|
|
|
|
|
|
|
|
type CardInstance = Card.CardInstance<Board>
|
|
|
|
|
|
|
|
type Effect = Effect.Effect<Board>
|
|
|
|
|
|
|
|
type Condition = Effect.Condition<Board>
|
|
|
|
|
|
|
|
type Action = Effect.Action<Board>
|
|
|
|
|
2019-12-07 18:31:53 +01:00
|
|
|
let emptyBoard =
|
2019-12-09 10:44:05 +01:00
|
|
|
{ players = (Player.initialPlayer 8000, Player.initialPlayer 8000)
|
2019-12-09 13:20:39 +01:00
|
|
|
moment = 0, Draw }
|
2019-12-07 18:31:53 +01:00
|
|
|
|
|
|
|
module Game =
|
|
|
|
open Board
|
|
|
|
open Turn
|
|
|
|
open Player
|
2019-12-09 13:20:39 +01:00
|
|
|
open Side
|
2019-12-09 10:44:05 +01:00
|
|
|
|
|
|
|
type PlayerAction =
|
|
|
|
| Pass
|
|
|
|
| NormalSummon
|
|
|
|
| InitialDraw
|
|
|
|
| Activate
|
|
|
|
| Set
|
|
|
|
|
2019-12-07 18:31:53 +01:00
|
|
|
|
|
|
|
let draw (player: Player) =
|
|
|
|
match player.side.deck with
|
2019-12-09 13:20:39 +01:00
|
|
|
| [] -> player |> Player.state .-> Lost "deckout"
|
2019-12-07 18:31:53 +01:00
|
|
|
| card :: deck ->
|
2019-12-09 13:20:39 +01:00
|
|
|
let hand = card :: player.hand
|
2019-12-07 18:31:53 +01:00
|
|
|
|
2019-12-09 13:20:39 +01:00
|
|
|
player
|
|
|
|
|> Player.hand .-> hand
|
|
|
|
|> Player.deck .-> deck
|
2019-12-07 18:31:53 +01:00
|
|
|
|
2019-12-09 13:20:39 +01:00
|
|
|
// Player is the last arg to be able to use this with the withCurrentPlayer function
|
|
|
|
let toDeckBottom (card: CardInstance) (player: Player) = over Player.deck (fun d -> card :: d) player
|
2019-12-07 18:31:53 +01:00
|
|
|
|
|
|
|
let processTurn (board: Board) =
|
2019-12-09 13:20:39 +01:00
|
|
|
match board ^. Board.phase with
|
|
|
|
| Draw -> over Board.currentPlayer draw board
|
2019-12-07 18:31:53 +01:00
|
|
|
| _ -> board
|
|
|
|
|
2019-12-09 13:20:39 +01:00
|
|
|
let doTurn (board: Board) = over Board.moment nextPhase <| processTurn board
|