Console game enemies

I just finished the code for enemies in my little console game (it's almost all from a tutorial - but I know what every single line of the code does)

All this code does is make the letters 'S' randomly move around the map, at least it's supposed to do that, but it doesn't work...

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
80
81
82
83
84
85
#include <iostream>
#include <windows.h>
#include <ctime>

char Map[20][40] =
{"#######################################",
 "#                                     #",
 "#                                     #",
 "#                  S                  #",
 "#     S                               #",
 "#                                     #",
 "#                                     #",
 "#                                     #",
 "#            S             S          #",
 "#                                     #",
 "#                                     #",
 "#                        S            #",
 "#     S                               #",
 "#                                     #",
 "#######################################"};

short random = 0;
bool running = true;

int main()
{

 while(running)
 {
    system("cls");
    for (int i = 0; i < 20; i++)
    {
    std::cout << Map[i] << "\n";
    }

    for (int enemy_y = 0; enemy_y < 20; enemy_y++)
    {
    for (int enemy_x = 0; enemy_x < 40; enemy_x++)
    {
        switch (Map[enemy_y][enemy_x])
        {
            case 'S':
            random = rand() % 4 + 1;

            switch(random)
            {
                case 1:
                if (Map[enemy_y-1][enemy_x] != '#' && Map[enemy_y-1][enemy_x] != 'S')
                {
                Map[enemy_y-1][enemy_x] = ' ';
                Map[enemy_y][enemy_x] = 'S';
                } break;

                case 2:
                if (Map[enemy_y+1][enemy_x] != '#' && Map[enemy_y+1][enemy_x] != 'S')
                {
                Map[enemy_y+1][enemy_x] = ' ';
                Map[enemy_y][enemy_x] = 'S';
                } break;

                case 3:
                if (Map[enemy_y][enemy_x-1] != '#' && Map[enemy_y][enemy_x-1] != 'S')
                {
                Map[enemy_y][enemy_x-1] = ' ';
                Map[enemy_y][enemy_x] = 'S';
                } break;

                case 4:
                if (Map[enemy_y][enemy_x+1] != '#' && Map[enemy_y][enemy_x+1] != 'S')
                {
                Map[enemy_y][enemy_x+1] = ' ';
                Map[enemy_y][enemy_x] = 'S';
                } break;
            }
            break;
        }
    }
    }

    Sleep(100);
    random = 0;
 }

return 0;
}


Can anyone help me fix this?
Last edited on
(This example reminds me of sparse matrices
http://en.wikipedia.org/wiki/Sparse_matrix)

Replace the ' ' and 'S' in all 4 cases:
1
2
                Map[enemy_y][enemy_x+1] = 'S';
                Map[enemy_y][enemy_x] = ' ';

Try storing the x and y coordinates of the 'S' in the map instead of storing the whole map.

If you know what data structures and functions are, use them. It'll make coding much simpler.
I tried running your code. It runs slowly.

I once made a function which sets the cursor position at (0,0) using the Windows API. Using that function instead of system("cls") increases the speed of the game.
*Facepalm at not realizing the 'S's and spaces had to be the other way around, just overlooked that*

I am quite new to structures, but I can store the position of the player in a structure called player, now how are you supposed to store the locations of enemies when there are many on a map????
The player is by himself - so you just change the initial position for the player, before the next map is displayed, but if there are multiple enemies...

And yes, I know system("whatever") is bad, I also have a function for it.
I only wrote system("cls") so I wouldn't have to write all of the code, only the parts that cause problems (like I said this is just part of a game I'm making, so enemies are only part of it).

I found the full article on clearing the screen: http://www.cplusplus.com/articles/4z18T05o/
Last edited on
To store multiple enemies, you can create an array or use std::vector.

1
2
3
4
5
6
7
struct position
{
    unsigned int x;    //x-coordinate of an object
    unsigned int y;    //y-coordinate of an object
};

position enemy_list[13];    //a list of 13 enemies 
I decided I should rewrite the code, where instead of saying where enemies can't go, I'd tell where they can go (less code, better structure as the game expands).

For now - problem solved :)
Topic archived. No new replies allowed.