Thread: Creating a Match-3 "Candy Crush" game using Arrays

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    1

    Creating a Match-3 "Candy Crush" game using Arrays

    I'm having a little bit of trouble on an assignment and am looking for some advice. I'm supposed to create a "game" that is similar to Candy Crush or Bejeweled. The assignment takes in a .txt file that contains a matrix of values from 1-5 and then assigns each value to a spot in a [10][10] array. Then an Escape Code function prints out colored pixels in place of the number values for each spot, creating a "game board" looking output. Finally, the program is supposed to look for any matches of 3 same-colored pixels and replace them with a white pixel and "XX". Then the program prints the corrected game board with the matches X'd out.

    I have it mostly coded, but I've encountered a couple of issues.


    1.) I am supposed to label the columns and rows 0-9, and while I have no problem coding the labels for the columns using printf( ), when I try to print the row labels I get a random string of numbers.

    2.) I replaced the matches with white pixels by reassigning the value in the array to 7, for which the ANSI Escape Code is white. However, I'm unsure about how to print the "XX" in the same spot.

    Here is the input(.txt) file and the output of the program so far:

    Creating a Match-3 "Candy Crush" game using Arrays-screen-shot-2015-03-07-1-14-52-am-pngCreating a Match-3 "Candy Crush" game using Arrays-screen-shot-2015-03-07-1-12-55-am-png

    And here is what I have coded at this point:

    Code:
    #include <stdio.h>
    
    void printEscapeCode(int c);
    
    int main(void)
    {
            /* Declare an image array to be gameboard */
            int gameboard[10][10];
    
            /* Declare variables and load in how many rows and columns */
            int Nrows;
            Nrows = 0;
            int Ncols;
            Ncols = 0;
    
            scanf("%d %d",&Nrows, &Ncols);
    
            /* Load in candy values for each row */
            int row, col;
            for(row = 0; row < Nrows; row++)
            {
                    /* Load in candy values for each column */
                    for(col = 0; col < Ncols; col++)
                    {
                           /* Declare variable to hold value */
                            int x;
                            scanf("%d",&x);
                           /* Tell where to store candy value */
                            gameboard[row][col] = x;
                    }
            }
            /* Calls function to print candy colors for each row */
            printf("  0 1 2 3 4 5 6 7 8 9\n");
            for(row = 0; row < Nrows; row++)
            {
                    printf("? ");
                    /* Calls function to print candy colors for each column */
                    for(col = 0; col < Ncols; col++)
                    {
                            if(gameboard[row][col] == gameboard[row+1][col] && gameboard[row+1][col] ==  gameboard[row+2][col])
                            {
                                    gameboard[row][col] = 7;
                                    gameboard[row+1][col] = 7;
                                    gameboard[row+2][col] = 7;
                            }
                            if(gameboard[row][col] == gameboard[row][col+1] && gameboard[row][col+1] == gameboard[row][col+2])
                            {
                                    gameboard[row][col] = 7;
                                    gameboard[row][col+1] = 7;
                                    gameboard[row][col+2] = 7;
                            }
                            if(gameboard[row][col] == 7)
                            {
                                    printEscapeCode(7);
                            }
                            if(gameboard[row][col] < 7)
                            {
                                    printEscapeCode(gameboard[row][col]);
                            }
                    }
                    printEscapeCode(7);
                    printf("\n");
            }
            return 0;
    }
    /* Function that prints candy colors */
    void printEscapeCode(int c){
            printf("\x1b[4%dm  ",c);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    As first posts go, this is top notch - well done


    1.) I am supposed to label the columns and rows 0-9, and while I have no problem coding the labels for the columns using printf( ), when I try to print the row labels I get a random string of numbers.
    > printf("? ");
    So this didn't work?
    printf("%d",row);

    2.) I replaced the matches with white pixels by reassigning the value in the array to 7, for which the ANSI Escape Code is white. However, I'm unsure about how to print the "XX" in the same spot.
    > printf("\x1b[4%dm ",c);
    Have you fully understood what each character in this format string is doing?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2015
    Location
    Clemson, South Carolina, United States
    Posts
    1
    CPSC 101 instructor here who gave the assignment! I'd really prefer if my students would use Piazza to ask classmates for help so that they could do things in a supervised way.

    Any of my students who copy this code, beware: if it looks too similar to the code posted here, after spring break I will be calling you into my office individually to have you explain what is happening in the code. If you can't adequately explain it, you'll be receiving no more than 25 points for this assignment (out of 120), and that's merciful, according to the syllabus policy on intellectual dishonesty:

    Academic Dishonesty: You are expected to do your own work.

    If you are found to have cheated, instead of adding points to your grade, I will subtract those points from your grade. For example, if you cheat on a programming assignment (worth 120 points), I will subtract 120 points from the points you have earned so far.

    Additionally, I will report any cheating to the Academic Integrity Committee. For more information on academic integrity, you may look on the Clemson website.
    I will clearly indicate which assignments should be completed individually. Generally, it is okay to ask a classmate for help, but not to share code with each other (copy/paste).

    For each assignment, I will ask you to turn in a collaboration statement using a template I will provide. If you do not submit a collaboration statement, you will only receive half the points you earn.
    No one's in trouble yet. Sometimes it's hard to know the line between collaboration and cheating. I'm just trying to help you draw that line--copy and pasting code will get you into trouble now and in the future.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 11-17-2013, 02:57 AM
  2. King (Candy Crush Saga) is hiring superstar C++ Developers!
    By King games in forum Projects and Job Recruitment
    Replies: 5
    Last Post: 10-17-2013, 02:15 PM
  3. Replies: 4
    Last Post: 07-17-2012, 09:02 AM
  4. Replies: 9
    Last Post: 03-31-2009, 04:23 PM
  5. Replies: 9
    Last Post: 03-30-2009, 06:37 PM

Tags for this Thread