Blitz Basic Tute #8 - Concentration Required

We're now at the interesting stage - the creative stage - the stage where it is very difficult to tell you what to do. I can tell you the way I got there. You can just copy & paste code and not really learn anything. Basically you need to accept that a lot of mental energy is needed to create a masterpieces on a computer. As Edison said: Genius is 1% inspiration, 99% perspiration.

Obviously the first stage is to know in great detail what you're trying to do. To this end if you're not totally familiar with Concentration, spend some time playing my JavaScript version.

The first thing I asked myself is "How do I know if two cards are a pair?". You could start your brain-straining right away by answering that question before reading on.

A careful study of how the cards are numbered reveals that two cards will be a pair if the difference between them is 13, 26 or 39. So we don't have to worry about positives and negatives we'll use the absolute function, abs(), which just returns the value without the sign. So you could imagine a couple of lines of code like this:

d=Abs(card-last_card)
If d=13 Or d=26 Or d=39 Then we have a pair

From this thinking I realised that I would need to have saved the last card turned over. I also realised that I would need to know whether there was a last card! I could imagine something like:

; suppose the player have just turned over card number n
if last_card=0 then
  last_card=n
else
  if we have a pair then
    remove the two cards
  else
    turn the cards face down
  end if
  last_card=0
end if

So now the question is: "How do I remove a card?". I decided to have an on_table array - each set to true initially.
I modified cards_display() so it checked on_table(n).

If on_table(n) Then
  ...
End If

I also modified cards_which_one() so that it only looked at face down cards.

If mouse_in(x,y,x+74,y+98) Then If Not face_up(n) Then card=n

Hint: Make sure you keep your head straight about the number of a card (ie the box that it is in on the screen) and its value (ie whether it is the Ace of Clubs or the two of hearts etc). I like to use code like:

last_card=cards(last_n)
card=cards(n)

Here's my final program.


Valid HTML 4.0 Strict Valid CSS