Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Checking if pieces are near each other
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
cycro1234




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
xHoly-Divinity




PostPosted: Tue Jan 18, 2005 6:35 pm   Post subject: (No 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
OsoInsane_abh




PostPosted: Tue Jan 18, 2005 6:37 pm   Post subject: (No 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
cycro1234




PostPosted: Tue Jan 18, 2005 6:44 pm   Post subject: (No 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?
cycro1234




PostPosted: Tue Jan 18, 2005 7:44 pm   Post subject: (No subject)

Will that suffice?
Bacchus




PostPosted: Tue Jan 18, 2005 7:52 pm   Post subject: (No 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
cycro1234




PostPosted: Tue Jan 18, 2005 7:57 pm   Post subject: (No 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.
Cervantes




PostPosted: Tue Jan 18, 2005 8:00 pm   Post subject: (No 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
Sponsor
Sponsor
Sponsor
sponsor
Bacchus




PostPosted: Tue Jan 18, 2005 8:01 pm   Post subject: (No 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
cycro1234




PostPosted: Tue Jan 18, 2005 8:17 pm   Post subject: (No 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?
Cervantes




PostPosted: Tue Jan 18, 2005 8:21 pm   Post subject: (No 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.
cycro1234




PostPosted: Tue Jan 18, 2005 8:35 pm   Post subject: (No 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?
cycro1234




PostPosted: Tue Jan 18, 2005 9:47 pm   Post subject: (No 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?
Bacchus




PostPosted: Tue Jan 18, 2005 9:52 pm   Post subject: (No 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
cycro1234




PostPosted: Tue Jan 18, 2005 9:54 pm   Post subject: (No subject)

Yes, but how bout its position on the y axis? What if it's at the top? or in the middle?
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 30 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: