Advertisement
Guest User

Untitled

a guest
May 11th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.33 KB | None | 0 0
  1. open System
  2.  
  3. /// Union of possible choices for a round of rock-paper-scissors
  4. type Choice =
  5. | Rock
  6. | Paper
  7. | Scissors
  8.  
  9. /// Gets the string representation of a Choice
  10. let getString = function
  11.    | Rock -> "Rock"
  12.    | Paper -> "Paper"
  13.    | Scissors -> "Scissors"
  14.  
  15. /// Defines rules for winning and losing
  16. let beats (a : Choice, b : Choice) =
  17.    match a, b with
  18.    | Rock, Scissors -> true    // Rock beats Scissors
  19.    | Paper, Rock -> true       // Paper beats Rock
  20.    | Scissors, Paper -> true   // Scissors beat Paper
  21.    | _, _ -> false
  22.  
  23. let genMove =
  24.     Scissors
  25.  
  26. /// Gets the move chosen by the player
  27. let rec getMove() =
  28.    printfn "Rock, Paper, Scissors"
  29.    printf "Please enter your choice: "
  30.    let choice = Console.ReadLine ()
  31.    match choice with
  32.    | "rock" | "Rock" -> Rock
  33.    | "paper" | "Paper" -> Paper
  34.    | "scissors" | "Scissors" -> Scissors
  35.    | _ ->
  36.        printf "Invalid choice.\n\n"
  37.        getMove ()
  38.  
  39. /// Place where all the game logic takes place.
  40. let rec game() =
  41.    let computer = genMove
  42.    let player = getMove()
  43.    Console.WriteLine ("Player: {0} vs Computer: {1}", getString player, getString computer)
  44.    Console.WriteLine (
  45.        if beats(player, computer) then "Player Wins!\n"
  46.        elif beats(computer, player) then "Computer Wins!\n"
  47.        else "Draw!\n"
  48.    )
  49.    game()
  50.  
  51. game()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement