Number guesser
The code for it is
display dialog "Here's a game. Guess a number from 0 to 10 and if you get it in less than 6 tries, you win! Let's go!"
set repeatTest to true
set theAnswer to random number from 1 to 10
set theFirstNumber to 1
repeat while (repeatTest = true)
display dialog "Well, let's play! Enter a number from 0 to 10." default answer "It has to be a number!"
set theString to the text returned of result as number
if theString is equal to theAnswer then
set repeatTest to false
display dialog "You guessed right! It was " & theAnswer & "! You got the number in " & theFirstNumber & " tries." buttons ("Yeah!")
else if theFirstNumber is greater than 5 then
set repeatTest to false
display dialog "Too many tries. You got it in " & theFirstNumber & " tries."
else if theString is less than 0 then
set repeatTest to true
display dialog "Cannot be lower than 1!" buttons ("OK")
else if theString is greater than 10 then
set repeatTest to true
display dialog "Cannot be higher than 10!" buttons ("OK")
else
set repeatTest to true
display dialog "Sorry..that's wrong." buttons ("Ohhh..")
set theFirstNumber to theFirstNumber + 1
end if
end repeat
I'll explain the code, but I won't explain anything else.
(display dialog "Here's a game. Guess a number from 0 to 10 and if you get it in less than 6 tries, you win! Let's go!") means it will display a dialog. You would've seen this from the Hello World page.
(set repeatTest to true) means the "repeatTest" is true.
(set theAnswer to random number from 1 to 10) means that the var "theAnswer" is set to a random number from to 10.
(set theFirstNumber to 1) means that the var "theFirstNumber" is set to the number 1.
(repeat while (repeatTest = true)) means that if the var "repeatTest" is true, it will go back to the display dialog part once there is nothing to happen.
It's another display dialog.
(set theString to the text returned of result as number) means that the var "theString" is set to the text returned of the result AS A NUMBER.
else if statement ahead!
(if theString is equal to theAnswer then) means that is the var "theString" is the same as the var "theAnswer" an action will happen. Let's just say that the action is theAction.
'theAction = ('set repeatTest to false
display dialog "You guessed right! It was " & theAnswer & "! You got the number in " & theFirstNumber & " tries." buttons ("Yeah!")) ...You know what will happen.
More to come!