Thread: 2d array help!

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    17

    2d array help!

    Hello all,

    I am working on creating a simple sketch pad, which will be a console program. I got the idea from "C++ How to Program" from Deitel & Deitel. If any of you know of Turtle Graphics, then you'll know what I'm talking about. If not, then here's what I'm having problems with.

    The "pad" is going to be a 15x60 array that will start off with all elements initialized to a blank (i.e. space character). The user can press 'w' to move their "pen" up, 's' for down, 'a' for left, and 'd' for right. If they press the spacebar, this sets the point of the pen "down", and wherever they move the pen on the pad (array), their chosen sketch character (say, for example, an asterix) will be plotted. This will happen until they press the spacebar again to "raise" the pen. When the pen is raised, they can move about freely without drawing anything.

    Here's my problem: I'm trying to develop an alg. and write pseudocode to accomplish the above. However, I'm having trouble figuring out how to keep track of where the pen is. The pen will be a visible character on the pad array (I have it set to '+' right now), so if they pen passes over a character and the pen is not DOWN (i.e. not drawing, just moving the pen around), the pen must replace the character in the position its on TEMPORARILY so that when the user moves the pen again, that character will still be in its original location (because the pen point is not down, there's no drawing.) Does anyone have any suggestions, preferably a snippet of code? I'd really appreciate it.

    There are other things to check for of course (i.e. if the user moves right, will the array bounds be exceeded; I can do this on my own). The only thing I really need help w/is what I mentioned above. Any comments or suggestions are welcome as well. This is my most ambitious project, and I am really excited about it. Thanks for all of your help!

    Sincerely,

    cpp4ever
    ------------------------------------------
    Normally I'd put something clever, but that isn't the point of being here now is it?

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    Here's an algorhythm that relates how I would try solving this problem

    develop function to check for valid move (so don't overwrite array)

    develop function to display pad

    develop function to display menu to change pen from draw to no draw, moves available to user, and exit option

    in main():

    declare pad as 2d array of char

    initialize pad to spaces

    declare pen to be of type char and initialize it to n

    declare player as type char and initialize to + sign

    position player in initial position of pad

    declare marker as type char--user will choose this; can't be + sign.

    declare input to be of type char.

    ask user for marker char to be used in current game

    display board;

    display menu;

    ask user for input;

    in an unending loop:
    use switch statement to determine how to use input

    case spacebar
    if pen == n
    pen = y
    else if pen == y
    pen = n


    case e
    exit program

    cases w s a d
    check if valid move

    if valid move then:
    store current char in new square in temp variable
    change new square to + (player)
    change old square to whatever is in temp if pen == n
    change old square to marker if pen == y

    else if move not valid:
    display error message

    case default
    display error message

    end switch statement

    after each uiser selection from menu (except exit)
    clear screen
    display board
    display menu
    ask user for input

    end loop

    end main()display board
    display menu
    ask user for input

  3. #3
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    well...

    you could make something like this:

    bool down; //for if the pen is down or not
    bool changed=FALSE;

    //whenever they first hit the space bar, down is set to TRUE
    //the next space bar hit sets it to FALSE, then TRUE, etc.
    //You do THAT by this:

    //whenever space bar is hit:

    if(down==TRUE)
    down=FALSE;
    changed=TRUE;
    if(down==FALSE && changed==FALSE)
    down=TRUE;
    changed=FALSE;

    if(down==TRUE)
    array[5][5]=the users input;
    if(down==FALSE)
    array[5][5]=array[5][5];

    //then you redraw the array, with a function that shows all the
    //array spots.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  3. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM