Thread: help writing pseudocode into code for C puzzle

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    16

    help writing pseudocode into code for C puzzle

    Hi,

    I am just learning how to program in C, so please bear with me if I'm asking really trivial questions. I'm basically writing code for a puzzle known as the 15-puzzle. The board itself is comprised of 4 rows and 4 columns, so it has a length of 16. I am currently writing the functionvoid initialize tiles() which initializes the pixel data for each tile. So it's basically supposed to look like View image: 15puzzle initially.

    I'm not really sure how to approach this. I have written myself some skeleton code for what I think I should do, but a little guidance would be much appreciated! Thanks!


    Code:
     
    
    
    /*
     * Initialize the pixel data of each tile.
     *
     * Background colors should be initialized to the following, where B = black,
     * R = red, and W = white:
     *   W R W R
     *   R W R W
     *   W R W R
     *   R W R B
     *
     * The tiles' digits should be initialized to the following:
     *   01 02 03 04
     *   05 06 07 08
     *   09 10 11 12
     *   13 14 15
     *
     * the tens digit and units digit of a two-digit number n can be
     * calculated as follows:
     *   tens_digit = n / 10
     *   tens_digit = n % 10
     *
     * Global variables used: tile_data.
     *
     * Functions called: initialize_tile.
     */
    
    
    void initialize_tiles(int tile_data[][]){
        int col, row;
        
    
    
        /* TO DO: loop through each tile in global array: int tile_data[]
                  determine background color for each tile: (red or white)
                  determine the 2 digits to place on each tile: (01, 02, ..., 15)
                  determine color for the digits
                  once determined - call: initialize_tile() with the above info
         */
    
    
    
    
        /*     pseudocode:
    
    
        int column, row;
        int color;
        int tile_index, digit1, digit2;
    
    
        FOR each column on board
            FOR each row on board
                // calculate color for tile
                IF [(column + row) % 2] = 0 THEN color = WHITE
                ELSE color = RED
    
    
                // calculate digits for tile
                tile_index = column + row * 4
                digit1 = (tile_index + 1) / 10
                digit2 = (tile_index + 1) % 10
    
    
                // finally, call initialize tile with data calculated above
                CALL initialize_tile() with tile_index, digit1, digit2, color, BLACK
            }
        }
    
    
        // set all pixels to BLACK for blank tile
        FOR each pixel in tile
            tile_data[15][pixel] = BLACK;
        */
    
    
    
    
    }
    do I use use color as well as tile_index, digit1, and digit2 as parameters? any help would be great, thanks!
    Last edited by mgravier; 03-21-2013 at 09:57 AM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I think the prototype has as arguments the dimensions of the array...So it should be like this
    Code:
    void initialize_tiles(int tile_data[][], int COL, int ROW){
        ...
    ?
    Right?
    If so...

    I am going to do this
    Code:
     FOR each column on board
            FOR each row on board
    to code
    Code:
    for( column = 0 ; column < COL ; column++ )
    {
          for( row = 0 ; row < ROW ; row++ )
          {
                     .......
    can you take from there?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    16
    great, thanks! how about color, tile_index, digit1, and digit2? do I put those as parameters as well? Also, what would go inside the for loop (how do I calculate the color for each tile)? I have edited my code in order to understand the problem better.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Now it's your turn to give it a shot. Read your post and try to answer your questions. Yes, that's recursion
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing writing code to reading code
    By binks in forum C Programming
    Replies: 8
    Last Post: 06-12-2012, 09:41 AM
  2. writing good code is like writing an artistic expression
    By renzokuken01 in forum C Programming
    Replies: 5
    Last Post: 02-03-2011, 08:48 PM
  3. how to convert a c source code into a pseudocode?
    By blogchama in forum C Programming
    Replies: 2
    Last Post: 01-20-2010, 03:07 PM
  4. writing the pseudocode........functions
    By bajan in forum C++ Programming
    Replies: 3
    Last Post: 04-15-2005, 12:59 PM
  5. self-writing code?
    By genghis in forum C++ Programming
    Replies: 19
    Last Post: 01-15-2003, 04:13 PM