This one is regarding handlers and improving script organizations. Once I am done with this book (2 to 4 more chapters), I will create a single reference post with all the results for others to follow and include a link in my signature area for others.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
----------------------------------------
-- Script Name: AppleScript Shell Game.scpt
-- Version: 1.0
-- Author: Jerry Lee Ford, Jr.
-- Date: August 2007
--
-- Description: This AppleScript game challenges the player to guess under
-- which of three shells the game's virtual ball is hiding.
----------------------------------------
-- Declare the following variable as having a global scope
global GameTitle
-- Define and initialize variable values
set AcctBalance to 10 -- The amount of money in the player's account
set GameTitle to "AppleScript Shell Game" -- Stores the game's name
-- Loop until the player goes broke or decides to quit
repeat until AcctBalance is less than or equal to 0
-- Call the handler that collects the player's bet
set Bet to GetBet (AcctBalance)
-- Analyze the player's input
if Bet = "Quit Game" then -- The player clicked on the Quit Game button
exit repeat
else if
-- The player has made a bet
if Bet = "$1" then set Bet to 1
if Bet = "$2" then set Bet to 2
end if
-- Call the handler that generates random numbers
set randomNumber to GetRandomNumber ()
-- Call the handler that collect's the player's guess
set Guess to GetGuess ()
-- Call the handler that processes the player's guess
set AcctBalance to ProcessGuess (Guess, randomNumber, AcctBalance, Bet)
end repeat
-- To get here, the player has either quit the game or gone broke
if AcctBalance is less than or equal to 0 then
display dialog "Game Over. You have gone broke."
else if
display dialog "Please play again soon!" buttons {"OK"}
with GameTitle
end if
-- This handler generates a random number between 1 and 3 representing
-- the shell under which the ball is hidden
on GetRandomNumber ()
-- Generate a random number between 1 and 3
set randomNo to random number from 1 to 3
return randomNo -- Return the random number to the calling statement
end GetRandomNumber
-- This handler prompts the player to make a bet or to quit
on GetBet (AcctBalance)
-- Prompt the player for the size of her bet or to Quit
set Amount to button returned of (display dialog "You have $" &¬
AcctBalance & "in your account." & return & return &¬
"How much would you like to bet?" buttons {"$1", "$2", "Quit Game"} with title GameTitle)
return Amount - Return the player's selection
end GetBet
-- This handler prompts the player to guess which shell the ball is under
on GetGuess ()
-- Prompt the player which shell the ball is under
set reply to button returned of (display dialog "OK, now that you" &¬
"have made your bet, it is" &
"time to make a guess. Which shell do you think the" &¬
"ball is under?" buttons {"Shell 1", "Shell 2", "Shell 3"} ¬
with title GameTitle)
-- Translate the player's selection into a number
if reply = "Shell 1" then
set reply to 1
else if reply = "Shell 2" then
set reply to 2
else if reply = "Shell 3" then
set reply to 3
end if
return reply -- Return a number representing the player's guess
end GetGuess
-- This handler processes the player's guess and determines whether the
-- player has won or lost
on ProcessGuess(Guess, randomNumber, AcctBalance, Bet)
-- Determine if the player's guess was right or wrong
if Guess = randomNumber then -- The player guess was correct
set AcctBalance to AcctBalance + Bet -- Adjust the player's account
display dialog "Correct! You win $" & Bet &¬
buttons {"Ok"} with title GameTitle
else if -- The player's guess was not correct
set AcctBalance to AcctBalance - Bet -- Adjust the player's account
display dialog "Incorrect! You lose $" & Bet &¬
". The ball was under Shell" &¬
randomNumber & "." buttons {"OK"} with title GameTitle
end if
return AcctBalance -- Return the player's new account balance
end ProcessGuess
Does the script work as expected ? If there are no specific issues, and it’s just a code review, I note that there is white space separating calls to and declarations of handlers, and their associated parentheses ± arguments , e.g.:
rather than
on GetBet(AcctBalance)
However, this isn’t a problematic issue, as the compiler doesn’t really care one way or the other, though it elects to format compiled code without the white space.
@CJK I have realized that the code in the particular book I am working through is out of date and may be incorrect; so that is why I am asking for help with coding these exercises.
The book I am referencing is called “AppleScript for the Absolute Beginner” by Jerry Lee Ford, Jr.
Last time I tested the code, it wouldn’t compile because of errors.
Edit: I worked through any potential typos and paragraph breaks and, after that, no compile errors.
P.S. This script worked without issues. Here is the finished code
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
----------------------------------------
-- Script Name: AppleScript Shell Game.scpt
-- Version: 1.0
-- Author: Jerry Lee Ford, Jr.
-- Date: August 2007
--
-- Description: This AppleScript game challenges the player to guess under
-- which of three shells the game's virtual ball is hiding.
----------------------------------------
-- Declare the following variable as having a global scope
global GameTitle
-- Define and initialize variable values
set AcctBalance to 10 -- The amount of money in the player's account
set GameTitle to "AppleScript Shell Game" -- Stores the game's name
-- Loop until the player goes broke or decides to quit
repeat until AcctBalance is less than or equal to 0
-- Call the handler that collects the player's bet
set Bet to GetBet(AcctBalance)
-- Analyze the player's input
if Bet = "Quit Game" then -- The player clicked on the Quit Game button
exit repeat
else
-- The player has made a bet
if Bet = "$1" then set Bet to 1
if Bet = "$2" then set Bet to 2
end if
-- Call the handler that generates random numbers
set randomNumber to GetRandomNumber()
-- Call the handler that collect's the player's guess
set Guess to GetGuess()
-- Call the handler that processes the player's guess
set AcctBalance to ProcessGuess(Guess, randomNumber, AcctBalance, Bet)
end repeat
-- To get here, the player has either quit the game or gone broke
if AcctBalance is less than or equal to 0 then
display dialog "Game Over. You have gone broke."
else
display dialog "Please play again soon!" buttons {"OK"} ¬
with title GameTitle
end if
-- This handler generates a random number between 1 and 3 representing
-- the shell under which the ball is hidden
on GetRandomNumber()
-- Generate a random number between 1 and 3
set randomNo to random number from 1 to 3
return randomNo -- Return the random number to the calling statement
end GetRandomNumber
-- This handler prompts the player to make a bet or to quit
on GetBet(AcctBalance)
-- Prompt the player for the size of her bet or to Quit
set Amount to button returned of (display dialog "You have $" & ¬
AcctBalance & "in your account." & return & return & ¬
"How much would you like to bet?" buttons {"$1", "$2", "Quit Game"} with title GameTitle)
return Amount -- Return the player's selection
end GetBet
-- This handler prompts the player to guess which shell the ball is under
on GetGuess()
-- Prompt the player which shell the ball is under
set reply to button returned of (display dialog "OK, now that you" & ¬
"have made your bet, it is" & ¬
"time to make a guess. Which shell do you think the" & ¬
"ball is under?" buttons {"Shell 1", "Shell 2", "Shell 3"} ¬
with title GameTitle)
-- Translate the player's selection into a number
if reply = "Shell 1" then
set reply to 1
else if reply = "Shell 2" then
set reply to 2
else if reply = "Shell 3" then
set reply to 3
end if
return reply -- Return a number representing the player's guess
end GetGuess
-- This handler processes the player's guess and determines whether the
-- player has won or lost
on ProcessGuess(Guess, randomNumber, AcctBalance, Bet)
-- Determine if the player's guess was right or wrong
if Guess = randomNumber then -- The player guess was correct
set AcctBalance to AcctBalance + Bet -- Adjust the player's account
display dialog "Correct! You win $" & Bet ¬
buttons {"Ok"} with title GameTitle
else -- The player's guess was not correct
set AcctBalance to AcctBalance - Bet -- Adjust the player's account
display dialog "Incorrect! You lose $" & Bet & ¬
". The ball was under Shell" & ¬
randomNumber & "." buttons {"OK"} with title GameTitle
end if
return AcctBalance -- Return the player's new account balance
end ProcessGuess