Visual F# 100 Examples: Example 12 and 13

This is the continuation of our planned 100 Visual F# examples. By the way, the bugs in examples number 7 to 11 had been fixed. My apologies, I should’nt have written those without using Visual F# compiler:)


Problem: Make a console application that will asks the user to enter the day that he was born and display his character or future based on the famous nursery rhyme written in England, “Monday’s Child”.

open System
//changes the console application title
System.Console.Title<-"Tell the Future"
//adds foreground color
System.Console.ForegroundColor<-ConsoleColor.Cyan
printfn "What day were you born?"
let strday=System.Console.ReadLine()
//clears the screen
System.Console.Clear()
//converts the inputs to lowercase then compare it our
//specified values
if strday.ToLower()="monday" then
    printfn "Monday's child is fair of face"
else if strday.ToLower()="tuesday" then
    printfn "Tuesday's child is full of grace"
else if strday.ToLower()="wednesday" then
    printfn "Wednesday's child is full of woe"
else if strday.ToLower()="thursday" then
    printfn "Thurdays's child has far to go"
else if strday.ToLower()="friday" then
    printfn "Friday's child is loving and giving"
else if strday.ToLower()="saturday" then
    printfn "Saturday's child works hard for a living"
else if strday.ToLower()="sunday" then
    printfn "Sunday's child is bonny and blithe and good and gay"
else
    printfn "Invalid input"
Problem: Develop a console application that will asks the wind speed in kilometer per hour(kph) and display its equivalent Philippine Storm Signal number.
open System
//changes the console application title
System.Console.Title<-"Determine Storm Signal Number"
//adds foreground color
System.Console.ForegroundColor<-ConsoleColor.Cyan
printfn "Enter wind speed(kph):"
let intspeed=Convert.ToInt32(System.Console.ReadLine())
//clears the screen
System.Console.Clear()
//if the wind speed ranges from 30 to 60
if intspeed>=30 && intspeed<=60 then
    printfn "Storm signal number 1"
//if the wind speed ranges from 61 to 100
else if intspeed>60 && intspeed<=100 then
    printfn "Storm signal number 2"
//if the wind speed ranges from 101 to 185
else if intspeed>100 && intspeed<=185 then
    printfn "Storm signal number 3"
//if the wind speed is greater than 185
else if intspeed>185 then
printfn "Storm signal number 4"
else 
printfn "Invalid input"

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"