Microsoft Small Basic

Program Listing: KNB159
'Last One Loses - Text Version

difficultycheck = 1
While difficultycheck = 1
TextWindow.WriteLine("What difficulty level? (1, 2, 3, 4, or 5)")
difficulty = TextWindow.ReadNumber()

If(difficulty >= 1 and difficulty <= 5)Then
difficultycheck = 0
EndIf
EndWhile

matchesTaken = 0
TextWindow.WriteLine("How many matches will be used?")
numOfMatches = TextWindow.ReadNumber()
firstMove = Math.GetRandomNumber(2)

FindFirstMove()


If PlayerFirst = "true" Then
TextWindow.WriteLine("You can go first")
Goto playerMoveLabel
Else
TextWindow.WriteLine("I'll go first")
Goto comMoveLabel
EndIf

playerMoveLabel:

TextWindow.WriteLine(numOfMatches + " matches remain.")

playerTakeLabel:

TextWindow.WriteLine("How many would you like to take? (1, 2, or 3)")
matchesTaken = TextWindow.ReadNumber()

If(matchesTaken < 0 Or matchesTaken > 3)Then
goto playerTakeLabel
Else
If(numOfMatches - matchesTaken < 1) Then
Goto playerTakeLabel
Else
numOfMatches = numOfMatches - matchesTaken
EndIf
EndIf

If(numOfMatches <= 1) Then
Win()
Else
TextWindow.WriteLine(numOfMatches + " matches remain.")
EndIf


comMoveLabel:
AI()

If(numOfMatches - comMatchesTaken < 1) Then
Goto comMoveLabel
Else
numOfMatches = numOfMatches - comMatchesTaken
EndIf


If(numOfMatches <= 1) Then
Lose()
Else
Goto playerMoveLabel
EndIf

'Subroutines

Sub FindFirstMove
If firstMove = 1 Then
PlayerFirst = "true"
Else
PlayerFirst = "false"
EndIf
EndSub

Sub AI
smartAI = Math.GetRandomNumber(difficulty * difficulty)

If(smartAI < difficulty) Then
If(matchesTaken = 0) Then
comMatchesTaken = Math.GetRandomNumber(3)
Else
If(matchesTaken = 1) Then
comMatchesTaken = 3
ElseIf(matchesTaken = 2) Then
comMatchesTaken = 2
ElseIf(matchesTaken = 3)Then
comMatchesTaken= 1
EndIf
EndIf
Else
comMatchesTaken = Math.GetRandomNumber(3)
EndIf

Program.Delay(2000)
If(comMatchesTaken = 1) Then
TextWindow.WriteLine("I'll take " + comMatchesTaken + " match")
Else
TextWindow.WriteLine("I'll take " + comMatchesTaken + " matches")
EndIf
Program.Delay(1000)
EndSub

Sub Lose
TextWindow.WriteLine("You Lost!")
TextWindow.WriteLine("How's that floor taste?")
Program.Delay(3000)
Program.End()
EndSub

Sub Win
TextWindow.WriteLine("You won!")
TextWindow.WriteLine("You beat me, but next time you won't be so lucky...")
Program.Delay(3000)
Program.End()
EndSub