Newbie needs help with script (display randomized numbers)

I’d like to see this done in AppleStudio Studio. Besides that, I think the code is good.

Hi ATST. In your number version, variables like errorList and others aren’t used, so instead I’ve used a tally system to keep track of the score ( theScore ).
Since I’ve only changed play_game(), you’ll need to copy and paste this into a copy of your script, replacing the old function.

to play_game()
	set {showTime, numLength} to memVal
	set roundNum to 1
	set theScore to 0
	repeat
		display dialog "Ready to start?" with title "Round " & roundNum
		set w to ""
		set n to ""
		tell "0123456789" to repeat numLength times -- faster: {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
			set w to w & some character
			set n to n & some character
		end repeat
		display dialog (return & tab & tab & tab & tab & w & " times " & n) ¬
			with title ("Round " & roundNum) buttons "Cancel" giving up after showTime
		set inputAnswer to text returned of (display dialog "Please type the answer: " default answer "")
		set correctAnswer to w * n as string
		if inputAnswer is correctAnswer then
			set theScore to theScore + 1
			set dialogText to "Correct!"
		else
			set dialogText to "Incorrect"
		end if
		set dialogText to dialogText & return & w & " times " & n & " is " & correctAnswer
		set roundNum to roundNum + 1
		display dialog dialogText with title ("Score: " & theScore) buttons {"Cancel", ("Round " & roundNum & ".")} default button 2
	end repeat
end play_game

That’s great, thanks for you help.

Hi Guys! I haven’t posted for a while. Been really busy. I have a new memory game I am trying to program called “n-Back.”

The idea is that you have to recall a word from a list, and you have a choice to pick how many word positions back you must recall said word, where words are randomly displayed on the screen.

So for example, you have a list of five words individually displayed: “cat”, “dog”, “ball”, “car”, “pipe”. You chose earlier in the menu that you would try to recall the word two positions back from the last word, hence 2-Back. That word would be “ball”.

I came up with this script which doesn’t work. It doesn’t display the individual words, and it can’t make sense of which word, which is to be the correct answer, is positioned from the last word in the displayed list.


property theWords : paragraphs of (do shell script "/usr/shore/dict/words | tr \"A-Z\" \"a-z\"")
property timeLimit : 2
property rep : 20
property nWord : 3

repeat
	set theList to a reference to theWords
	
	choose from list {10, 15, 20, 25, 30} with prompt "How long is the word-list?" default items {rep}
	if result is not false then
		set rep to result as integer
	else
		error number -128 -- User canceled
	end if
	
	choose from list {2, 3, 4, 5, 6} with prompt "How far back do you want to recall?" default items {nWord}
	if result is not false then
		set nWord to result as integer
	else
		error number -128 -- User canceled
	end if
	
	display dialog "Testing will begin after the beeps."
	delay 2
	beep 2
	
	set testList to {}
	repeat rep times
		set testList's end to some item in theList
		display dialog (result) buttons {"Cancel"} giving up after timeLimit
	end repeat
	
	delay 1
	display dialog "Please enter the " & nWord & "-" & "Word:" default answer "" buttons {"Cancel", "Done"} default button 2
	set answerWord to text returned of result
	set lastWord to last item of testList
	set nBack to item (lastWord - nWord) in testList
	
	set errorCount to 0
	if item answerWord ≠ nBack then set errorCount to errorCount + 1
	
	display dialog "You made " & errorCount & " errors." buttons {"View Answer", "Quit", "Retry"} default button 3
	set nextAction to button returned of result
	
	if result is "View Answer" then
		display dialog ("Correct Answer: " & nBack & return & "Your answer: " & answerWord) buttons {"Quit", "Retry"}
		set nextAction to button returned of result
	end if
	
	if nextAction is "Quit" then exit repeat
end repeat

Can you help me?

Perhaps something like this might get you closer, ATST:

property theWords : read POSIX file ("/usr/share" & "/dict/words")
property timeLimit : 2
property rep : 20
property nWord : 3

to choose_item(l, p, i)
	tell (choose from list l with prompt p default items i) to if it is not false then return item 1
	error number -128
end choose_item

set errorCount to 0
repeat
	set rep to choose_item({10, 15, 20, 25, 30}, "How long is the word-list?", rep)
	set nWord to choose_item({2, 3, 4, 5, 6}, "How far back do you want to recall?", nWord)
	display dialog "Testing will begin after the beeps."
	delay 2
	beep 2
	set testList to {}
	repeat rep times
		set testList's end to some paragraph of theWords
		display dialog result buttons {"Cancel"} giving up after timeLimit
	end repeat
	set nBack to testList's item -nWord
	delay 1
	set answerWord to text returned of (display dialog "Please enter the " & nWord & ¬
		"-" & "Word:" default answer "" buttons {"Cancel", "Done"} default button 2)
	if answerWord is nBack then
		set testResult to "is. correct!" & return & return & "Well done!"
	else
		set testResult to "should have been: " & nBack & "." & return & return & "Bad luck!"
		set errorCount to errorCount + 1
	end if
	if button returned of (display dialog "Your answer was: " & answerWord & "." & ¬
		return & "That " & testResult & return & return & "Errors so far: " & errorCount ¬
		buttons {"Quit", "Retry"} default button 2) is "Quit" then exit repeat
end repeat

Hey that’s it exactly. Thanks for the help Kai!