Advertisement
Guest User

Untitled

a guest
Mar 28th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 3.66 KB | None | 0 0
  1. module TitleModule =
  2.     type GameTitle = GameTitle of string
  3.        
  4.     type TitleEvents =
  5.         | Start
  6.         | Exit
  7.  
  8. open TitleModule
  9. open System.Text.RegularExpressions
  10. open System.Linq
  11. open System
  12.  
  13. module EndGameModule =
  14.     type EndGameTitle = EndGameTitle of string
  15.  
  16.     type EndGameEvents =
  17.         | PlayAgain
  18.         | Exit
  19.  
  20. module BingoModule =
  21.     type Value = Value of int
  22.     type Cell =
  23.         { Value: Value
  24.           X: int
  25.           Y: int }
  26.  
  27.     type GameState =
  28.         | None
  29.         | Winner of Cell list
  30.  
  31.     type Bingo =
  32.         { Cells: Cell list
  33.           GameState: GameState }
  34.  
  35.     type BingoEvents =
  36.         | Loaded of Bingo
  37.         | PlaceValue of X: int * Y: int * Value: Value
  38.         | ValuePlaced of Bingo
  39.         | WinnerFound of Bingo
  40.         | Exit
  41.  
  42.     let generate height width =
  43.         {
  44.             Cells = []
  45.             GameState = None
  46.         }
  47.  
  48.     let placeValue bingo value x y = ()
  49.  
  50.     type Generator =
  51.         { generate: unit -> Value }
  52.  
  53. module GameProcessor =
  54.  
  55.     type CurrentGameState =
  56.         | Title of TitleModule.GameTitle
  57.         | Bingo of BingoModule.Bingo
  58.         | EndGame of EndGameModule.EndGameTitle
  59.         | Exit
  60.  
  61.     type Events =
  62.         | TitleEvents of TitleModule.TitleEvents
  63.         | BingoEvents of BingoModule.BingoEvents
  64.         | EndGameEvents of EndGameModule.EndGameEvents
  65.  
  66.     let private applyEvent currentGameState event =
  67.         match currentGameState, event with
  68.         | Title _, TitleEvents titleEvent ->
  69.             match titleEvent with
  70.             | TitleEvents.Start -> Bingo (BingoModule.generate 3 3)
  71.             | TitleEvents.Exit -> EndGame (EndGameModule.EndGameTitle "Buy")
  72.         | _ ->  currentGameState
  73.  
  74.     let processEvents events currentGameState =
  75.         events |> Seq.fold applyEvent currentGameState
  76.  
  77.     let init = Title (GameTitle "Bingo!")
  78.  
  79.     let view state = state.ToString()
  80.  
  81. open GameProcessor
  82.  
  83. [<EntryPoint>]
  84. let main _ =
  85.  
  86.     let rec untilPressed regex =
  87.         let consoleOutput = Console.ReadLine()
  88.  
  89.         match Regex.IsMatch(consoleOutput, regex) with
  90.         | true -> Regex.Matches(consoleOutput, regex)
  91.                         .Cast<Match>()
  92.                         .First()
  93.                         .Groups
  94.                         .Cast<Group>()
  95.                         .ToList()
  96.                         |> List.ofSeq
  97.                         |> List.map(fun x -> x.Value)
  98.         | false -> untilPressed regex
  99.  
  100.     let isPressed regex =
  101.         untilPressed regex |> ignore
  102.  
  103.     let run =        
  104.         let rec loop state =
  105.             printfn "%s" (state |> GameProcessor.view)
  106.            
  107.             let events =
  108.                 seq {
  109.                     match state with
  110.                     | CurrentGameState.Title _ ->
  111.                         isPressed "s"
  112.                         yield TitleEvents (TitleEvents.Start)
  113.                         isPressed "x"
  114.                         yield TitleEvents (TitleEvents.Exit)
  115.                     | CurrentGameState.Bingo _ ->
  116.                         printfn "Input coordinaes"
  117.                         let x, y = untilPressed "(\d+) (\d+)" |> (fun m ->
  118.                             printfn "%A" m
  119.                             m.[1] |> int, m.[2] |> int)
  120.  
  121.                         yield BingoEvents (BingoModule.BingoEvents.PlaceValue (x, y, BingoModule.Value 3) )
  122.                     | _ -> failwith "Unknown state"
  123.  
  124.                 }
  125.  
  126.             Console.Clear()
  127.  
  128.             loop (state |> GameProcessor.processEvents events)
  129.  
  130.         loop GameProcessor.init
  131.  
  132.     run |> ignore
  133.  
  134.     Console.ReadKey() |> ignore
  135.  
  136.     0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement