What you are looking for is arrays.
First you declare your arrays (call them array[2] and lastarray[2].
In your loop, you first make lastarray equal to array (for i= 0 to 2)
Then you look for button changes. If a button is changed, the number will be the new array[2], array[1] will be lastarray[2] and array[0] will be lastarray[1]
There are probably more elegant ways to do this, but if you are not running too much other calculations, it should work.