954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Moveable character?

Hey.
I am wanting to make this program so that the user can move the position of his characters. I have made a attempt but i am confused about some bits.

// Code Shark

#include <iostream>

using namespace std;

int main() {
    char character = 'X', movement;
    
    do {
        system("Cls");
        
        cout << "Do you want your character to move [L]eft, [R]ight, [U]p or";
        cout << " [D]own or [E]xit: ";
        cin >> movement;
        
        switch (movement)
        {
               case 'L':
                    cout << "\b" << character;
                    break;
                    
               case 'R':
                    cout << " " << character;
                    break;
                    
               case 'U':
                    //cout <<  ???;
                    break;
                    
               case 'D':
                    cout << endl << character;
                    /* the thing is if i just go endl here the character will
                       go straight to the start of the new line and most likelys
                       the character will be spaces away from it. */
                    break;
                    
               case 'E':
                    break;
        }
    }
    while(movement != 'E');
    
    cin.get();
    
    return 0;
}


Some of the problems include: At the start of the Do loop the characters position will be deleted in my code because of the system clear. Another worry is that when the user wants to move up or down the character will be brought to the start of the line and that may not be directly under/above the last position. If you could help i would be very graceful.

Code Shark
Newbie Poster
14 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

Hey. I am wanting to make this program so that the user can move the position of his characters. I have made a attempt but i am confused about some bits.

// Code Shark

#include <iostream>

using namespace std;

int main() {
    char character = 'X', movement;
    
    do {
        system("Cls");
        
        cout << "Do you want your character to move [L]eft, [R]ight, [U]p or";
        cout << " [D]own or [E]xit: ";
        cin >> movement;
        
        switch (movement)
        {
               case 'L':
                    cout << "\b" << character;
                    break;
                    
               case 'R':
                    cout << " " << character;
                    break;
                    
               case 'U':
                    //cout <<  ???;
                    break;
                    
               case 'D':
                    cout << endl << character;
                    /* the thing is if i just go endl here the character will
                       go straight to the start of the new line and most likelys
                       the character will be spaces away from it. */
                    break;
                    
               case 'E':
                    break;
        }
    }
    while(movement != 'E');
    
    cin.get();
    
    return 0;
}

Some of the problems include: At the start of the Do loop the characters position will be deleted in my code because of the system clear. Another worry is that when the user wants to move up or down the character will be brought to the start of the line and that may not be directly under/above the last position. If you could help i would be very graceful.


I think the way to go is to keep track of the (x,y) coordinates of the character. When you move the character, decrement or increment the x or y coordinate of the character, then display it. The program below is a modification of your program where the character is represented by 'O' and it can move around in a 5 x 5 character grid. Empty spaces are denoted by 'X'. (xPos,yPos) represent the character's current location and the 5 x 5 grid is redrawn each trip through the do-while loop.

#include <iostream>
using namespace std;


int main ()
{
    char  grid[5][5];
    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            grid[i][j] = 'X';
        }
    }
    char movement;
    int xPos = 2;
    int yPos = 2;
    
    do 
    {
        grid[xPos][yPos] = 'O';
        system("Cls");
        
        for (int i = 0; i < 5; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                cout << grid[j][i];
            }
            
            cout << endl;
        }
        
        cout << "Do you want your character to move [L]eft, [R]ight, [U]p or";
        cout << " [D]own or [E]xit: ";
        cin >> movement;

        grid[xPos][yPos] = 'X';
        
        switch (movement)
        {
               case 'L':
                    if (xPos > 0)
                    {
                        xPos--;
                    }
                    break;
                    
               case 'R':
                    if (xPos < 4)
                    {
                        xPos++;
                    }
                    
               case 'U':
                    if (yPos > 0)
                    {
                        yPos--;
                    }
                    break;
                    
               case 'D':
                    if (yPos < 4)
                    {
                        yPos++;
                    }
                    break;
                    
               case 'E':
                    break;
        }
    }
    while(movement != 'E');
    
    cin.get();
    
    return 0;
}
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You