#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    2D array elimination


    Hey
    I'm trying to working on a part of program that involves an 2D array with randomly arrange 4 letter (b, r, g, y - symbolizing color).
    Kind of like this. The size can vary.
    Code:
    gbbybbbb
    ryybbrby
    yrbrybyr
    yrbybybb
    ybybbybg
    byygybby
    bbggrbbg
    brrbgbbr
    The program will prompt the user to enter coordinate thereby picking a letter. For example b, then all the bs that's connected to each other would be eliminated. Then columns will collapse, and shift over to the left.

    I'm still pretty new to C so any help will be appreciated.
    I'll try to post up pictures but I can't right now...
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0
    Say you start with something like this,
    Code:
     
    grygbbb
    grbbbbb
    grbbbby
    grrbbgy
    grrrbgy
    grrrbby
    and the user enters the coordinates for the very top right b, then you get something like this (elimination of all the connected bs only vertically and horizontally. NOT diagonally connected)
    Code:
    gryg   
    gr
    gr    y
    grr  gy
    grrr gy
    grrr  y
    then the columns will collapse so there's no letter that has no letter underneath it, as if by virtual gravity
    Code:
    gr
    gr
    gry    y
    grrg   y
    grrr  gy
    grrr  gy
    then the columns shift over to that left so that columns with no elements are eliminated
    Code:
    gr
    gr
    gry  y
    grrg y
    grrrgy
    grrrgy
    And the user continues to enter coordinates until no more letters can be eliminated.
  4. #3
  5. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,396
    Rep Power
    1871
    So what's your question?

    Because it looks like a homework dump and expectation of complete code on a plate in return, and that just isn't going to happen.

    You showed three distinct steps, so how about 3 functions.
    Code:
    #define SIZE 10
    
    // eliminate the connected letters starting at x,y
    void eliminate ( char grid[SIZE][SIZE], int x, int y );
    
    // do the gravity bit
    void collapse ( char grid[SIZE][SIZE] );
    
    // do the shift bit
    void shift ( char grid[SIZE][SIZE] );
    
    // For good measure, and an aid to debugging
    // just print the grid
    void print ( char grid[SIZE][SIZE] );
    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
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by salem
    So what's your question?

    Because it looks like a homework dump and expectation of complete code on a plate in return, and that just isn't going to happen.

    You showed three distinct steps, so how about 3 functions.
    Code:
    #define SIZE 10
    
    // eliminate the connected letters starting at x,y
    void eliminate ( char grid[SIZE][SIZE], int x, int y );
    
    // do the gravity bit
    void collapse ( char grid[SIZE][SIZE] );
    
    // do the shift bit
    void shift ( char grid[SIZE][SIZE] );
    
    // For good measure, and an aid to debugging
    // just print the grid
    void print ( char grid[SIZE][SIZE] );
    I don't expect a full code. That would be way too generous. Just any hints or tips.
    A short pseudo code would be awesome.

    I'm wondering whether the first function would be necessary though. I don't necessarily need a turn the characters into a space. So long as the 1st grid ends up looking like the bottom grid, it's all good.
  8. #5
  9. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,396
    Rep Power
    1871
    You need to post some actual code (or actual pseudocode), so we can see exactly what you're capable of, and thus tune the advice and help accordingly.

    Simply spoon-feeding anything beforehand suffers from the goldilocks problem (I knew that already :rolleyes: , just what I needed :) , I'm still confused :( )

    But I'm guessing you'll need this to complete step 1.
    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

IMN logo majestic logo threadwatch logo seochat tools logo