Saturday 14 June 2014

3 match algorithm used in games like candy rush, farm saga code for java language

The 3 match or more then 3 is pretty simple, lets create the grid first
-------------------- code --------
  public int[][] grid =  
   {{4,4,4,3,7,6,8,0},  
   {2,3,4,5,6,7,7,0},  
   {8,9,6,9,2,4,8,0},  
   {7,8,9,5,6,7,8,0},  
   {7,8,9,5,6,7,8,0},  
   {7,8,9,5,6,7,3,0},  
   {7,8,9,5,6,7,3,0},  
   {0,0,0,0,0,0,0,0}};  
  8x8, we will be using only 7x7 0's will not be scanned  
  // this will check the columns for 3 match or more  
  private static void checkCol(int pRow) {  
  // you can loop over and over by passing the row to search on  
  int match =0;  
  for(int i=0; i < 7; i++){  
   if(grid[pRow][i] == grid[pRow][i+1]){  
        // increase match found by 1  
   match ++;  
  // u can use a array list to store matched grid points  
   }else{  
       // 2 = 3 matched , 3 = 4 matched and so on   
   if(match >=2){  
   int f = match;  
   f++;  
   System.out.print("found "+f);  
   }  
   break; // break loop  
   }  
   match = 0;  
   }  
  }  
  }  
 // this will check the rows for 3 match or more , same as above just little changes  
 private static void checkRow(int pCol) {  
  int match =0;  
  for(int i=0; i < 7; i++){  
   if(grid[i][pCol] == grid[i+1][pCol]){  
             // increase match found by 1  
   match ++;  
 // u can use a array list to store matched grid points  
   }else{  
            // 2 = 3 matched , 3 = 4 matched and so on    
   if(match >=2){  
    int f = match;  
    f++;  
    System.out.print("found "+f);  
    }  
    break; // break loop  
   }  
   match = 0;  
   }   
  }  
 }  
-----------------

Thats it simple as that, you use to make android or any 3 or more match game, if you haev better way please post in the comment android game is made that uses same : Coin Crush - 3match android

Tuesday 17 July 2012