Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (*
- * Angel T. Vazquez
- * 841-12-9051
- *
- *Rock paper scissor games in f#
- *)
- module rock_paper_scissors
- open System
- let rec start_game acc wins =
- //Main loop of the game
- if acc > 0.0
- then printf "Do you want to play again (Y/N) "
- let decision = Console.ReadLine()
- match decision with
- |"N" -> printfn "\nThanks for playing. \nYou won %.1f percent of the time." (((wins:float) / (acc:float)) * 100.0)
- |"Y" ->
- printf "\nEnter your selection (0=Rock, 1=Paper, 2=Scissors): " //Asks for user selection
- let selection = Console.ReadLine()
- //Creates a random number between zero and two
- let seed = Random ()
- let cpuSelection = seed.Next(0,2)
- //Matches user selection with cpuselection
- //To determine winner
- match selection with
- |"0"-> printf "My selection is : 0 = Rock "
- if cpuSelection = 0
- then printfn"Tie"
- start_game (acc + 1.0) (wins)
- elif cpuSelection = 2
- then printfn "You win"
- start_game (acc + 1.0) (wins + 1.0)
- else printfn "You lose"
- start_game (acc + 1.0) (wins)
- |"1"-> printf "My selection is : 1 = Paper "
- if cpuSelection = 1
- then printfn"tie"
- start_game (acc + 1.0) (wins)
- elif cpuSelection = 0
- then printfn "You win"
- start_game (acc + 1.0) (wins + 1.0)
- else printfn "You lose"
- start_game (acc + 1.0) (wins)
- |"2"-> printf "My selection is : 2 = Scissors "
- if cpuSelection = 2
- then printfn"tie"
- start_game (acc + 1.0) (wins)
- elif cpuSelection = 1
- then printfn "You win"
- start_game (acc + 1.0) (wins + 1.0)
- else printfn "You lose"
- start_game (acc + 1.0) (wins)
- |_ -> printf("dss")
- |_ -> printf ""
- //When the game is just strating
- else
- printf "Enter your selection (0=Rock, 1=Paper, 2=Scissors): "
- let selection = Console.ReadLine()
- let seed = Random ()
- let cpuSelection = seed.Next(0,2)
- //Matches user selection with cpu selection to determine winner
- match selection with
- |"0"-> printf "My selection is : 0 = Rock "
- if cpuSelection = 0
- then printfn"Tie"
- start_game (acc + 1.0) (wins)
- elif cpuSelection = 2
- then printfn "You win"
- start_game (acc + 1.0) (wins + 1.0)
- else printfn "You lose"
- start_game (acc + 1.0) (wins)
- |"1"-> printf "My selection is : 1 = Paper "
- if cpuSelection = 1
- then printfn"tie"
- start_game (acc + 1.0) (wins)
- elif cpuSelection = 0
- then printfn "You win"
- start_game (acc + 1.0) (wins + 1.0)
- else printfn "You lose"
- start_game (acc + 1.0) (wins)
- |"2"-> printf "My selection is : 2 = Scissors "
- if cpuSelection = 2
- then printfn"tie"
- start_game (acc + 1.0) (wins)
- elif cpuSelection = 1
- then printfn "You win"
- start_game (acc + 1.0) (wins + 1.0)
- else printfn "You lose"
- start_game (acc + 1.0) (wins)
- |_ -> printf("dss")
- start_game 0.0 0.0
- System.Console.ReadKey() |> ignore
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement