Game of memory

writing a game of memory, stuck on how to freeze buttons once a match is
made any help would be appreciated. Yes, this is for school intro to
programming, my code isn’t very eloquent.

require “fox16”
include Fox

class ClickButton < FXButton
def initialize(parent, a, b, num)
super(parent, nil, :width =>100, :height=>80, :x=>a, :y=>b,
:opts=>LAYOUT_FIX_WIDTH|LAYOUT_FIX_HEIGHT|FRAME_RAISED|FRAME_THICK|LAYOUT_FIX_X|LAYOUT_FIX_Y)
@symbol = num

self.connect(SEL_COMMAND) do
  self.parent.list(self)
  if self.parent.check == true
    self.text = @symbol
  end
end

end
end

class MemoryWindow < FXMainWindow
def initialize(app_instance)
super(app_instance,“Memory Game”, :width => 400, :height => 320)

@clicked_list=[]

array1 = [1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8]
array1=array1.shuffle

num=array1.pop
num=num.to_s
@b1=ClickButton.new(self, 0, 0, num)
num=array1.pop
num=num.to_s
@b2=ClickButton.new(self, 0, 80, num)
num=array1.pop
num=num.to_s
@b3=ClickButton.new(self, 0, 160, num)
num=array1.pop
num=num.to_s
@b4=ClickButton.new(self, 0, 240, num)
num=array1.pop
num=num.to_s
@b5=ClickButton.new(self, 100, 0, num)
num=array1.pop
num=num.to_s
@b6=ClickButton.new(self, 100, 80, num)
num=array1.pop
num=num.to_s
@b7=ClickButton.new(self, 100, 160, num)
num=array1.pop
num=num.to_s
@b8=ClickButton.new(self, 100, 240, num)
num=array1.pop
num=num.to_s
@b9=ClickButton.new(self, 200, 0, num)
num=array1.pop
num=num.to_s
@b10=ClickButton.new(self, 200, 80, num)
num=array1.pop
num=num.to_s
@b11=ClickButton.new(self, 200, 160, num)
num=array1.pop
num=num.to_s
@b12=ClickButton.new(self, 200, 240, num)
num=array1.pop
num=num.to_s
@b13=ClickButton.new(self, 300, 0, num)
num=array1.pop
num=num.to_s
@b14=ClickButton.new(self, 300, 80, num)
num=array1.pop
num=num.to_s
@b15=ClickButton.new(self, 300, 160, num)
num=array1.pop
num=num.to_s
@b16=ClickButton.new(self, 300, 240, num)
num=array1.pop
num=num.to_s

end

def list(some_button)
@clicked_list << some_button
puts @clicked_list.inspect
end

def check
if @clicked_list.length > 2
@clicked_list=[]
@b1.text=nil
@b2.text=nil
@b3.text=nil
@b4.text=nil
@b5.text=nil
@b6.text=nil
@b7.text=nil
@b8.text=nil
@b9.text=nil
@b10.text=nil
@b11.text=nil
@b12.text=nil
@b13.text=nil
@b14.text=nil
@b15.text=nil
@b16.text=nil
return false

  else
    return true

end

end

def create
super
self.show(PLACEMENT_SCREEN)
end
end

app=FXApp.new
MemoryWindow.new(app)
app.create
app.run

First thing’s first: it’s easier to theorize when you’re looking at more
concise code. Code is not designed for computers, it’s designed for
programmers (otherwise we’d still be using punchcards). So make your
code clean and clear, even if you’re the only one reading it.

num=array1.pop
num=num.to_s
@b1=ClickButton.new(self, 0, 0, num)
num=array1.pop
num=num.to_s
@b2=ClickButton.new(self, 0, 80, num)

If you’re numbering a set of variables it’s a hint that you should be
using a container of some sort. In this case an array.

No:
@b1 = ?
@b2 = ?

Yes:
@b = []
@b[0] = ?
@b[1] = ?

Reason why:
@b = []
array1.each_with_index do |ary_element, index|
num = ary_element.to_s
x = 100 * (index / 4)
y = 80 * (index % 4)
@b[index] = ClickButton.new(self, x, y, num)
end

#later

@b.each do |thing|
thing.text = nil
end

That should give you the ability to look at what you’re doing far more
easily.

In regards to your question, I don’t know anything about Fox so I can’t
give you a direct answer. I wouldn’t give you code for an assignment
anyway, but I can give you general advice as follows:

You want to impose a specific state on an object. The state can be
either on or off, so it is a ‘boolean’ state. Add a property to the
object’s class:

@matched

and initialize it to false. When the user creates a condition where the
state of that object should be changed, set @matched to true. In the
methods that determine how the object interacts (how to draw it, what to
do when it is clicked, etc) check the value of @matched and react
accordingly.

As a side-note, I don’t see what part of your code handles what to do
when the user clicks on a button, so I don’t really know where that
check would go.

Finally, when posting code that is more than 10 lines or so please save
the code into a text file or rb file and attach it to your post rather
than posting the code in the message body. :slight_smile:

Hope that helps.

I’ve gotta take off for a Christmas party here, but I was staring at
your code there and I saw that ClickButton only has an initializer,
which suggests to me that the ClickButton class is not handling
anything. Again, I don’t know what the Fox stuff does, but the
ClickButton should be responsible for managing a click on itself, even
if it’s simply setting a state in a larger scope. That’s probably a
first step in solving your trouble. One of the principals of good
programming is that an object should handle all of its responsibilities
and none of the responsibilities of anything else. If you’re making a
Class whose objects represent your buttons then that class should be
where the code for handling a click from the user goes.

Merry Christmas! Hoping this helps. :slight_smile: