How to edit printed array board for simple candy crush game

This is a discussion on How to edit printed array board for simple candy crush game within the C Programming forums, part of the General Programming Boards category; So as part my first year assignment, I am required to create a simple candy crush game. I am this ...

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    5

    Unhappy How to edit printed array board for simple candy crush game

    So as part my first year assignment, I am required to create a simple candy crush game.

    I am this stage where I am required to print the 'box' for navigation purpose at the center of the board once the program initializes.

    Here is the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    //FUNCTION: Draw the Board
    int drawBoard()
    {
        //Declare array size
        int board[9][9];
    
    
        //initialize variables
        int rows, columns, randomNumber, flag;
    
    
        //random number seed generator
        srand(time(NULL));
    
    
            for ( rows = 0 ; rows < 9 ; rows++ )
            {
    
    
                for ( columns = 0 ; columns < 9 ; columns++ )
                {
                    flag = 0;
    
    
                    do
                   {
                        //generate random numbers from 2 - 8
                    randomNumber = rand() %7 + 2;
    
    
                    board[rows][columns] = randomNumber;
    
    
                    //Checks for 2 adjacent numbers.
                    if  ( board[rows][columns] == board[rows - 1][columns] || board[rows][columns] == board[rows][columns - 1] )
                        {
                            flag = 0;
                            continue;
                        }
    
    
                    else
                    //Prints the correct board
                         {
                            flag = 1;
                            printf( "  %d  ", board[rows][columns] );
                         }
    
    
                    } while ( flag == 0 );
    
    
                }//end inner for-loop
    
    
                printf("\n\n");
    
    
            }//end outer for-loop
    
    
    }//end FUNCTION drawBoard
    
    
    //FUNCTION: Mark the surrounding of the number with "|" and "_" at board[5][5]
    void marker( int a )
    {
        printf( " _ \n" );
        printf( "|%c|\n", a );
        printf( " _ \n" );
    }
    
    
    int main()
    {
        drawBoard();
    }
    So I need to warp the box around the number at coordinate
    Code:
    board[5][5]
    .

    I understand that the sequence could be to clear the screen and print the whole board with the indicated marker.

    *Yes I am required to move the box in later part of the program but for now, I just need to get the box to show up at coordinate
    Code:
    board[5][5]
    Well...yes I am stuck and have been losing hair for the past 2 days over this

    Name:  The box.jpg
Views: 1453
Size:  73.7 KB
    Last edited by Sam Liew; 10-24-2013 at 05:33 AM. Reason: Insert image of how the game should look like - well initially.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you want the center of the box at [5][5], where will you need to print the top and bottom of the box? Where will you need to print the sides of the box? Are you supposed to leave room for that, or are you supposed to draw on top of the other numbers?

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    5
    Hi tabstop,

    I left room for that in the line

    printf( " %d ", board[rows][columns] ); and also
    printf("\n\n");

    in the loop.

    Emm I think I'll leave an image of how it should look like in the original post Please have a look there, thanks!

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, that's the tricky bit -- you can never go backwards (unless you're learning DOSbox console IO, which heaven help us), so if you always print \n\n you can never print the -- bits in that otherwise-empty line. So you'll have to be more careful about that. Similarly for always printing the spaces.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    To do this without any platform specific stuff you're going to have to redraw the whole board. When you approach 5-5 you print a border, whatever border actually makes sense at the time. Since the cursor would move in the same direction most of us read (left to right, top down) then you would border the number in the sequence of _ top, | left, right |, and _ bottom. It's going to be annoying if you have to mark each area for a selection, but it's possible.

  6. #6
    Registered User
    Join Date
    Oct 2013
    Posts
    5

    I think I am getting close!

    Hmm so the solution is definitely that I have to print the marker during in the loop

    I tried these in the do-while loop.

    Code:
     while ( board[5][5] == 1 )
                             {
                                 marker( board[5][5] );
                             }
    Code:
    if ( board[5][5] )
    marker ( board[5][5] );
    I think the issue here is that I don't know how to ensure the function is called when the rows = 5 and columns = 5 / i.e board[5][5]

    I know I'm almost there!

    What could be wrong here?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What's the purpose of the marker function? Is it a requirement you have such a thing, because at the moment it just seems to be getting in the way.

  8. #8
    Registered User
    Join Date
    Oct 2013
    Posts
    5

    It's a requirement :(

    Quote Originally Posted by tabstop View Post
    What's the purpose of the marker function? Is it a requirement you have such a thing, because at the moment it just seems to be getting in the way.
    Yeah I know right :/ It is amazingly annoying how something that looks so simple is thinning my hair It is part of the assignment's requirement

    It functions as a navigator. In the later part of program, I am required to use the marker to navigate through the board.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, is the function supposed to navigate through the board or is it supposed to draw a square? (It would be a bit weird to have a function do both.)

    If you do need a function to draw a square, one possibility may be to create a larger (as in 19x19) grid of characters that represents the physical layout of the board -- you can pass that grid to functions and they can fill in numbers or the extra square, etc, and then once all that has been added just print the grid.

  10. #10
    Registered User
    Join Date
    Nov 2013
    Location
    Paris, France, France
    Posts
    1
    Have you finish your Candy Crush ?

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, alextemporao!

    After three weeks on this forum, with no answers, I'm sure the thread has gone dead. If you have a question, it's best to start a new thread, to give it the attention it deserves.

  12. #12
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    After three weeks on this forum, with no answers, I'm sure the thread has gone dead.
    I think that's why he wanted to know if the OP finished his program or not. Such things are what PMs are for, I guess, though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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, 03:15 PM
  2. Replies: 3
    Last Post: 10-17-2013, 12:26 AM
  3. array being printed incorrectly
    By amissot in forum C Programming
    Replies: 4
    Last Post: 12-29-2011, 05:10 AM
  4. Replies: 8
    Last Post: 11-12-2008, 11:25 AM
  5. Simple Eye-Candy program
    By EdanD in forum C Programming
    Replies: 7
    Last Post: 06-30-2004, 09:48 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21