Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Checking adjacent objects for similarities

Author
Message
DB_Newbie
16
Years of Service
User Offline
Joined: 4th Aug 2009
Location:
Posted: 23rd Feb 2010 04:20
Hi all,

Umm... I'm trying to make a code where the game can check if 3 objects of the same colour is next to each other.

Basically, you're moving objects around in grids and I want to it check if objects of the same colour are next to each other (vertically or horizontally).

I have a UDT that stores what row, column and colour each object is. But I can't seem to figure out how to check if (for example), 3 blue objects are all in a row.

By the way, there is no collision and I do not intend to make one.

Can anyone help me?

My logic was:

"For each object, check if any objects adjacent to it shares a colour with the original one. If 2 or more objects share the same colour, player loses"

How do I achieve this?

Thanks
deaths judge
19
Years of Service
User Offline
Joined: 4th Jan 2007
Location:
Posted: 23rd Feb 2010 10:50
try checking object numbers instead of colour. just keep the object numbers documented somewhere so you know what object number is what colour.

[[[[wobble]]]] {{{{wobble}}}}
DB_Newbie
16
Years of Service
User Offline
Joined: 4th Aug 2009
Location:
Posted: 23rd Feb 2010 11:59
Yeah that was suggested as a friend. Unfortunately, the objects don't stay in one place since they are moved around.

So, in the beginning when it's set up, the objects are all in order. But after they been moved around, object 7 can be next to 9, can be next to 3 etc.

Oh and in the beginning of the program, the code makes random the random objects and then colours them. So everytime you start program, the colours on each object will always be randomized.
baxslash
Valued Member
Bronze Codemaster
19
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 23rd Feb 2010 14:26
So what you do is set up an array (assuming you have 3x3 grid as an example):


Set the values for each object when they are moved:


Assuming you have 3 colours (1 is red, 2 is Blue and 3 is Green)...

Then you check each row for 2 or more of the same colour in a row:


...and call the function as follows:


win=1 there are 2 or more in a row.
win=0 there aren't...

Van B
Moderator
23
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 23rd Feb 2010 15:49 Edited at: 23rd Feb 2010 16:01
I'd do what Haxslash is doing, however only for a small footprint of 3x3. I think you'll probably have a bigger grid to worry about - so I'd do something like this - lets assume a 16x16 grid and 5 different colours numbered 0 to 4

fail=1

For c=0 to 4
For y=0 to 15
count=0
For x=0 to 15
If cell(x,y)=c then inc count,1 else count=0
If count=3 then fail=0
Next x
Next y
Next c

So that would scan the cell array for C - if the cell colour is C, then that's a match - and increment the count, otherwise reset the count. If the count adds up to 3, and you want to remove rows of 4 5 etc, then perhaps just set the count to 2, so if the next colour is the same again, the count is back at 3, removing the cell or whatever, kinda nibbling at the grid if you know what I mean....

If count=3 then fail=0 : count=2

Thats only for left to right though, if you want to scan up and down to, duplicate that and switch around the For Next loops - so Y increments in the shortest loop instead of X...

For c=0 to 4
For x=0 to 15
count=0
For y=0 to 15
If cell(x,y)=c then inc count,1 else count=0
If count=3 then fail=0
Next y
Next x
Next c

To clear the cells for instance, I'd take the current position in the scan, and clear the current cell and the 2 cells to the left. That way, it might be clearing cells that are already clear - but it would be pretty robust and straightforward. Once you have removed cells, then have then tumble down, by checking the cell above for colour. If there are no matches, then the variable fail will be 1, otherwise a match will set that to 0. I'd repeat until there are no matches, that way you have chain reactions as the board settles - chain reactions are always good, add in score multipliers and sounds and stuff to chain reactions - it encourages thoughtful play.

This is how I would do a Bejewelled game, if that's the sort of thing you have in mind.


Health, Ammo, and bacon and eggs!
baxslash
Valued Member
Bronze Codemaster
19
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 23rd Feb 2010 16:02
That's smart thinking @Han B!

I like the idea about chain reactions too. Always fun when you get those extra points (tetris I'm thinking of)!

Van B
Moderator
23
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 23rd Feb 2010 16:07
Sorry Saxblash, it's been a long day

Forgot to mention that Cell should be based on the object positions. For each object, record it's colour in the relevant cell location, so it's really easy to compare them. It's the only way to go with grid based games and would make comparisons like that very easy.


Health, Ammo, and bacon and eggs!

Login to post a reply

Server time is: 2026-07-27 05:11:39
Your offset time is: 2026-07-27 05:11:39