
-----------------------------------
noname96
Tue Jan 22, 2013 1:29 pm

How to match two cards in my game
-----------------------------------
I'm making a concentration game. I have all 16 cards randomly placed in my grid. There are 8 pairs of the same picture. I was wondering how to check to see if the two cards I click on are the same. If they are the same they would stay flipped up, otherwise they will return to their face down position.

-----------------------------------
Insectoid
Tue Jan 22, 2013 1:49 pm

RE:How to match two cards in my game
-----------------------------------
This is going to require proper structuring in your program. Every card is going to need a couple of variables. A face value (hint: the picture on the card does not matter at all) and a value representing if the card is face-up or face-down. If you have this set up, it's very easy to check if two selected cards are the same, and even easier to make sure the cards that are flipped up stay flipped up.

-----------------------------------
noname96
Tue Jan 22, 2013 5:00 pm

Re: How to match two cards in my game
-----------------------------------
thanks for the reply. this is what i have so far...
I'm still going blank on how to check if the two cards you click on are the same. im assuming you would have to do something with the x and y coordinates? 



setscreen ("graphics:1201;601;cursor;nobuttonbar")
View.Set ("offscreenonly")



Draw.FillBox (0, 1, 1203, 600, 7)



var font1 : int
font1 := Font.New ("serif:16")
assert font1 > 0
Font.Draw ("CLICK HERE TO START", 737, 318, font1, white)



var match:array 1..2 of int
var stream : int
var word : string
var board : array 1 .. 4, 1 .. 4 of string
var select : array 1 .. 4, 1 .. 4 of boolean
var pic : array 1 .. 16 of int
var X : int
var Y : int
var X2 : int
var Y2 : int
var array2 : array 1 .. 16 of int
for r : 1 .. 4
    for c : 1 .. 4

        select (r, c) := true

    end for
end for
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%PUT LETTERS IN CARDS%%%%%%%%%%%%%%%%%%%%%%%

procedure letter
    open : stream, "sequence.txt", get, seek
    Y := 12
    for r : 1 .. 4
        X := 8
        for c : 1 .. 4
            get : stream, skip
            exit when eof (stream)
            get : stream, board (r, c)
            locate (X, Y)
            put board (r, c)
            X += 8

        end for
        Y += 16
    end for
    close : stream
end letter



pic (1) := Pic.FileNew ("ORANGE.jpg")
pic (2) := Pic.FileNew ("WATERMELON.jpg")
pic (3) := Pic.FileNew ("STRAWBERRIES.jpg")
pic (4) := Pic.FileNew ("PUMPKIN.jpg")
pic (5) := Pic.FileNew ("BANANA.jpg")
pic (6) := Pic.FileNew ("APPLE.jpg")
pic (7) := Pic.FileNew ("GRAPES.jpg")
pic (8) := Pic.FileNew ("MANGO.jpg")
pic (9) := Pic.FileNew ("ORANGE.jpg")
pic (10) := Pic.FileNew ("WATERMELON.jpg")
pic (11) := Pic.FileNew ("STRAWBERRIES.jpg")
pic (12) := Pic.FileNew ("PUMPKIN.jpg")
pic (13) := Pic.FileNew ("BANANA.jpg")
pic (14) := Pic.FileNew ("APPLE.jpg")
pic (15) := Pic.FileNew ("GRAPES.jpg")
pic (16) := Pic.FileNew ("MANGO.jpg")

for i : 1 .. 16
    array2 (i) := i
end for

procedure shuffle
    var Temp, R : int
    for e : 1 .. 3
        for i : 1 .. 16
            R := Rand.Int (1, 16)
            Temp := array2 (R)
            array2 (R) := array2 (i)
            array2 (i) := Temp
        end for
    end for
end shuffle



var temp : array 1 .. 2 of int
var x, y, button : int
var click : boolean := true
procedure fill
    var counter : int := 0
    var x2, x3, y2, y3 : int
    y2 := 543
    y3 := 420
    for i : 1 .. 4
        x2 := 30
        x3 := 150
        for j : 1 .. 4
            counter += 1
            if select (i, j) = true then
                Draw.FillBox (x2, y2, x3, y3, red)
            end if
            if select (i, j) = false then
                Draw.FillBox (x2, y2, x3, y3, white)
                Pic.Draw (pic (array2 (counter)), x2, y3, picCopy)
                
                Draw.Box (x2, y2, x3, y3, 3)
            end if
            
            
            x2 += 130
            x3 += 130
        end for
        y2 -= 130
        y3 -= 130
    end for
end fill

procedure find
    var x2, y2 : int := 0
    Mouse.Where (x, y, button)
    for r : 1 .. 4
        x2 := 0
        for c : 1 .. 4
            if x > 30 + x2 and x < 150 + x2 and y < 542 + y2 and y > 420 + y2 and button not= 0 then
                select (r, c) := false
            end if
            x2 += 130
        end for
        y2 -= 130
    end for
end find








%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%only click on two cards at a time%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

shuffle
loop
    Mouse.Where (x, y, button)
    Text.Locate (1, 1)
    if button = 0 then
        put x : 4, "  ", y : 4, "  button up"
    else
        put x : 4, "  ", y : 4, "  button down"
    end if
    for i : 1 .. 2
        loop
            Mouse.Where (x, y, button)
            exit when button not= 0
        end loop
        find
        fill
        View.Update
        loop
            Mouse.Where (x, y, button)
            exit when button = 0
        end loop
    end for

    for i : 1 .. 2

    end for


    delay (1000)

    for i : 1 .. 4
        for j : 1 .. 4
            select (i, j) := true
        end for
    end for
    fill
    View.Update
