i need help with making a tile matching game!!! :(

edited October 2013 in How To...

i have to make a tile matching game that is due tomorrow night and i have no idea how to even due this. my teavher really isnt helpful with notes. (they make little sense) this is the setup i need to use. it has to be a 4x4 game.

void setup()
{
  // square set for the board + the panel for start, next and score

}

void draw()
{
//probably empty? But it keeps listening to events an loops to redraw

}

void mousePressed()
{
  // very important logic , “switch” or multi-way selection

….
}

void initializeBoard(int n)
{


// one single function for initializing the board irrespective of the size
// because it is parameterized on “n” 

} 

void initializeTurn()
{

}

void selectTile1()
{

}

void selectTile2()
{

}

void match()
{
  // it matches the tiles selected 
 // if matched , it scores 
 // It sets MA or NM state
}

void closeTiles()
{

}

void displayScore()
{

}

im hoping someone can help walk me threw this if possible.....................................#desperate

Answers

  • Given the state of the code (nothing), you won't code anything in time... I doubt somebody will do the job for you (well, you didn't ask that either).

    Have you tried to read the tutorials on the main site?

    You will need a 2D global array (the board), a global variable to hold the score, how to load images (the tiles) and to display them, how to detect a click on a rectangle, etc.

  • edited October 2013

    This is a homework assignment, so you need to write the code, but I can at least give an example of how to implement it.

    I'm guessing (you didn't say) that if it is a 16 tile board that there are 8 pairs of tiles to be matched. You can represent them with integers, an example board could be:

    0 0 1 1
    2 2 3 3
    4 4 5 5
    6 6 7 7
    

    Obviously this is a very boring arrangement of tiles, so you probably want to reorder them randomly. This can be done by going through the array of tiles and for each index swapping it with another random index. You would want to do this every time a new board should be created (or if you just play the game once in setup()).

    The mousePressed() function is there for the swapping. You need to determine which tile the mouse is on top of. This can be done using mouseX / mouseY and testing which box they are inside of. This is done with bounding box code / point in rectangle code, there are many examples of this on the forum.

    When the mouse is pressed, store the tile (the integer) that was clicked. If the next click is the same integer, then there was a tile match. If the integers are different it was not a tile match.

  • edited October 2013 Answer ✓

    Side note: you are bit late for your assignment, but if you want to do it anyway, try using the above suggestions, show some code, we will be happy to guide you. Going through that can be very good for learning Processing (and programming).

    [EDIT] I accidentally accepted my own answer (instead of editing it!), and even as admin, I cannot remove this flag! Sorry for the noise...

  • edited October 2013

    http://forum.processing.org/two/discussion/627/shuffle-in-a-2d-array

    From the link above, a snippet to shuffle a 2D array in no time: :-j

    void shuffle2dArray(int[][] arr) {
      final int cols = arr.length;
      final int rows = arr[0].length;
    
      for (int nc = (int) random(cols), c = 0; c != cols; c++) {
        final int[] col = arr[c], ncol = arr[nc];
    
        for (int nr = (int) random(rows), r = 0; r != rows; r++)
          if (nc != c | nr != r) {
            ncol[nr] ^= col[r] ^= ncol[nr];
            col[r] ^= ncol[nr];
          }
      }
    }
    
  • This might be an easier swapping code to make sense of...

    int tiles[] = new int[16];
    
    void setup() {
      size(400, 400);
    
      // Make the boring array of integers
      for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 2; j++) {
          tiles[i*2+j] = i;
        }
      }
      println("Before swapping:");
      println(tiles);
    
      // Randomly swap the integers in the array
      for (int i = 0; i < 16; i++) {
        int randomIndex = (int)random(16);
        int swap = tiles[i];
        tiles[i] = tiles[randomIndex];
        tiles[randomIndex] = swap;
      }
      println("After swapping:");
      println(tiles);
    }
    
    void draw() {
    }
    
  • edited October 2013

    http://studio.processingtogether.com/sp/pad/export/ro.9ABG0RKl9D2Bx/latest

    Example of tile select/deselect by mouse click! =:)

    @asimes -> that is 1D, not 2D as mine! O:-)

  • I didn't say anything about dimensions. I just gave code that someone new to Processing / coding has a chance at understanding.

  • edited October 2013

    @asimes I wasn't criticizing your example. I just wanted to highlight the fact that yours are for 1D array shuffling and mine for 2D!
    So 1 could know which 1 to choose from depending on his/her needs! 8-X

    And I agree that mine can't be understood that easily. b-(
    It's more like a black-box mini-code; which we feed it, and then it returns the desired result! :-??
    In this case, a shuffled 2D array! <):)

  • edited October 2013

    Anyways, here's a white-box version. Stripped from any non-orthodox tricks: >:/

    void shuffle2dArraySimpler(int[][] arr) {
      final int cols = arr.length;
      final int rows = arr[0].length;
    
      for (int c = 0; c != cols; ++c)  for (int r = 0; r != rows; ++r) {
        final int nc = (int) random(cols), nr = (int) random(rows);
    
        final int swap = arr[c][r];
        arr[c][r] = arr[nc][nr];
        arr[nc][nr] = swap;
      }
    }
    
Sign In or Register to comment.