Deleting characters from 2d arrays

This is a discussion on Deleting characters from 2d arrays within the C Programming forums, part of the General Programming Boards category; hello all i need some help. as it stands i am coding a game: 'Bejeweled' in C, for a school ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    2

    Deleting characters from 2d arrays

    hello all i need some help.

    as it stands i am coding a game: 'Bejeweled' in C, for a school project.
    So before you read my problem remember I only want hints not complete solves
    so far i have coded:

    function to fill and print-

    a 6 x 6 array to be filled up with random characters (any one of a given four eg # $ % &)

    function to check-

    where there are 3 or more of the same characters in a row (both horizontally and vertically)- and mark those positions

    function to shift any character above a marked character down
    eg lets pretend you have
    Code:
    $      the output of that column should be:                         -
    %                                                                   -
    #                                                                   -
    !                                                                   $
    !                                                                   %
    !                                                                   #
    then of course a simple function which replaces all '-' with new random characters.

    unfortunately, this shifting down only works sometimes, in general it works fine, but sometimes it only shifts my column down once or twice instead of three times.

    i am using 2 for loops to count backwards from element [5][5] to [0][0] and inside these loops another loop which when it sees an '!' element it checks above it until there is a character which is not '!' or '-' and replaces the position of '!' with that character, and that character with '-'.

    my general question: is there a smarter way to do this? eg, see where there are '!' chars, remove them from the array, and shift the column down- without using these troublesome iterations.

    what really bugs me is that my algorithm seems to work fine on paper, but only sometimes in my actual program. Testing through all 36 cases about 6 times to check exactly where ive gone wrong seems a bit tedious, so i just wanted a poke in the right direction.

    there of course could be something wrong with my code
    but if i post that here ive basically solved the problem for my entire class.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > without using these troublesome iterations.
    No there isn't.

    The way you solve it is by having a specific function to do just that specific task, and you make sure it works before you put it in the main code.

    Code:
    #include <stdio.h>
    #define SIZE 5
    
    void shift ( char a[SIZE][SIZE], int row, int col ) {
      while ( row > 0 ) {
        a[row][col] = a[row-1][col];
        row--;
      }
      a[row][col] = '-';
    }
    void print ( char a[SIZE][SIZE] ) {
      int r;
      for ( r = 0 ; r < SIZE ; r++ ) {
        printf( "%.*s\n", SIZE, a[r] );
      }
      printf( "\n" );
    }
    
    int main ( ) {
      char a[SIZE][SIZE] = {
        "00*00",
        "00$00",
        "00!00",
        "00!00",
        "00000",
      };
      int r = 3, c = 2;
      print( a );
      while ( a[r][c] == '!' ) {
        shift( a, r, c );
        print( a );
      }
      return 0;
    }
    If you want to "optimise" it by counting the number of '!' you're going to eliminate, and move a column 'n' places instead of 1 at a time, that's up to you.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    Salem, shouldn't that be int main(void)?


  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    LOL - good call.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    2

    Thumbs up

    danke but i fixed it with a loop. apparently it was not shifting my chars down to the last available free space on the first time through, so i just forced it to check about 9 times, and it seems to work. will try your way as well of course..
    if anyone else is interested:

    Code:
    void remove_comb(char temp_board[MAX_SIZE][MAX_SIZE])
    {
    	int i, q;
    	int count_up;
    	int i_count;
    	int loop_run;
    	loop_run = 10;
    	while(loop_run > 0){ // control loop
    
    		for(i =5; i >= 0; i--){
    			i_count =i;
    
    			for(q =5; q >= 0; q--){
    				if((temp_board[i_count][q] == '0') || (temp_board[i_count][q] == '-')){
    					for(count_up =i_count; count_up >= 0; count_up--){
    						if(temp_board[count_up][q] != '0' && temp_board[count_up][q] != '-'){
    							temp_board[i_count][q] = temp_board[count_up][q];
    							temp_board[count_up][q] = '-';
    							i_count--;
    						} // closes if
    		 			} // closes for
    				} // closes if
    			} // closes for
    		} // closes main for
    
    		loop_run--;
    	} // closes while loop
    
    	printf("\n\nRemove Combination:\n\n\n\n\n");
    	printf("\t\t    1  2  3  4  5  6");
    	for(i =0; i < MAX_SIZE; i++){
    		printf("\n\n\t\t%d  ", i+1);
    		for(q =0; q < MAX_SIZE; q++)
    			printf(" %c ", temp_board[i][q]);
    	}
    }
    it's perhaps not very elegant, but i did only learn c about 3 months ago

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D arrays with variable indexes
    By homeyg in forum C++ Programming
    Replies: 13
    Last Post: 01-22-2005, 09:40 PM
  2. File I/O with 2D arrays
    By hypertension in forum C Programming
    Replies: 2
    Last Post: 02-04-2003, 07:47 AM
  3. 2d arrays: H e l p!
    By scuba22 in forum C++ Programming
    Replies: 4
    Last Post: 11-04-2002, 04:54 PM
  4. intialise 2D arrays??
    By hyper88 in forum C Programming
    Replies: 5
    Last Post: 04-05-2002, 04:56 PM
  5. 2D arrays with functions made easy
    By goran in forum C Programming
    Replies: 1
    Last Post: 09-17-2001, 12:08 PM

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