Text Game

Made a simple text game but I cannot or dont even know where to begin on
adding walls.
Do I have to point out each location where the wall is going to be??
That would be pain. Is there an easier way?

Right now I only have 1 wall which is 0
The player is number 8
and the background is 1


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
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <conio.h>
#include <string>
#include <cstdlib>

using namespace std;

int main ()
{
    // Room Size
    int x = 10;
    int y = 10;

    // Some variables
    int grid[y][x];
    int pX = 0;
    int pY = 0;
    string pCommand;

    // Loop
    GameLoop:
    system("CLS");

    // Collison walls
    if (pCommand == "u"){
        if (pY > 0 && grid[pY-1][pX] != 0){
            pY -= 1;
        }
    }
    if (pCommand == "d"){
        if (pY < y-1 && grid[pY+1][pX] != 0){
            pY += 1;
        }
    }
    if (pCommand == "l"){
        if (pX > 0 && grid[pY][pX-1] != 0){
            pX -= 1;
        }
    }
    if (pCommand == "r"){
        if (pX < x-1 && grid[pY][pX+1] != 0){
            pX += 1;
        }
    }

    // Refresh/Placing everything
    for(int i=0; i<x; i++){
        for(int n=0; n<y; n++){
            grid[i][n] = 1;
            grid[pY][pX] = 8;
            grid[5][5] = 0;
            cout << grid[i][n];
        }
        cout << endl;
    }

    // Stuff
    cout << "\nX: " << pX;
    cout << "\nY: " << pY << endl;

    cout << "\n"
         << "u = up \n"
         << "d = down \n"
         << "l = left \n"
         << "r = right \n"
         << "e = exit \n";

    cout << "\nCommand: ";
    cin >> pCommand;

    if (pCommand == "e"){
        goto Exit;
    }

    goto GameLoop;

    Exit:
    getch();
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// The size of an array in C++ has to be constant but some compilers 
// accepts it anyway. The names x and y sounds like coordinates,
// naming them width and hight might make more sense. In that case
// you can use x and y as names instead of i and n because they are
// actually coordinates.
const int x = 10;
const int y = 10;

// You can initialize the grid this way
int grid[y][x] =
{
	8, 1, 0, 0, 0, 0, 0, 0, 0, 0,
	1, 1, 0, 1, 1, 1, 1, 1, 1, 0,
	0, 1, 0, 1, 0, 0, 0, 0, 1, 0,
	0, 1, 0, 1, 0, 0, 0, 1, 1, 0,
	0, 1, 0, 1, 1, 1, 0, 1, 1, 0,
	0, 1, 0, 0, 0, 1, 0, 1, 1, 0,
	0, 1, 1, 1, 1, 1, 0, 1, 1, 0,
	0, 0, 0, 0, 0, 0, 0, 1, 1, 0,
	0, 1, 1, 1, 1, 1, 1, 1, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};

Last edited on
Wow thanks a lot it worked!
But now when I type "u" I go right

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
int main ()
{
    // Room
    const int width = 10;
    const int height = 10;

    int grid[width][height];

    // Some variables
    int pX = 0;
    int pY = 0;
    string pCommand;

    // Loop
    GameLoop:
    system("CLS");

    // Collison walls
    if (pCommand == "u"){
        if (pY > 0 && grid[pY-1][pX] != 0){
            pY -= 1;
        }
    }
    if (pCommand == "d"){
        if (pY < height-1 && grid[pY+1][pX] != 0){
            pY += 1;
        }
    }
    if (pCommand == "l"){
        if (pX > 0 && grid[pY][pX-1] != 0){
            pX -= 1;
        }
    }
    if (pCommand == "r"){
        if (pX < width-1 && grid[pY][pX+1] != 0){
            pX += 1;
        }
    }

    // Refresh/Placing everything
    for(int x=0; x<width; x++){
        for(int y=0; y<height; y++){
            grid[width][height]=
            {
                1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
                1, 1, 0, 1, 1, 1, 1, 1, 1, 0,
                0, 1, 0, 1, 0, 0, 0, 0, 1, 0,
                0, 1, 0, 1, 0, 0, 0, 1, 1, 0,
                0, 1, 0, 1, 1, 1, 0, 1, 1, 0,
                0, 1, 0, 0, 0, 1, 0, 1, 1, 0,
                0, 1, 1, 1, 1, 1, 0, 1, 1, 0,
                0, 0, 0, 0, 0, 0, 0, 1, 1, 0,
                0, 1, 1, 1, 1, 1, 1, 1, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0
            };
            grid[pY][pX] = 8;
            cout << grid[y][x];
        }
        cout << endl;
    }
I think you have your x and y mixed up (i.e. grid[pX][pY]), but really it's just however you understand it.
Topic archived. No new replies allowed.