Visual F# 100 Examples: Example 11

Problem: Make a simple crack the treasure chest console application game.

// Learn more about F# at http://fsharp.net

open System
//change the title to Crack the Safe
System.Console.Title<-"Crack the Safe"
//change the foreground color to green
Console.ForegroundColor<-ConsoleColor.Green
//dislay the game title screen
printfn "\t\t\t Crack the Safe"
printfn "\t\t\t Press enter to continue..."
//waits for a keypress from the user
System.Console.ReadLine()|>ignore
//Game action sequence
printfn "\t\t\t You have found a legendary treasure chest"
printfn "\t\t\t believed to contain the rarest gem on Earth"
printfn "\t\t\t Press enter to continue... "
//waits for a keypress from the user
System.Console.ReadLine()|>ignore

printfn "\t\t\t this chest can be opened only by"
printfn "\t\t\t entering a mysterious 4-digit key code"
printfn "\t\t\t Press enter to continue..."
//waits for a keypress from the user
System.Console.ReadLine()|>ignore

let random=new Random()
let intkeycode=Convert.ToInt32(random.Next(1000,9999))

printfn "\t\t\tEnter the keycode:"
let intusercode= Convert.ToInt32(Console.ReadLine())
//if usercode is equal to the computer generated code then
if intusercode=intkeycode then
//display a congratulatory message
    printfn "\t\t\Congrats You have opened the treasure chest."

//otherwise 
else
    printfn "\t\t\tInvalid Code"

printfn "\t\t\tPress enter to continue... "
System.Console.ReadLine()|>ignore
printfn "\t\t\tGame Over"

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.