Untitled

mail@pastecode.io avatar
unknown
fsharp
a year ago
3.0 kB
2
Indexable
Never
// For more information see https://aka.ms/fsharp-console-apps
open System
printfn "Welcome to Rock, Paper, Scissors, Lizard, Spock!"

type GameResults = PlayerWon | ComputerWon | Tie
type GameActions = Rock | Paper | Scissors | Lizard | Spock
let gameActionComparisons =
    Map.empty
        .Add(GameActions.Rock, [GameActions.Scissors; GameActions.Lizard])
        .Add(GameActions.Paper, [GameActions.Rock; GameActions.Spock])
        .Add(GameActions.Scissors, [GameActions.Paper; GameActions.Lizard])
        .Add(GameActions.Spock, [GameActions.Rock; GameActions.Scissors])
        .Add(GameActions.Lizard, [GameActions.Paper; GameActions.Spock])

let resolveGame (playerAction: GameActions) (computerAction: GameActions): GameResults =
    if (playerAction = computerAction)
        then GameResults.Tie
    else
        let playerWon = gameActionComparisons[playerAction] |> List.contains computerAction
        match playerWon with
        | true -> GameResults.PlayerWon
        | false -> GameResults.ComputerWon

let generateComputerAction(): GameActions =
    let Rand = Random()
    match Rand.Next(5) with
    | 0 -> 
        printfn ("computer got Rock")
        GameActions.Rock
    | 1 -> 
        printfn "computer got Paper"
        GameActions.Paper
    | 2 -> 
        printfn "computer got Scissors"
        GameActions.Scissors
    | 3 -> 
        printfn "computer got Lizard"
        GameActions.Lizard
    | 4 -> 
        printfn "computer got Spock"
        GameActions.Spock
    | _ -> failwith "somehow got 5?"

let evaluateGame (pressedKey: char): GameResults option =
    let computerActionRandom = generateComputerAction()
    match pressedKey with
    | '1' -> Some (resolveGame GameActions.Rock computerActionRandom)
    | '2' -> Some (resolveGame GameActions.Paper computerActionRandom)
    | '3' -> Some (resolveGame GameActions.Scissors computerActionRandom)
    | '4' -> Some (resolveGame GameActions.Lizard computerActionRandom)
    | '5' -> Some (resolveGame GameActions.Spock computerActionRandom)
    | 'q' -> None
    | _ -> failwith $"cant evaluate character %c{pressedKey}" 

let rec gameLoop (score: int * int * int): Unit =
    let (playerWins, computerWins, ties) = score
    printfn $"Score is Player: %d{playerWins}, Computer: %d{computerWins}, Ties: %d{ties}"
    printfn "'q' to quit"
    printfn "'1' for Rock"
    printfn "'2' for Paper"
    printfn "'3' for Scissors"
    printfn "'4' for Lizard"
    printfn "'5' for Spock"

    let pressedKey = System.Console.ReadKey().KeyChar
    System.Console.Clear()
    let evaluatedGame = evaluateGame pressedKey
    match evaluatedGame with
    | Some x ->
        match x with
        | GameResults.PlayerWon -> gameLoop (1+playerWins, computerWins, ties)
        | GameResults.ComputerWon -> gameLoop (playerWins, 1+computerWins, ties)
        | GameResults.Tie -> gameLoop (playerWins, computerWins, 1+ties)
    | None -> ()

[<EntryPoint>]
gameLoop (0,0,0)