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:
// Learn more about F# at http://fsharp.net
open System
//change the console title
System.Console.Title<-"Race to Ten"
//adds the foreground and background color
System.Console.ForegroundColor<-ConsoleColor.DarkBlue
System.Console.BackgroundColor<-ConsoleColor.Gray
//clears the screen. This is to apply the background color once the
//console application is loaded
System.Console.Clear()
//display the game title screen
printfn "\t\t\t\tRace to Ten"
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..."
System.Console.ReadLine()|>ignore
System.Console.Clear()
//display the game description screen
printfn "Instructions:"
printfn "In this game, each player(you vs. the computer) enters a number between 1 to 3"
printfn "The previously inputted number will be added to the present number"
printfn "The first player to enter a number that adds up to 10 wins the game"
printfn "Press Esc to quit the game"
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..."

let mutable userkey=System.Console.ReadLine()
System.Console.Clear()
//declares our variables
let rndnum=new Random()
let mutable intsum=0
let mutable intusernum=0
let mutable intremain=0
//loop while sum is not equal to 10 and 
//the spacebar key has been pressed
while (intsum < 10 ) do
//computer generates a number
        printfn "\n\nAI's turn..."
        let intainum=rndnum.Next(1,3)
        printfn "AI num: %d" intainum
//accumulates the number to the
//value of sum
        intsum<-intsum + intainum
        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"
        System.Console.ReadLine()|>ignore
System.Console.Clear()
//display how many numbers more to go
//before 10
intremain<-intsum-10  
        printfn "%d more to go!" intremain
 //if the sum is equal to 10 
 //display "computer wins"
        if intsum>=10 then  
System.Console.Clear()
//reset the value of sum so that 
//the game will loop again
//remove intsum<-0 if you want the
 //game to end after one game
            printfn "Computer Wins!" 
            intsum<-0
            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"
            System.Console.ReadLine()|>ignore
System.Console.Clear()          
//otherwise ask for a number         
printfn "\n\nYour turn:" 
intusernum<-(int)(System.Console.ReadLine())
 //if the number exceeds 3 then
 //ask for a number again
        if intusernum>3 then
printfn "Number must be between 1 to 3"
printfn "You turn:"
intusernum<-(int)(System.Console.ReadLine())
            intsum<-intsum + intusernum
            System.Console.Clear()
//accumulates the inputted number to the value of sum
        intsum<-intsum + intusernum 
        intremain<-intsum-10  
        printfn "%d more to go!" intremain
        if intsum>=10 then
System.Console.Clear()
printfn "You Win!"
intsum<-0
            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"
            System.Console.ReadLine()|>ignore
System.Console.Clear()