Computer Science Canada

Checking if pieces are near each other

Author:  cycro1234 [ Tue Jan 18, 2005 6:29 pm ]
Post subject:  Checking if pieces are near each other

Hello. I'm making a Connect 4 game, and I'm having MAJOR problems programing the check. The check basically checks to see if there are 4 of one colour in a row. I've used whatdotcolour, but it does not work very well. I've been told that an array might work, but I can't seem to figure out how that would work. Any help would be GREATLY appreciated. Laughing
Thanks in advance.

Author:  xHoly-Divinity [ Tue Jan 18, 2005 6:35 pm ]
Post subject: 

You've got to show us what you've got so far, and you have to be more specific as to what you need help in an array. You can always read the tuturial section about arrays as well

Author:  OsoInsane_abh [ Tue Jan 18, 2005 6:37 pm ]
Post subject: 

I dont know if you realise this or not but cyro (yes i figured out who you were with your "smart" idea to make connect 4) you did not post your code so i should wonder how peopke should help you

Author:  cycro1234 [ Tue Jan 18, 2005 6:44 pm ]
Post subject: 

Well, I've done the check wit whatdotcolour, as I mentioned. Here's how it looks:

code:
if whatdotcolour (x, y) = 12 and whatdotcolour (x + 60, y) = 12 and whatdotcolour (x + 120, y) = 12 and whatdotcolour (x + 180, y) = 12 then


As for the arrays, I've read the tuts, but how could i exactly incorporate it into my check?

Author:  cycro1234 [ Tue Jan 18, 2005 7:44 pm ]
Post subject: 

Will that suffice?

Author:  Bacchus [ Tue Jan 18, 2005 7:52 pm ]
Post subject: 

using array (for all i kno) will still require a lot of ifs but it might be easier if u use a chart like this
code:
var tmp:array 1..5,1..5 of int
x=clr num

          1          2          3          4          5

1        x          x          x          x          x

2        x          x          x          x          x

3        x          x          x          x          x

4        x          x          x          x          x

5        x          x          x          x          x

and use that to check

Author:  cycro1234 [ Tue Jan 18, 2005 7:57 pm ]
Post subject: 

Is the grid u made for the rows and columns in connect 4? Although I've read the tutorial on arrays, i must admit i still only know how to create them and call upon variables made. I can't realy manipulate them well. Embarassed Any further help would be appreciated.

Author:  Cervantes [ Tue Jan 18, 2005 8:00 pm ]
Post subject: 

What you're probably going to want to do (aside from learning to use arrays as easily as normal variables) is have a 2D array, for your grid. Have the value of the grid represent something. Say, 0 for player 1, 1 for player 2, and -1 for empty. Then, check through every spot on your grid (using two for loops) to see if there are four in a row in any direction from that spot (using a bunch of for loops). It would be best if you made this a function, so you can take advantage of quitting the function as soon as a four in a row is found.
-Cervantes

Author:  Bacchus [ Tue Jan 18, 2005 8:01 pm ]
Post subject: 

ye those would represent the column and rows ex) tmp(1,1) would be the left hand top corner, tmp(5,5) would be the bottom right hand corner, that right there is a 2d array! good way to keep more info in one var lol and u can use fors to access them easily

Author:  cycro1234 [ Tue Jan 18, 2005 8:17 pm ]
Post subject: 

I sort of understand it better now. Here is the code that draws my circles in a grid:

code:
setscreen ("graphics:max;max")
for x : 100 .. 490 by 60
        for X : 100 .. 460 by 70
            drawfilloval (x + 20, X + 20, 24, 24, 43)
            drawoval (x + 20, X + 20, 24, 24, black)
            drawarc (x + 20, X + 20, 23, 23, 80, 280, black)
            drawarc (x + 20, X + 20, 22, 22, 120, 240, black)
            drawarc (x + 20, X + 20, 21, 21, 120, 240, black)
            drawfilloval (x + 39, 521, 24, 7, 43)
            drawoval (x + 39, 521, 24, 7, black)
        end for
    end for


So does my 2D array have the values of x and y as the variables?

Author:  Cervantes [ Tue Jan 18, 2005 8:21 pm ]
Post subject: 

The real location of any element of the 2D array is equal to (element1, element2). So, the location of the value at grid (4, 3) is (4,3). So, if grid (4,3) = 1, (using a little interpretation), there is a piece for player2 at (4,3). Keep in mind, however, that I'm talking about location on a grid, not on the screen.

Author:  cycro1234 [ Tue Jan 18, 2005 8:35 pm ]
Post subject: 

So basically i should overlay the array on my circles, and set the value to 1 if a section is filled with a player 1 piece and value to 2 if its a player 2 piece?

Author:  cycro1234 [ Tue Jan 18, 2005 9:47 pm ]
Post subject: 

Ok, i think i prety much got that figured out now. However, i hit the same problem I had before. See, to make the animation of the piece falling i animate a circle falling down, but left the x a variable. Since, the drop is always the same, i could leave the y coordinates the same, and only have to change the x. To make the y decrease, i put it in a for loop. So it would look something like this:

procedure dropball (x:int)
for y : 470..30 by 70
drawoval (x,y+70,20,20,7)
delay (100)
drawoval (x,y,20,20,7)
end for
end dropball

I also have to call upon a check procedure in there that checks for 4 in a row. However, the check requires 2 values, an x and a y. The x for the horizontal positioning of the ball, and y for the final RESTING place of the ball. If i do the check outside of the for loop, the y value is no longer accesible, and if i do the check inside the for loop, the check is done for every y value. I tried using a variable, but i get the same result. Any ideas?

Author:  Bacchus [ Tue Jan 18, 2005 9:52 pm ]
Post subject: 

well theres only a certain amount of rows, and each row has the same y variable... so all you have to do is check for what row the piece is in

Author:  cycro1234 [ Tue Jan 18, 2005 9:54 pm ]
Post subject: 

Yes, but how bout its position on the y axis? What if it's at the top? or in the middle?

Author:  Bacchus [ Tue Jan 18, 2005 9:57 pm ]
Post subject: 

thats wat i said, for how ever many rows you have there a common y variable for each one. im sure you have a check somewhere to see how far it can drop, and when you get the row its in set the y

Author:  cycro1234 [ Tue Jan 18, 2005 10:04 pm ]
Post subject: 

The check to see how far it can drop consists of checking the colour of the pixel of the space right underneath it. If its red or blue, my piece colours, then dont draw the next piece.

Which is exactly my problem. My check is ok! But the check is done at ever space that the piece falls through. I need it to check only AFTER the piece fell. !!! Please help me!

Author:  Bacchus [ Tue Jan 18, 2005 10:38 pm ]
Post subject: 

here to get better help add your program as an attachment and we can see wat we can do, talking only goes so far

Author:  cycro1234 [ Tue Jan 18, 2005 10:58 pm ]
Post subject: 

Ok, will do.

Author:  Bacchus [ Tue Jan 18, 2005 11:22 pm ]
Post subject: 

hm.. leave a bit of the circle when you drop it, some went right thru, and some nocked the bottom one out lol. ok for the array thing. instead of just using 1 x and y variable use the array and when you drop it use the array there

Author:  cycro1234 [ Tue Jan 18, 2005 11:35 pm ]
Post subject: 

OOPS! That was the wrong one. I fixed the error in this one. And by using the array to check, do u mean every time a ball falls in a section, change that section's value to something else?

But, I think that if i can fix my problem, the fact that it checks at every space, i can make it work. I think I just have to move that check (x,y) somewhere else...

Author:  Bacchus [ Wed Jan 19, 2005 7:51 am ]
Post subject: 

fixed the pieces going theu, but they still leave a bit after when you drop it (like an outline right at the top) also when moving the arrow, y not make it move to the next space instead of having to guess if you inbetween or not?

Author:  cycro1234 [ Wed Jan 19, 2005 8:25 am ]
Post subject: 

I was thinking of doing what u suggested, I wanted to leave a little area that's in between. I'll see what I can do. As for my problem wit the checking OUTSIDE of the loop, im still stuck! Hit Wall

