Bejeweled - How make it possible to remove gem when it fall.

Started by
9 comments, last by zamiel 11 years, 1 month ago

<Language: ActionScript3>

Hi I would like to know if anyone know, the algo for removing gems when it fall on a grid. (Like bejeweled style)

With a 2D array of element it seems to be not stable.

Thanx

Advertisement

I do not know your language, although your problem should have an simple solution.

I also do not know the rules of your game. If getting an (x) number of the same type of jewel is in a row or column then do an element comparison.

set up a double For...Next Loop with the number of elements in your 2D array -1

then compare 1 element to the next and if they are the same then increment a counter also set a starter element.

When when reach to elements that do not equal each other exit your loop, Check your counter. If the counter is => the minimum number then goto a sub-routine to delete those elements and fill them in with the elements from above.

If I sat down for 30 minutes I could provide you with the algorithym, But where is the fun in that.

If you get stuck. Contact me and I will see what I can do.

Good Luck.

Your Brain contains the Best Program Ever Written : Manage Your Data Wisely !!

I dont see why you think a 2D array would be unstable. 2D arrays are exactly how grids are represented in games. If you prefer some additional encapsulation. you could use a managed container like a dynamic array. Additionally, each element in the 2D array could be some kind of "GridCell" class, which contains information about the grid. You should also create a wrapper class around your 2D grid array which will manage it for you, which has operations such as "dropRow", "dropColumn", "checkForMatches", ect. Best of luck.

metsfan

<I dont see why you think a 2D array would be unstable. 2D arrays are exactly how grids are represented in games.>

It's because when I destroy a gems the space inside become null but other gems when they fall and goes in that case,

Poigahn

For now I coded all the games. All the Gems are unable to click while they fall. But I want a system more dynamic.

metsfan

<I dont see why you think a 2D array would be unstable. 2D arrays are exactly how grids are represented in games.>

It's because when I destroy a gems the space inside become null but other gems when they fall and goes in that case,

The 2D array would represent the relative location of the gems in terms of gameplay, but it shouldn't be tied to the actual screen location of the gems. This separation is necessary so that you can do animations while the array is updating.

Suppose you clear 3 gems in a column. Your array will now have three zero values (empty areas) in one column, and the algorithm to update the array would be, for every zero value that is in a row below a non-zero value (gems), trade their places. To do this in one pass, you start from the 2nd to bottom row going up and horizontally. Afterwards all your empty spots will be on the top. Then you simply fill these zero spots with random values for new gems.

New game in progress: Project SeedWorld

My development blog: Electronic Meteor

I also have never really used action script either, but I have done something similar to a bejeweled clone. It took me a while to get the blocks to fall properly, but I did manage with a 1D array treated as a 2D array to keep things simple. Unfortunately, the fine tuned source code was on my old macbook which was stolen about 6 months ago, so I have to redo the changes I made on it. Fortunately, I remember what I was doing wrong.

I'll have to dig up the source on my Notebook PC if you'd like to see.

Shogun.

blueshogun96

This could be very interessing to see what you've done. Thanx

CC Ricers

Thanx for the tips. I'll give it a try right now.

use a 2D array(or 1D treated as 2D with an stride). anyway, what i'd do for bejewed is:

when a gem is destroyed, instantly update the tile(or gem) list so that internally everything is represented correctly, and you have no null spaces.

now then, each tile should also contain a draw position, do not change this value when you are updating the list, just make sure it's retained when you swap tiles around.

so, now, even though a gem is destroyed, internally the grid looks correct, however when drawn, the gem should appear as if it's in it's old position, now, on each update tick, move that draw position where it's suppose to be on the grid, this should create the illusion of the gem falling.

in theory this system can be used for anything, if you want new gems to drop from above the camera, when adding a new gem to the array, simply set the draw position above the camera. when you swap gems around, you could either instantly set the correct position, or you can just let the gem move to where it's suppose to.

Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

You don't need to "destroy" the gem per-se. Just make some state that represents if there is a gem or not. Like an enum of "Empty" "BlueGem" "GreenGem" etc.

Removing a gem would set the 2D array at that position to "Empty" state.

If you draw it you check the 2D array and say, if BlueGem -> draw(BlueGemTile) and such. You don't need to tie in your representation of the state of the game (grid of gems) to your visual representation of it (tiles, board, score number, etc). That means that you don't need to represent the "falling" on your grid of gems, just represent it on the visual side (the "falling" tiles), and when it stops "falling", update the actual jewel position on your data grid.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

For the falling movement, determine a "target position" for each of the gems, which are just keyframes for the screen position where the animation ends. Here is where you can use your updated 2D array to find target positions. For example if each gem is 50x50 pixels you would want a gem that's stored at [2, 3] in the array to be 100, 150 when it's in play.

Draw the new gems somewhere off the screen or outside the grid, just in the same horizontal position as the row they will fall in. Then keep moving them down until they reach or are past the vertical position. After all gems are in place, the game continues as normal. Then you can re-check for more matches, which is how you would handle combos, cascades, etc.

New game in progress: Project SeedWorld

My development blog: Electronic Meteor

This topic is closed to new replies.

Advertisement