0

There is a easier way and more efficent way to do this with a for loop I believe...I can't put the nail on it can anyone help me out?

I want to move a character around in a array...right or left or up and down with md arrays.

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
   
    int map[3] =  {  1,0,0,};
    
    
    for (int i=0;i<3;i++)
    {  
    cout << map[i] <<  " ";
    }
    
     char move;
     cout << "[[R]ight\t";
     cin >> move;
     switch ( move )
     {
            case 'R':
                     int map[3] = { 0,1,0,};
                            for (int i=0;i<3;i++)
                                {  
                                cout << map[i] <<  " ";
                                }
                           }
    
    
    cin.get();
    
    return EXIT_SUCCESS;
}
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Kanoisa
0

Well if you keep a index to the array that says where the 1 is, you can easily just switch to change it or make a function or a class to encapsulate the lot.

basically you are looking for something like the following from what i can see.

int map[x][y]; //declare x*y sized array (need real numbers or consts here)
int xPos = 0; yPos = 0; //initial position of the 1 is at 0,0 (or whereever you like)

//display options ..say
1: MoveRight
2: MoveLeft
3: MoveUp
4: MoveDown

//get input and switch
//....

case(1): //right
    map[xPos][yPos] = 0; //clear the current square as we have moved the 1
    map[xPos+1][yPos] = 1; //put the 1 in the new location
    xPos++; //update the var with the correct location of the 1
 
//other cases would follow the same idea except xPos-1 for left, yPos+-1 for up and down

Hope thats the kind of effect your looking at.

As to using a for loop to print the thing, theres not really anything else for a c style array

Ps: in your switch you declare a new temp array, just use the array's name and dont make a new one or it wont save the change, unless thats what you want.

Edited by Kanoisa: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.