Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- open System
- /// Union of possible choices for a round of rock-paper-scissors
- type Choice =
- | Rock
- | Paper
- | Scissors
- /// Gets the string representation of a Choice
- let getString = function
- | Rock -> "Rock"
- | Paper -> "Paper"
- | Scissors -> "Scissors"
- /// Defines rules for winning and losing
- let beats (a : Choice, b : Choice) =
- match a, b with
- | Rock, Scissors -> true // Rock beats Scissors
- | Paper, Rock -> true // Paper beats Rock
- | Scissors, Paper -> true // Scissors beat Paper
- | _, _ -> false
- let genMove =
- Scissors
- /// Gets the move chosen by the player
- let rec getMove() =
- printfn "Rock, Paper, Scissors"
- printf "Please enter your choice: "
- let choice = Console.ReadLine ()
- match choice with
- | "rock" | "Rock" -> Rock
- | "paper" | "Paper" -> Paper
- | "scissors" | "Scissors" -> Scissors
- | _ ->
- printf "Invalid choice.\n\n"
- getMove ()
- /// Place where all the game logic takes place.
- let rec game() =
- let computer = genMove
- let player = getMove()
- Console.WriteLine ("Player: {0} vs Computer: {1}", getString player, getString computer)
- Console.WriteLine (
- if beats(player, computer) then "Player Wins!\n"
- elif beats(computer, player) then "Computer Wins!\n"
- else "Draw!\n"
- )
- game()
- game()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement