Example 18: Race to Ten Text-Based Game

Problem: Make a simple Race to Ten text-based game. The details of the game is shown in the following screenshot:



Code:
  1. // Learn more about F# at http://fsharp.net  
  2. open System  
  3. //change the console title  
  4. System.Console.Title<-"Race to Ten"  
  5. //adds the foreground and background color  
  6. System.Console.ForegroundColor<-ConsoleColor.DarkBlue  
  7. System.Console.BackgroundColor<-ConsoleColor.Gray  
  8. //clears the screen. This is to apply the background color once the  
  9. //console application is loaded  
  10. System.Console.Clear()  
  11. //display the game title screen  
  12. printfn "\t\t\t\tRace to Ten"  
  13. printfn "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\tPress any key to continue..."  
  14. System.Console.ReadLine()|>ignore  
  15. System.Console.Clear()  
  16. //display the game description screen  
  17. printfn "Instructions:"  
  18. printfn "In this game, each player(you vs. the computer) enters a number between 1 to 3"  
  19. printfn "The previously inputted number will be added to the present number"  
  20. printfn "The first player to enter a number that adds up to 10 wins the game"  
  21. printfn "Press Esc to quit the game"  
  22. printfn "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\tPress any key to continue..."  
  23.   
  24. let mutable userkey=System.Console.ReadLine()  
  25. System.Console.Clear()  
  26. //declares our variables  
  27. let rndnum=new Random()  
  28. let mutable intsum=0  
  29. let mutable intusernum=0  
  30. let mutable intremain=0  
  31. //loop while sum is not equal to 10 and   
  32. //the spacebar key has been pressed  
  33. while (intsum < 10 ) do  
  34. //computer generates a number  
  35.         printfn "\n\nAI's turn..."  
  36.         let intainum=rndnum.Next(1,3)  
  37.         printfn "AI num: %d" intainum  
  38. //accumulates the number to the  
  39. //value of sum  
  40.         intsum<-intsum + intainum  
  41.         printfn "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\tPress any key to continue...Esc to quit"  
  42.         System.Console.ReadLine()|>ignore  
  43. System.Console.Clear()  
  44. //display how many numbers more to go  
  45. //before 10  
  46. intremain<-intsum-10    
  47.         printfn "%d more to go!" intremain  
  48.  //if the sum is equal to 10   
  49.  //display "computer wins"  
  50.         if intsum>=10 then    
  51. System.Console.Clear()  
  52. //reset the value of sum so that   
  53. //the game will loop again  
  54. //remove intsum<-0 if you want the  
  55.  //game to end after one game  
  56.             printfn "Computer Wins!"   
  57.             intsum<-0  
  58.             printfn "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\tPress any key to continue...Esc to quit"  
  59.             System.Console.ReadLine()|>ignore  
  60. System.Console.Clear()            
  61. //otherwise ask for a number           
  62. printfn "\n\nYour turn:"   
  63. intusernum<-(int)(System.Console.ReadLine())  
  64.  //if the number exceeds 3 then  
  65.  //ask for a number again  
  66.         if intusernum>3 then  
  67. printfn "Number must be between 1 to 3"  
  68. printfn "You turn:"  
  69. intusernum<-(int)(System.Console.ReadLine())  
  70.             intsum<-intsum + intusernum  
  71.             System.Console.Clear()  
  72. //accumulates the inputted number to the value of sum  
  73.         intsum<-intsum + intusernum   
  74.         intremain<-intsum-10    
  75.         printfn "%d more to go!" intremain  
  76.         if intsum>=10 then  
  77. System.Console.Clear()  
  78. printfn "You Win!"  
  79. intsum<-0  
  80.             printfn "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\tPress any key to continue...Esc to quit"  
  81.             System.Console.ReadLine()|>ignore  
  82. System.Console.Clear()