Making a moving item controlled by using WASD

Hey guys, I'm new here, but so far I like this place :)

Okay so for the problem:
I'm working on a personal project with a screen that looks like this:
+++++
+++++
++O++
+++++
+++++

And below that it asks where you want to move. I've done the majority of the work, I just have to figure out how to actually move the O. Here's my source code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;

char myScreen[ 5 ][ 5 ]
{//    0    1    2    3    4
    { '+', '+', '+', '+', '+' },//0
    { '+', '+', '+', '+', '+' },//1
    { '+', '+', 'O', '+', '+' },//2
    { '+', '+', '+', '+', '+' },//3
    { '+', '+', '+', '+', '+' } //4
};
int location[ 2 ] { 2 };

int moveScreen( char input )
{
    if ( input == 'W' )
    {
        //need help here
    }
    else if ( input == 'A' )
    {
        //and here
    }
    else if ( input == 'S' )
    {
        //and here
    }
    else if ( input == 'D' )
    {
        //and here :)
    }
}

int drawScreen( int arraySize )
{
    cout << ".-----------.\n| ";

    for ( int i = 0; i < arraySize; ++i )
    {
        for ( int j = 0; j < arraySize; ++j )
        {
            cout << myScreen[ i ][ j ] << " ";
        }
        if ( i < 4 )
            cout << "|\n| ";
        else
            cout << "|\n";
    }

    cout << "'-----------'\n\t W\n\tASD\nMove: ";
}

int main()
{
    const int arraySize = 5;
    char input = 'W';

    while ( input == 'W' || input == 'A' || input == 'S' || input == 'D' )
    {
        moveScreen( input );
        drawScreen( arraySize );

        cin >> input;
        input = toupper(input);
    }
}


Any ideas?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
int location[ 2 ] = { 2, 2 };

void moveScreen( char input )
{
    if ( input == 'W' )
    {
        if ( location[0] > 0 )
        {
            myScreen[location[1]][location[0]] = '+' ;
            myScreen[location[1]][--location[0]] = 'O'; 
        }
    }
    ...


Note that neither moveScreen nor drawScreen return values. Stop telling the compiler they should. This code assumes that location[0] is the x value and location[1] is the y value.
ah thank you! this made my life easier :)
Topic archived. No new replies allowed.