end loop

-----------------------------------
Insectoid
Tue Jan 22, 2013 5:21 pm

RE:How to match two cards in my game
-----------------------------------
[code]var array2 : array 1 .. 16 of int [/code]

What is this? You need to name your variables better. 

You should have a 2-D array that keeps track of all the cards on the table. Let's call it 'table'. Write a procedure that does nothing except draw the cards. To see if a card should be drawn face-up, check if the card at table(x,y) (cards should already have this information, as per my first post) is flipped up or down. Based on that, either draw it up or down.

To check if a card is equal, compare the value of the selected cards. Do your cards have values? For example, cards with oranges have a value of 1. Cards with watermelons have a value of 2, etc. Then you just need to check if the value of card (x,y) is the same as the value of card (other_x, other_y).

-----------------------------------
noname96
Tue Jan 22, 2013 6:26 pm

Re: How to match two cards in my game
-----------------------------------
ok, but the thing is my x,y coordinates for each card changes everytime, since its randomly arranged. so i cant say if it clicks on this specific x,y location and this specific x,y location then they stay flipped. 

array2 is just an array that randomizes the numbers from 1 to 16. then i put pic(array2) to randomize the pictures.

-----------------------------------
Insectoid
Tue Jan 22, 2013 7:14 pm

RE:How to match two cards in my game
-----------------------------------
This is a fundamental flaw in your design, and you're going to have to change it to make it work.

-----------------------------------
noname96
Tue Jan 22, 2013 8:38 pm

Re: How to match two cards in my game
-----------------------------------
And what exactly do I have to change?

-----------------------------------
Insectoid
Tue Jan 22, 2013 9:01 pm

RE:How to match two cards in my game
-----------------------------------
Maybe you should adjust your shuffle procedure. I notice you have 16 values in it. Since you have 2 of each card, why not have 8 values represented twice? Then once two cards are picked, simply check if the value is the same?

-----------------------------------
noname96
Fri Jan 25, 2013 7:01 pm

Re: How to match two cards in my game
-----------------------------------
my problem comes with the 'when you click on two cards, match the values'. I dont understand how to make the program know that when i click on a card, it should return the value of the picture that i click on. right now, all it knows is that when i click on a certain set coordinate range, it should draw a random picture over it. therefore i cant say something like 'if  i click on picture 1 and then picture 9, then that is a match' as it does not know where picture 1 or picture 9 is. I really hope you understand what my dilemma is, and i GREATLY hope that you can help me resolve it, being that my game is due monday :(

-----------------------------------
Insectoid
Fri Jan 25, 2013 7:19 pm

RE:How to match two cards in my game
-----------------------------------
You need to decide at the beginning of the game, before any cards have been clicked, where every single card is. I suggest you use a 2D array to hold the values of each card, where each index in the array represents the coordinates of the card stored at that index. Do not think in terms of pictures. The picture doesn't matter. I don't care about the picture. If you know where every card is, then drawing the right picture when a card is clicked will be no problem. For example:


var pictures: array 1..8 of int //store ONE copy of each picture in this array.
var table_of_values: array 1..4, 1..4 of int //Store card values in this array
var table_of_images : array 1..4, 1..4 of int //Store your shuffled cards here

var x, y : int //we'll use these to store some temporary random values

for i : 1..4 //initialize all the values to 0 in this loop
    for j : 1..4
        table_of_values(i,j) := 0
    end for
end for

//this block assigns locations to cards
for i : 1..2 //we want 2 copies of each card, so do it twice.
    for j : 1..8 //assign a spot to each card
        loop //now we assign cards to random locations
            x := Rand.Int (1,4)
            y := Rand.Int(1,4)
            exit when table_of_values (x,y) = 0//make sure the spot we've picked is empty
        end loop
        table_of_values(x,y) := j
        table_of_images(x,y) := pictures(j)
    end for
end for


Now you have a couple of really handy arrays that know exactly where each card is. You'll need to convert your pixel coordinates for buttons to array coordinates for...the arrays with some math, but you can figure that out yourself. Once you've got the array coordinates,  you just need to check if table_of_values(x1,y1) = table_of_values(x2,y2). If they're the same, then it's the same card. 

I warn you though; in order to make this work, you'll have to throw out almost all of your existing code. That sucks, but that's how it is. You won't get it working otherwise.

-----------------------------------
evildaddy911
Sat Jan 26, 2013 9:59 am

RE:How to match two cards in my game
-----------------------------------
@insectoid: in Turing, we use "%" to start a comment, not "//"

The shuffling algorithm i use is much faster; 
the 1D array version i use goes like this:

for decreasing i : upper(array) .. lower(array)
    swap (array (i), array (Rand.Int (lower (array), i))
end for

you can easily modify that for use with multidimensional arrays by using multiple "for decreasing..." loops

-----------------------------------
Insectoid
Sat Jan 26, 2013 1:03 pm

RE:How to match two cards in my game
-----------------------------------
@insectoid: in Turing, we use "%" to start a comment, not "//"  Oops, my bad. This was bound to happen eventually, since I can't actually test my Turing code.

As for my shuffling algorithm- I made that up on the spot. I've never bothered to write a proper shuffling algorithm since I only rarely shuffle things. Yours looks much better than mine. At least it's guaranteed to terminate. Mine could theoretically never finish shuffling.
