Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (521)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (589)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Assistance with Match-3 Style Game Logic  (Read 2456 times)
0 Members and 1 Guest are viewing this topic.
Offline coder_for_life22

Senior Newbie





« Posted 2014-02-21 04:09:52 »

Hi guys,

I would like to build a "connect the dots" style game.

I already know how to fill in the grid for the game and i know how to remove the objects when the user selects a pair.

The problem is, i am not sure how to fill in spaces when objects are removed.

For example, if 2 items are removed from row 1 and 1 item is removed from row 2. what would be the algorithm to drop the items in row 2 down to slots, and the items in row 1 down 1 position above where the item was removed.

I hope i exlained my question well. For this game i will be using Libgdx(which is java) so any java pseudocode will be GREAT. I will also be using sprites of libgdx and tween engine to move objects.


Thanks so much guys, hope i can find the help i need.
Offline seismic

Senior Devvie


Medals: 4
Projects: 1



« Reply #1 - Posted 2014-02-21 07:39:05 »

I'm doing something similar currently, so I can help I guess Cheesy

My first solution was something like this

1  
2  
3  
4  
5  
6  
7  
8  
for( x = 0; x < MAX_CELLS_X; x++ ) {
     for( y = 0; y < MAX_CELLS_Y; y++) {
          if( cell[x][y].isEmpty() ) {
               for( d = y + 1; d < MAX_CELLS_Y; d++ ) {
                    cell[x][d].getOccupant().moveBy( 0, -((d-y)*CELL_SIZE));
                    }
          }
     }

So this was off the top of my head, sorry if it won't compile Grin. Anyway, the moveBy is a scene2D function. You  may have your own implementation here of course (like setting a boolean doFall or something) and have the occupants update each frame or something like that.

Hope it helps.
Offline coder_for_life22

Senior Newbie





« Reply #2 - Posted 2014-02-21 11:15:49 »

Ah. Thank you for that. :-)

But how exactly does the cell[][] work?

Of what type is it?

And how do you go about updating the occupant?

I'm guessing get occup just returns the sprite.

Thanks a lot for your help so far!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline opiop65

JGO Kernel


Medals: 159
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #3 - Posted 2014-02-21 11:56:50 »

Judging from that statement I would say you need to take a step back fromd games and try to learn Java first. That is of a multi deminsional array type, which pretty much means it holds a bunch of references to blocks of memory (your objects). This is basic coding though, and I highly stress you stop trying to make a game and spend the time to learn programming first. You're only hurting yourself right now.

Offline coder_for_life22

Senior Newbie





« Reply #4 - Posted 2014-02-21 13:04:12 »

Judging from that statement I would say you need to take a step back fromd games and try to learn Java first. That is of a multi deminsional array type, which pretty much means it holds a bunch of references to blocks of memory (your objects). This is basic coding though, and I highly stress you stop trying to make a game and spend the time to learn programming first. You're only hurting yourself right now.

LOL. Actually i know java very well. I was asking what data type is it. Sprite, int, etc. ;-) I am fully aware of what an array is. This is like my 5th game. Just not sure how the match-3 thing works.

@Siesmic how do you go about updating the occupants each frame?

Offline seismic

Senior Devvie


Medals: 4
Projects: 1



« Reply #5 - Posted 2014-02-21 13:21:14 »

Well if you do it with scene2D you don't need to update them manually, you just add the action. But if you do the other method, where you set a boolean (like needsToFall or something), then you iterate through each of your Cell-occupants (extensions of Sprite), check if they need to fall (have the boolean set to true) and update their y-position accordingly.

Edit: Also, once they've fallen CELL_SIZE update the cell below to hold a reference to the sprite, set the needToFall boolean to false, so they don't fall forever Smiley
Offline coder_for_life22

Senior Newbie





« Reply #6 - Posted 2014-02-21 13:25:45 »

Yes I will be using sprites.

But how do I iterate through to check if the sprite needs to be moved down?
Offline seismic

Senior Devvie


Medals: 4
Projects: 1



« Reply #7 - Posted 2014-02-21 13:27:22 »

With a for loop like above