Author:  cycro1234 [ Wed Jan 19, 2005 3:05 pm ]
Post subject: 

Any suggestions?

Author:  cycro1234 [ Wed Jan 19, 2005 8:08 pm ]
Post subject: 

Here is the code with my problem :

code:
procedure dropBall (x : int)
    if playerTurn = 2 then
        loop
            y := y - 70
            if whatdotcolour (x, y) not= 12 and whatdotcolour (x, y) not= 9 then
                drawfilloval (x, y + 70, 24, 24, 43)
                drawoval (x, y + 70, 24, 24, black)
                drawarc (x, y + 70, 23, 23, 80, 280, black)
                drawarc (x, y + 70, 22, 22, 120, 240, black)
                drawarc (x, y + 70, 21, 21, 120, 240, black)
                delay (75)
                drawfilloval (x, y, 23, 23, brightblue)
                View.Update
                check (x, y)
            end if
            exit when y < 130

        end loop

        y := 540
        playerTurn := 1
    else
        loop
            y := y - 70
            if whatdotcolour (x, y) not= 12 and whatdotcolour (x, y) not= 9 then
                drawfilloval (x, y, 23, 23, brightred)
                drawfilloval (x, y + 70, 24, 24, 43)
                drawoval (x, y + 70, 24, 24, black)
                drawarc (x, y + 70, 23, 23, 80, 280, black)
                drawarc (x, y + 70, 22, 22, 120, 240, black)
                drawarc (x, y + 70, 21, 21, 120, 240, black)
                delay (75)
                drawfilloval (x, y, 23, 23, brightred)
                View.Update
                check (x, y)
            end if
            exit when y < 130
        end loop

        playerTurn := 2
        y := 540
    end if
end dropBall


As you can see, the check is inside the loop. This means that it will execute every time the value of y decreases. However, if I take it outside of the loop, the check either doesn't work at all, or at the wrong times. Any ideas to fix this would be greatly appreciated.

Thanks in advance

Author:  Cervantes [ Wed Jan 19, 2005 8:11 pm ]
Post subject: 

Okay, you've got your drop procedure, with some dandy graphics. You'll have a procedure (or section of code) to make sure the ball stops at the correct place. You shouldn't use whatdotcolour anywhere in this code, or this program. It would be just as easy to use an if statement and a for loop to find the last row (actually, a function would make things really easy here). For checking for a victory, go through every spot on the grid and check all around it, in every direction. Who cares if it's not efficient, it's connect four, not a real time strategy game.
code:

for x : 1 .. gridX
  for y : 1 .. gridY
    if checkVictory (x, y) = true then
      put "yay"
    end if
  end for
end for

In which case, checkVictory is a function that returns a boolean value if there are four in a row (or column or diagonal) of the same piece from (x,y).

Author:  cycro1234 [ Wed Jan 19, 2005 8:48 pm ]
Post subject: 

But how would I find the last row, if i use a whatdotcolour to control when the ball drops. How would I otherwise control it? How would I use the if statement and for loop to check if there is a piece underneath? Or a piece of the same colour beside it? Do I need an array? Forgive my inaptitude.

Author:  Cervantes [ Wed Jan 19, 2005 10:23 pm ]
Post subject: 

cycro1234 wrote:
But how would I find the last row, if i use a whatdotcolour to control when the ball drops.

I said don't use whatdotcolour.
Seriously, read up on arrays. Read up on 2D arrays. Practice making some simple programs that use arrays and 2D arrays. Then try to make a grid for your connect four game that uses a 2D array. Once you do that, everything should seem clear.

Author:  cycro1234 [ Wed Jan 19, 2005 10:25 pm ]
Post subject: 

U see, I honestly WOULD do exactly that. Try to figure out how arrays work. However, I don't have much time on my hands. And changing my code to use arrays might take a while.

Author:  basketball4ever [ Wed Jan 19, 2005 10:45 pm ]
Post subject: 

suggestion : P and prob the most simple way : P use dots with if statements : Pnewbish pride! Very Happy


: