Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (744)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (825)
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 460 times)
0 Members and 1 Guest are viewing this topic.
Offline 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 Huh

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<Candy,ArrayList<Tile>> matches = new HashMap<>();
      ArrayList<Tile> match = new ArrayList<>();

      // Find all horizontal matches
      for(int i=0;i<row;i++) {
         int j=0;
         match.clear();
         while(j < 8) {
            Tile current = tiles[i][j];
            Candy candy = current.getCandy();
           
            if(match.size() == 0 || match.get(0).getCandy().equals(candy)) {
               match.add(current);
            }else if(!match.get(0).getCandy().getType().equals(candy.getType())) {
               if(match.size() >= 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<col;j++) {
         int i=0;
         match.clear();
         while(i < 8) {
            Tile current = tiles[i][j];
            Candy candy = current.getCandy();
           
            if(match.size() == 0 || match.get(0).getCandy().equals(candy)) {
               match.add(current);
            }else if(!match.get(0).getCandy().getType().equals(candy.getType())) {
               if(match.size() >= 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
Offline 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)
Offline 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  Undecided. Maybe it has to do with that match.clear() part.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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<row;
in
1  
for(int i=0;i<row;i++) {
into
1  
i<=row;

Don't dream big, dream realistic.
beemelonstudio: Website | Twitter | Instagram
Offline 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.
Offline 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<Candy,ArrayList<Tile>> matches = new HashMap<>();
      ArrayList<Tile> match = new ArrayList<>();

      // Find all horizontal matches
      for(int i=0;i<row;i++) {
         int j=0;
         match.clear();
         while(j < 8) {
            Tile current = tiles[i][j];
            Candy candy = current.getCandy();
           
            if(match.size() == 0 || match.get(0).getCandy().equals(candy)) {
               match.add(current);
            }else if(!match.get(0).getCandy().getType().equals(candy.getType())) {
               match.clear();
               match.add(current);
            }
           
            if(match.size() >= 3) {
               matches.put(candy, match);
               System.out.println(match);
            }
           
            j++;
         }
      }

      // Find all vertical matches
      for(int j=0;j<col;j++) {
         int i=0;
         match.clear();
         while(i < 8) {
            Tile current = tiles[i][j];
            Candy candy = current.getCandy();
           
            if(match.size() == 0 || match.get(0).getCandy().equals(candy)) {
               match.add(current);
            }else if(!match.get(0).getCandy().getType().equals(candy.getType())) {
               match.clear();
               match.add(current);
            }
           
            if(match.size() >= 3) {
               matches.put(candy, match);
               //System.out.println(match);
            }
           
            i++;
         }
      }
   }
Offline 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==122
111222
1111=2

1  
2  
3  
4  
5  
=zzzz=
x1=z1=
x==z2y
xxxyyy
xxxx=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<col;j++)

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

 
Ecumene (150 views)
2017-09-30 02:57:34

theagentd (225 views)
2017-09-26 18:23:31

cybrmynd (304 views)
2017-08-02 12:28:51

cybrmynd (291 views)
2017-08-02 12:19:43

cybrmynd (300 views)
2017-08-02 12:18:09

Sralse (292 views)
2017-07-25 17:13:48

Archive (980 views)
2017-04-27 17:45:51

buddyBro (1106 views)
2017-04-05 03:38:00

CopyableCougar4 (1685 views)
2017-03-24 15:39:42

theagentd (1433 views)
2017-03-24 15:32:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
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!