How do get enemies to run around the maze game

I managed to write a maze game but I wished to add enemies to run around. Can somebody tell me how to do this?

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <iostream>
#include <windows.h>
#include <conio.h>
using namespace std;

char input;

int maze[7][18]={{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
				 {1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,1},
				 {1,0,1,1,0,1,0,1,1,1,1,1,1,1,1,0,0,1},
				 {1,0,1,1,0,1,0,1,0,0,0,0,0,0,0,0,1,1},
				 {1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1},
				 {1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0},
				 {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};

void draw();
void gotoXY(int, int);
void movement();

int main()
{
	for(int i=0; i<20; i++)
	{
		cout<<"=";
	}
	cout<<endl;
	
	cout<<"THE BEST SELLING GAME EVER!"<<endl;
	
	int title[5][20]={{0,1,1,1,1,1,0,1,1,1,0,1,1,1,1,0,1,1,1,1},
					   {0,1,0,1,0,1,0,1,0,1,0,0,0,1,0,0,1,0,0,0},
					   {0,1,0,1,0,1,0,1,1,1,0,0,1,0,0,0,1,1,1,1},
					   {0,1,0,1,0,1,0,1,0,1,0,1,0,0,0,0,1,0,0,0},
					   {0,1,0,1,0,1,0,1,0,1,0,1,1,1,1,0,1,1,1,1}};
	
	for(int i=0; i<5; i++)
	{
		for(int j=0; j<20; j++)
		{
			switch(title[i][j])
			{
				case 0: cout<<" "; break;
				case 1: cout<<char(219); break;
			}
		}
		cout<<endl;
	}
	
	cout<<"Press F to play!"<<endl;
	char key;
	cin>>key;
	if((key=='f')||(key=='F'))
	{
		system("CLS");
		draw();
		movement();
	}
	
	else
	{
		cout<<"MOFO!!";
		return 0;
	}

	//system("pause");
	return 0;
}

void draw()
{
	for(int i=0; i<7; i++)
	{
		for(int j=0; j<18; j++)
		{
			switch(maze[i][j])
			{
				case 0: cout<<" "; break;
				case 1: cout<<char(219); break;
				//case 2: cout<<" "; break;
			}
		}
		cout<<endl;
	}
}

void gotoXY(int x, int y)
{
	COORD coord;
	coord.X=x;
	coord.Y=y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}

void movement()
{
	int x=1;
	int y=1;
	int p=1;
	int q=1;
	
	while(1)
	{
		gotoXY(x,y);
		cout<<char(1);
	
		gotoXY(p,q);
		cout<<" ";
		
		if(x==17)
		{
			cout<<"you win";
			exit(1);
		}

		input=_getch();
	
		p=x;
		q=y;

		if(input=='w')
			y-=1;
			
		else if(input=='s')
			y+=1;
			
		else if(input=='a')
			x-=1;
			
		else if(input=='d')
			x+=1;			
	
		if(maze[y][x]==1)
		{
			x=p;
			y=q;
		}
	
		if((x==6)&&(y==3))
		{
			gotoXY(6,3);
			cout<<char(219);
			
			x=p;
			y=q;
		}
		
		if((x==7)&&(y==4))
		{
				gotoXY(7,4);
				cout<<char(219);
				x=p;
				y=q;
				gotoXY(8,1);
				cout<<"YOU ARE TRAPPED!!!";
		}
	}

	
}
what do you want them to do?

you can plop them down somewhere and every iteration you can randomly try to move NSEW if possible (only pick directions that it CAN move in).

you can do a pacman-ghost that computes the path to the player and always chases toward the player, or tries to cut him off by getting in front

you can have them solve the maze, with a recursive wall follow exit seek algorithm (AKA uncle trapspringers solution: while not escaped: go forward if possible, if not possible, rotate left)

to name 3 approaches..

Last edited on
Registered users can post here. Sign in or register to post.