Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (769) Games in Android Showcase (230) games submitted by our members Games in WIP (856) 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
 Matching algorithm almost success  (Read 1640 times) 0 Members and 1 Guest are viewing this topic.
xxMrPHDxx

Senior Newbie

Exp: 3 years

I love JAVA!

 « Posted 2017-11-20 16:51:45 »

I was working on my game as an inspiration from the game "Candy Crush". I did a check method to find all the 3 or more matches on the rows and column. I thought I did it but I saw a match that sit at the end of the row or column was not added to the matches that it found. Someone could help? I'm stuck forever

This is the algorithm I used to check all matches:

 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  51  52  53  54  55  56  57  58  59 `private void check() {      Map> matches = new HashMap<>();      ArrayList match = new ArrayList<>();      // Find all horizontal matches      for(int i=0;i= 3) {                  matches.put(candy, match);                  System.out.println(match);               }               match.clear();               match.add(current);            }            j++;         }                        // And I thought this could solve that problem but it's not         if(j == 7 && match.size() >= 3) {            match.add(tiles[i][j]);            matches.put(tiles[i][j].getCandy(), match);            //System.out.println(match);         }      }      // Find all vertical matches      for(int j=0;j= 3) {                  matches.put(candy, match);                  //System.out.println(match);               }               match.clear();               match.add(current);            }            i++;         }                        // Here too, but for the vertical matches         if(i == 7 && match.size() >= 3) {            matches.put(tiles[i][j].getCandy(), match);            //System.out.println(match);         }      }   }`

You can see more of my code at my GitHub here
Icecore
 « Reply #1 - Posted 2017-11-20 23:23:38 »

 1  2  3  4  5  6  7  8  9  10  11 `for y 0 to end   for x 0 to end   if (x, y + 1) == (x, y) &&      (x, y + 2) == (x, y)      Match Y ([x, y], [x, y + 1], [x, y + 2])         if (x + 1, y) == (x, y) &&      (x + 2, y) == (x, y)      Match X ([x, y], [x + 1, y], [x + 2, y])   //don't forget to check bounds on +1 +2 )   //   if (y >= end || x >= end) return -1`

but what if you have:
 1  2  3  4 `###############XXXX#`

result will be:
 1  2  3  4 `###############MMMX#`

i think you can fix it its little more code ^^

also + one more option
 1  2  3  4 `#####X####X####XXX##`

Last known State: Reassembled in Cyberspace
End Transmission....
..
.
Journey began Now)
xxMrPHDxx

Senior Newbie

Exp: 3 years

I love JAVA!

 « Reply #2 - Posted 2017-11-21 09:26:31 »

But that algorithm of yours only consider 3 match only. I was going to consider 3-match, 4-match and so on.

Anyway, my code is working though. It's just not detecting match when one of the match piece lies on the end of the row/column. I don't know what's wrong  . Maybe it has to do with that match.clear() part.
 Games published by our own members! Check 'em out!
Stampler
 « Reply #3 - Posted 2017-11-21 12:27:18 »

Hi,

just a random tought: Do your row and col variables have the right value or are they too low by one?
Given your description it looks like your for-loops end too early.

Maybe try changing
 1 `i
in
 1 `for(int i=0;i
into
 1 `i<=row;`

Don't dream big, dream realistic.
beemelonstudio: Website | Twitter | Instagram
xxMrPHDxx

Senior Newbie

Exp: 3 years

I love JAVA!

 « Reply #4 - Posted 2017-11-21 12:46:02 »

I'm sure I did it right and also I did try that to solve that but it give me array out of bound exception. I have no clue.
xxMrPHDxx

Senior Newbie

Exp: 3 years

I love JAVA!

 « Reply #5 - Posted 2017-11-21 14:34:37 »

Never mind. I have found the solution. I just need to check the matches length at the last of the while loop instead of the if else.

So, it will look like this

 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  51  52 `private void check() {      Map> matches = new HashMap<>();      ArrayList match = new ArrayList<>();      // Find all horizontal matches      for(int i=0;i= 3) {               matches.put(candy, match);               System.out.println(match);            }                        j++;         }      }      // Find all vertical matches      for(int j=0;j= 3) {               matches.put(candy, match);               //System.out.println(match);            }                        i++;         }      }   }`
Icecore
 « Reply #6 - Posted 2017-11-21 15:31:46 »

Maybe this help)
some test data
 1  2  3  4  5 `=1111=11=11=1==1221112221111=2`

 1  2  3  4  5 `=zzzz=x1=z1=x==z2yxxxyyyxxxx=y`

1 x = 9
2 y = 5
1 z = 6

maybe mark visited cells to skip duplicates
horizontal and vertical match may have same cells )
 1  2  3  4  5  6  7 `--i j++y x--while(j < 8)++while(j < col) //or++for(int j=0;j

Last known State: Reassembled in Cyberspace
End Transmission....
..
.
Journey began Now)
Pages: [1]
 ignore  |  Print

 EgonOlsen (1668 views) 2018-06-10 19:43:48 EgonOlsen (1705 views) 2018-06-10 19:43:44 EgonOlsen (1156 views) 2018-06-10 19:43:20 DesertCoockie (1584 views) 2018-05-13 18:23:11 nelsongames (1184 views) 2018-04-24 18:15:36 nelsongames (1708 views) 2018-04-24 18:14:32 ivj94 (2532 views) 2018-03-24 14:47:39 ivj94 (1757 views) 2018-03-24 14:46:31 ivj94 (2834 views) 2018-03-24 14:43:53 Solater (970 views) 2018-03-17 05:04:08
 Deployment and Packagingby mudlee2018-08-22 18:09:50Java Gaming Resourcesby gouessej2018-08-22 08:19:41Deployment and Packagingby gouessej2018-08-22 08:04:08Deployment and Packagingby gouessej2018-08-22 08:03:45Deployment and Packagingby philfrei2018-08-20 02:33:38Deployment and Packagingby philfrei2018-08-20 02:29:55Deployment and Packagingby philfrei2018-08-19 23:56:20Deployment and Packagingby philfrei2018-08-19 23:54:46
 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