Visual Memory Game

I poted a topic like this in the OSX Forum, but I think that my question would be better suited here.

I have been on this forum for a while learning how to program memory games in applescript, and have found great help from the forum’s members: all in this following thread:

I am looking to write another memory game in script but this time it is a spatial memory game. I’m looking for the program to do this: have some type of grid (say for the example 6 x 6) that displays red squares randomly and individually (but no square can be displayed again) for how many times you choose, and then after all is displayed, you are asked to repeat the process but in reverse order and (here is the real kicker) everything turned either clockwise a quarter turn, half turn, three-quarter turn, vertical flip (vertical mirror), or horizontal flip (horizontal mirror). After you do this process, the program tells you if you are correct.

I was advised that Studio in the Xcode package would do this type of thing, but it would be difficult to write. I badly want to get this program done, but I don’t know how to start/go-about it. The examples in the Xcode package don’t seem to help (from what I can tell).

Could someone help me?


How difficult it is depends.

There must be many ways to do this, but as a beginning I created a 6X6 matrix of square buttons and a button. When the button, named “button1”, is clicked, a random sequence of integers is created. The button cells are then highlighted in this random order. Here’s the test script:

property new_seq : {}
on clicked theObject
	set n to name of theObject
	if n is "button1" then
		set new_seq to GetRandSeq(36)
		repeat with i from 1 to 36
			set cell_index to item i of new_seq
			set the_cell to (first cell of matrix "matrix" of window "main" whose tag is (cell_index - 1))
			set highlighted of the_cell to true
			delay 1
			set highlighted of the_cell to false
		end repeat
	end if
end clicked
-- returns a sequence, of integers from 1 to n
on GetRandSeq(n)
	set l to {}
	repeat with i from 1 to n
		set end of l to i
	end repeat
	set return_list to {}
	repeat with i from n to 1 by -1
		set r to (random number from 1 to i)
		set end of return_list to item r of l
		set item r of l to missing value
		set l to integers of l
	end repeat
	return return_list
end GetRandSeq

Obviously, this can be modified in many different ways.

Note that to create a matrix in IB, press Option while dragging items from the Cocoa Views palette. When you create matrices in this manner, their ‘tag’ properties are set to zero-based indices.

Edited: edited the script.

I don’t think I understand it. I created a studio program called “visual memory” and I added the script you wrote and then under the “main.nib” add the “button.” But when I clicked “build and go” a window appeared with one button and would not do anything else when I tried to click it. I am obviously missing something here. What am I doing wrong?


After you drag the square button to the window, press Option and drag one of the side handles of the button. This creates a matrix of buttons.

After you have your matrix, name everything. To name an object, go to the info palette and select the AppleScript pane from the popup menu. I named my matrix “matrix” and the window “main”.

The AppleScript pane is also where you connect the handlers to the script. After you add a button to the window, go to the AppleScript pane, name the button, check the ‘clicked’ handler, and connect it to the script. When you do this, a ‘clicked’ handler is created in you script and you can edit it. Save changes first. The created handler looks like this:

on clicked theObject
(Add your script here.)
end clicked

theObject is a reference to the button when you click it. In my starter script I didn’t use it.

Hope this gets you started better. Write back with your progress.


or what about using a table view with button cells as game board? I’d suggest using a 2-dimensional array as datasource so you can easily do the flipped/mirrored data comparisons. Another advantage (in my eyes) is, that you can easily adapt the script for other game board sizes.

here’s how I would start (this example is creating random 6 x 6 boards):

  • add a table view of the size 140 x 134 pixel in IB
  • set row height to 20
  • add 6 table columns, each 20 pixels wide
  • drag an NSButtonCell to each of the columns
  • set the button type of all button cells to “small square button” and it’s behavior to on/off
  • set the identifier of the columns to numbers from 1 to 6
  • uncheck all table view options at ‘Display:’ and ‘Allow:’

connect the table view to the following applescript handlers:

  • on number of rows
  • on cell value
  • on change cell value

add a button and connect it to an on clicked handler

then this script should work to create random 6 x 6 boards:

property datasourcearray : {{0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}}

on clicked theObject
	set datasourcearray to my GetRandSeq(6)
	tell table view "tv" of scroll view "sv" of window "main" to update
end clicked

on number of rows theObject
	return 6
end number of rows

on cell value theObject row theRow table column theColumn
	set retVal to (item theRow of item (identifier of theColumn as integer) of datasourcearray)
	return retVal
end cell value

on change cell value theObject row theRow table column tableColumn value theValue
	set (item theRow of item (identifier of tableColumn as integer) of datasourcearray) to theValue
end change cell value

on GetRandSeq(theSize)
	set retVal to {}
	repeat with theRow from 1 to theSize
		set rowVal to {}
		repeat with theCol from 1 to theSize
			set rowVal to rowVal & ((random number) as integer)
		end repeat
		set retVal to retVal & {rowVal}
	end repeat
	return retVal
end GetRandSeq

The only not so nice thing about this way would be the visual row selection in the table view. But this could be removed in a NSTableview subclass.

Tell me if you are interested in this solution and need futher help …


Just got another idea for a solution for this problem:

  • check the option ‘Allow’ → ‘Empty Selection’ of the table view

and add:

	set selected rows of theObject to {}

in the ‘on change cell value’-Handler

Then only a short highlight of the row selection remains …

I am having touble naming everything. I clicked the applescript symbol from the main palette menu and it shows a blue block. I could name the individual buttons just by double clicking them, but I doubt this is want you were explaining to me to do, and I couldn’t figure out how to name the matrix and window. Since I couldn’t name anything, I could do the handlers as well.

This is interesting. I could add the NSTableView from the data section of the cocoa palette menu, and manually change the size of it by clicking option and dragging the mouse, but I couldn’t tell how to set the actual size of the table view.

Also, are all the things that I am supposed to do with the buttons - like setting what type and in what columns etc - part of some menu that I’m too inept to find?


might it be that you confused ‘name’ and ‘title’ ?

After creating the matrix you

  • select it with a single mouse click, then …

  • enter Apple+8 - a window opens (if it was not already open) titeled ‘NSMAtrix Inspector’ - the first pulldown menu should read ‘AppleScript’ - enter the desired name for the Matrix in the text field below (labeled ‘Name’)

  • doubleclick one of the button cells (somewhat closer to it’s lower edge so that it is completely highlited and NO text view is displayed) - the title of the inspector window should now be ‘Button Cell Inspector’

  • name it as described above

  • connect it to the ‘on clicked’ handler in the same window (check: Event Handlers → Action → clicked AND check the AppleScript for this handler - listed in the Script text view in the bottom of this window)

procede with these steps for all UI elements you want to have named/connected

hope this helps,


As for the size of a table view (and any other UI element) - it is set in the inspector - in the size tab (Apple + 3)

And here’s how to build the Table View in IB:

  • select the table view by selecting it’s scroll view + double clicking (The inspector now should be titeled ‘NSTableView Inspector’)
  • drag & drop an NSButtonCell to the first table coumn (you find the button cell in the same tab as the table view)
  • now single-click the header of this column (it should now show a white triangle)
  • here you can modify the column width to 20 (Inspector - tab 'Size or Apple+3)
  • now you can modify the button type of this column’s button cells in the inspector (first tab ‘Attributes’ or Apple+1)

I hope this description will lead to success …
if you 're interested, i can mail you the test project - just send me a mail I can answer to …