0 people like it.

A CONSOLE-BASED ROCK-PAPER-SCISSORS

When playing rock-paper-scissors (RPS), we show 0 fingers for rock, 2 for scissors and 5 for paper. This rule is also applied for this console-based game written in F#. Enjoy !

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
59: 
60: 
61: 
62: 
63: 
64: 
65: 
66: 
67: 
68: 
69: 
70: 
71: 
72: 
73: 
74: 
75: 
76: 
77: 
78: 
79: 
80: 
81: 
82: 
83: 
84: 
85: 
86: 
87: 
88: 
89: 
90: 
91: 
92: 
93: 
open System 

let RPS plr npc : int8 =

    match plr, npc with 
        | 0 , 0 | 2 , 2 | 5 , 5 ->  0y // Draw
        | 0 , 2 | 2 , 5 | 5 , 0 ->  1y // Player wins
        | 0 , 5 | 5 , 2 | 2 , 0 ->  2y // NPC wins
        | 6 , _                 -> -6y // Valid input from the player
        |           _           ->  9y // Default case

let Game : unit =
    
    let mutable local = 0
    let mutable total = 0

    let mutable again = ConsoleKey.Y
    while again = ConsoleKey.Y do

        local <- 0

        printf  "\n\n\t\t\tPLAYER\t\tNPC\t\tRESULT\n\n"

        for i in 1 .. 5 do
            
            printf "  ROUND %d / 5 :\t\t" i
            let plr = match System.Console.ReadKey(true).Key with

                        | ConsoleKey.NumPad0 | ConsoleKey.D0
                        | ConsoleKey.R  ->  Console.ForegroundColor <- ConsoleColor.Green
                                            printf "ROCK\t\t"
                                            0

                        | ConsoleKey.NumPad2 | ConsoleKey.D2
                        | ConsoleKey.S  ->  Console.ForegroundColor <- ConsoleColor.Yellow
                                            printf "SCISSORS\t"
                                            2

                        | ConsoleKey.NumPad5 | ConsoleKey.D5
                        | ConsoleKey.P  ->  Console.ForegroundColor <- ConsoleColor.Cyan
                                            printf "PAPER\t\t"
                                            5

                        | _             ->  Console.ForegroundColor <- ConsoleColor.DarkRed
                                            printf "???\t\t"
                                            6

            let npc = match (System.Random()).Next(0,3) with
                        | 0     ->  Console.ForegroundColor <- ConsoleColor.Green
                                    printf "ROCK\t\t"
                                    0
                        | 1     ->  Console.ForegroundColor <- ConsoleColor.Yellow
                                    printf "SCISSORS\t"
                                    2
                        | _     ->  Console.ForegroundColor <- ConsoleColor.Cyan
                                    printf "PAPER\t\t"
                                    5

            let dxs = match RPS plr npc with
                        |  0y   ->  Console.ForegroundColor <- ConsoleColor.Cyan
                                    printf "TIE\n" 
                                    +0
                        |  1y   ->  Console.ForegroundColor <- ConsoleColor.Green
                                    printf "WON\n" 
                                    +1
                        |  2y   ->  Console.ForegroundColor <- ConsoleColor.Red
                                    printf "LOST\n" 
                                    -1
                        | -6y   ->  Console.ForegroundColor <- ConsoleColor.DarkRed
                                    printf "???\n" 
                                    -2
                        |  _  ->    -0
            
            local <- local + dxs

            Console.ResetColor ()

        match local with
            | _ when local > 0 -> total <- total + 1
            | _ when local < 0 -> total <- total - 1   
            | _                -> ()

        printf "\n      >>  YOUR TOTAL SCORE: %d\n" total
        printf "\n      >>  Do you want to play again ? (Y/N) "
        again <- Console.ReadKey(true).Key

        printf "\n\n  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  "

    printf "\n\n      >>  YOUR FINAL SCORE: %d " total

    ()

Console.ReadKey(true).Key |> ignore
namespace System
val RPS : plr:int -> npc:int -> int8
val plr : int
val npc : int
Multiple items
val int8 : value:'T -> int8 (requires member op_Explicit)

--------------------
type int8 = SByte
val Game : unit
type unit = Unit
val mutable local : int
val mutable total : int
val mutable again : ConsoleKey
type ConsoleKey =
  | Backspace = 8
  | Tab = 9
  | Clear = 12
  | Enter = 13
  | Pause = 19
  | Escape = 27
  | Spacebar = 32
  | PageUp = 33
  | PageDown = 34
  | End = 35
  ...
field ConsoleKey.Y: ConsoleKey = 89
val printf : format:Printf.TextWriterFormat<'T> -> 'T
val i : int32
type Console =
  static member BackgroundColor : ConsoleColor with get, set
  static member Beep : unit -> unit + 1 overload
  static member BufferHeight : int with get, set
  static member BufferWidth : int with get, set
  static member CapsLock : bool
  static member Clear : unit -> unit
  static member CursorLeft : int with get, set
  static member CursorSize : int with get, set
  static member CursorTop : int with get, set
  static member CursorVisible : bool with get, set
  ...
Console.ReadKey() : ConsoleKeyInfo
Console.ReadKey(intercept: bool) : ConsoleKeyInfo
field ConsoleKey.NumPad0: ConsoleKey = 96
field ConsoleKey.D0: ConsoleKey = 48
field ConsoleKey.R: ConsoleKey = 82
property Console.ForegroundColor: ConsoleColor with get, set
type ConsoleColor =
  | Black = 0
  | DarkBlue = 1
  | DarkGreen = 2
  | DarkCyan = 3
  | DarkRed = 4
  | DarkMagenta = 5
  | DarkYellow = 6
  | Gray = 7
  | DarkGray = 8
  | Blue = 9
  ...
field ConsoleColor.Green: ConsoleColor = 10
field ConsoleKey.NumPad2: ConsoleKey = 98
field ConsoleKey.D2: ConsoleKey = 50
field ConsoleKey.S: ConsoleKey = 83
field ConsoleColor.Yellow: ConsoleColor = 14
field ConsoleKey.NumPad5: ConsoleKey = 101
field ConsoleKey.D5: ConsoleKey = 53
field ConsoleKey.P: ConsoleKey = 80
field ConsoleColor.Cyan: ConsoleColor = 11
field ConsoleColor.DarkRed: ConsoleColor = 4
Multiple items
type Random =
  new : unit -> Random + 1 overload
  member Next : unit -> int + 2 overloads
  member NextBytes : buffer:byte[] -> unit + 1 overload
  member NextDouble : unit -> float

--------------------
Random() : Random
Random(Seed: int) : Random
val dxs : int
field ConsoleColor.Red: ConsoleColor = 12
Console.ResetColor() : unit
val ignore : value:'T -> unit
Raw view Test code New version

More information

Link:http://fssnip.net/7Yt
Posted:4 years ago
Author:me
Tags: game