Visual F# 100 Examples: Example 11

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

  1. // Learn more about F# at http://fsharp.net  
  2.   
  3. open System  
  4. //change the title to Crack the Safe  
  5. System.Console.Title<-"Crack the Safe"  
  6. //change the foreground color to green  
  7. Console.ForegroundColor<-ConsoleColor.Green  
  8. //dislay the game title screen  
  9. printfn "\t\t\t Crack the Safe"  
  10. printfn "\t\t\t Press enter to continue..."  
  11. //waits for a keypress from the user  
  12. System.Console.ReadLine()|>ignore  
  13. //Game action sequence  
  14. printfn "\t\t\t You have found a legendary treasure chest"  
  15. printfn "\t\t\t believed to contain the rarest gem on Earth"  
  16. printfn "\t\t\t Press enter to continue... "  
  17. //waits for a keypress from the user  
  18. System.Console.ReadLine()|>ignore  
  19.   
  20. printfn "\t\t\t this chest can be opened only by"  
  21. printfn "\t\t\t entering a mysterious 4-digit key code"  
  22. printfn "\t\t\t Press enter to continue..."  
  23. //waits for a keypress from the user  
  24. System.Console.ReadLine()|>ignore  
  25.   
  26. let random=new Random()  
  27. let intkeycode=Convert.ToInt32(random.Next(1000,9999))  
  28.   
  29. printfn "\t\t\tEnter the keycode:"  
  30. let intusercode= Convert.ToInt32(Console.ReadLine())  
  31. //if usercode is equal to the computer generated code then  
  32. if intusercode=intkeycode then  
  33. //display a congratulatory message  
  34.     printfn "\t\t\Congrats You have opened the treasure chest."  
  35.   
  36. //otherwise   
  37. else  
  38.     printfn "\t\t\tInvalid Code"  
  39.   
  40. printfn "\t\t\tPress enter to continue... "  
  41. System.Console.ReadLine()|>ignore  
  42. printfn "\t\t\tGame Over"  

Visual F# 100 Examples: Example 8 to 10

Problem: Make a console application that will accept five numbers and display the sum.
  1. open System  
  2. //change the title to Add Five Numbers  
  3. System.Console.Title<-"Add Five Numbers "  
  4. //change the foreground color to cyan  
  5. Console.ForegroundColor<-ConsoleColor.Cyan  
  6. printfn "\t\t\tEnter the first number:"  
  7. let intnum1=Convert.ToInt32(System.Console.ReadLine())  
  8. printfn "\t\t\tEnter the second number:"  
  9. let intnum2=Convert.ToInt32(System.Console.ReadLine())  
  10. printfn "\t\t\tEnter the third number:"  
  11. let intnum3=Convert.ToInt32(System.Console.ReadLine())  
  12. printfn "\t\t\tEnter the fourth number:"  
  13. let intnum4=Convert.ToInt32(System.Console.ReadLine())  
  14. printfn "\t\t\tEnter the fifth number:"  
  15. let intnum5=Convert.ToInt32(System.Console.ReadLine())  
  16. let  intsum=intnum1+ intnum2 + intnum3 + intnum4 + intnum5  
  17. printfn "\t\t\tThe sum is:%i" intsum  
Problem: Make a console application that will ask the power transmitted and power received then display the power loss value.
  1. open System  
  2. //change the title to Calculate Power Loss  
  3. System.Console.Title<-"Calculate Power Loss "  
  4. //change the foreground color to cyan  
  5. Console.ForegroundColor<-ConsoleColor.Cyan  
  6. printfn "\t\t\t\tPower transmitted:"  
  7. let dblpowertrans=Convert.ToDouble(System.Console.ReadLine())  
  8. printfn "\t\t\tPower recieved:"  
  9. let dblpowerrec=Convert.ToDouble(System.Console.ReadLine())  
  10. let dblpowerloss= dblpowertrans/ dblpowerrec  
  11. printfn "\t\t\t Power loss:%f " dblpowerloss  
Problem: Develop a console application that will ask the base value and height value then display the volume of a pyramid.
  1. open System  
  2. //change the title to Volume of a Pyramid  
  3. System.Console.Title<-" Volume of a Pyramid"  
  4. //change the foreground color to cyan  
  5. Console.ForegroundColor<-ConsoleColor.Cyan  
  6. printfn "\t\t\tBase value:"  
  7. let dblbase=Convert.ToDouble(System.Console.ReadLine())  
  8. printfn "\t\t\tHeight value:"  
  9. let dblheight=Convert.ToDouble(System.Console.ReadLine())  
  10. let  dblvolume=(dblbase*dblheight)/3.0  
  11. printfn "\t\t\tVolume of a pyramid:%f" dblvolume