1  
2  
3  
4  
5  
6  
7  
for( x = 0; x < MAX_X; x++ ) {
     for( y = 0; y < MAX_Y; y++ ) {
          if( cells[x][y].getOccupant().needsToFall() {
               cells[x][y].getOccupant().fall(delta);
               }
          }
    }
Offline coder_for_life22

Senior Newbie





« Reply #8 - Posted 2014-02-21 13:35:56 »

So basically.

It first checks if a cell is empty or needs to fall. Then, if it is empty, we go up one row and move that item down to the empty position, and this will continue to happen for all the empty positions until the loop finishes? Correct?

But what if, more than 1 item is removed from a column? Or if more than 1 item is empty in column?

I ask because in my game, the user can connect as many matching sprites as possible. This means they can remove more than 1 item from a column, vs jus 3.
Offline The Lion King
« Reply #9 - Posted 2014-02-21 15:52:57 »

wrote this up in a couple minutes, assuming I understand your question correctly and I didn't make any dumb mistakes due to the fact that I didn't test this and wrote it up so quick, it should work.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
public void scanForDrops(int maxX, int maxY)
{
   int spaces = 0; //counts spaces
   int occupantsAbove = 0; //occupants above the point the loop is at
   boolean firstOccupantFound = false; //becomes true when the first cell in the column with a Piece is found
   for(int x = 0; x < maxX; x++)
   {
      spaces = 0;  
      occupantsAbove = 0;
      firstOccupantFound = false;
      for(int y = 0; y < maxY; y++)
      {
         if(cell[x][y].hasOccupant() )
         {
            if(!firstOccupantFound)
            {
               firstOccupantFound = true;
            }
            occupantsAbove++;  
         }
         else if(!cell[x][y].hasOccupant() && firstOccupantFound) //spaces above the first occupant dont matter
         {
            spaces = checkSpaces(x, y, maxY);  //gets spaces under this starting at this point
            dropAbove(x, y--, occupantsAbove, spaces); //drops all the pieces spaces amount of times starting at the piece above this space
            y+= spaces + 1; //puts y down past the spaces and one extra because we did a y-- on the last line
            spaces = 0; //now there are no spaces in between and itll keep checking down the line
         }
         
      }
   }
}

public int checkSpaces(int x, int y, int maxY)
{
   int spaces = 0;
   while(y < maxY && !cell[x][y].hasOccupant())
   {
      spaces++;
      y++;
   }
   return spaces;
}

public void dropAbove(int x, int y, int above, int spaces) // drops every piece above spaces amount of times
{
   for(int i = 0; i < above; i++)
   {
      cell[x][y - i].getOccupant().moveTo(x, y - i + spaces); // something like this dunno how your code looks
   }
}

"You have to want it more than you want to breath, then you will be successful"
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline coder_for_life22

Senior Newbie





« Reply #10 - Posted 2014-02-23 19:10:58 »

@LionKing

Nice! Thanks for that. I understand what your code does for the most part. :-)

A few questions, for the cell array, should this be a extended class of the sprite class as the data type?

And then create a method hasOccupant() in the Sprite class? Or does it have some type of other data type it should hold?

Also, how would i go about determining if a cell has an occupant?

Thanks so much man.
Offline coder_for_life22

Senior Newbie





« Reply #11 - Posted 2014-02-24 17:14:41 »

Ok, so i have it semi-working with this code:


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
private void moveObjectsDown(int x, int y) {
     
     
      // TODO Auto-generated method stub
     
     
      for (GameObject o : gameObjects) {
         
         if(o.getX() == x && o.getY() == y+1){
           
            // Animation of an object position to the coordinates (100,200), in one second
            Tween.to(o, Accessor.POSITION_Y, 1.0f).target(y, y).start(game.tweenManager);
            //o.setPosition(x, y);
           
            y++;
           
           
         }
         
         
         
         
         
      }
     
   }/code]

This works flawlessly when only one item is removed from a row. But when i remove two items or more from a row, it only moves the items left in that column to the highest position.

I guess a way i could fix this would be to figure out the item with the lowest y position in that particular column and then drop the items according, but i am not sure how to do this.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xFryIx (57 views)
2014-11-13 12:34:49

digdugdiggy (35 views)
2014-11-12 21:11:50

digdugdiggy (29 views)
2014-11-12 21:10:15

digdugdiggy (23 views)
2014-11-12 21:09:33

kovacsa (46 views)
2014-11-07 19:57:14

TehJavaDev (50 views)
2014-11-03 22:04:50

BurntPizza (50 views)
2014-11-03 18:54:52

moogie (65 views)
2014-11-03 06:22:04

CopyableCougar4 (63 views)
2014-11-01 23:36:41

DarkCart (148 views)
2014-11-01 14:51:03
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!