Maze thingy (Experiment)

You can expand it and also paint pictures!

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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <Windows.h>
using namespace std;

#define UP 72
#define LEFT 75
#define RIGHT 77
#define DOWN 80


char Area [10][10] =
    {{'0','0','0','0','0','0','0','0','0','0'},
    {'0',' ',' ',' ',' ',' ',' ',' ',' ','0'},
    {'0',' ',' ',' ',' ',' ',' ',' ',' ','0'},
    {'0',' ',' ',' ',' ',' ',' ',' ',' ','0'},
    {'0',' ',' ',' ',' ',' ',' ',' ',' ','0'},
    {'0',' ',' ',' ',' ',' ',' ',' ',' ','0'},
    {'0',' ',' ',' ',' ',' ',' ',' ',' ','0'},
    {'0',' ',' ',' ',' ',' ',' ',' ',' ','0'},
    {'0',' ',' ',' ',' ',' ',' ',' ','H','0'},
    {'0','0','0','0','0','0','0','0','0','0'}};

void print_world(char Area[10][10],const int number,int& the_x,int& the_y);
void clear_screen();
bool check_collision(char Area[][10], int the_size, int dir1, int dir2, int the_x, int the_y);
bool check_win(char Area[][10],int the_size,int dir1, int dir2, int the_x, int the_y);
bool check_trapped(char Area[][10],int dir1, int dir2, int the_x, int the_y);
void setcursor(bool, DWORD);

int main(int nNumberofArgs,char* pszArgs[])
{
    setcursor(0, 0);
	char dummy = ' ';
	int x=1, y=1;
	bool collision;
	bool win = false;
	bool trapped = false;
	print_world(Area,10,x,y);
	string direction;

    for(;;)
    {
        clear_screen();
		int ch = getch();
		direction = "      ";
		switch(ch)
		{
		case RIGHT:
				collision=check_collision(Area,10,0,1,x,y);
				if(!collision)
				{
					x++;
					Area[y][x - 1] = '0';
					direction="East";
					win = check_win(Area,10,0,1,x,y);
				}
				trapped = check_trapped(Area,0,1,x,y);
				break;
		case LEFT:
				collision=check_collision(Area,10,0,-1,x,y);
				if(!collision)
				{
					x--;
					Area[y][x + 1] = '0';
					direction="West";
					win = check_win(Area,10,0,-1,x,y);
				}
				trapped = check_trapped(Area,0,-1,x,y);
				break;
		case UP:
				collision=check_collision(Area,10,-1,0,x,y);
				if(!collision)
				{
					y--;
					Area[y + 1][x] = '0';
					direction="North";
					win = check_win(Area,10,-1,0,x,y);
				}
				trapped = check_trapped(Area,-1,0,x,y);
				break;
		case DOWN:
				collision=check_collision(Area,10,1,0,x,y);
				if(!collision)
				{
					y++;
					Area[y - 1][x] = '0';
					direction="South";
					win = check_win(Area,10,1,0,x,y);
				}
				trapped = check_trapped(Area,1,0,x,y);
				break;
		}

		print_world(Area,10,x,y);
		if(collision)
        {
            direction = "  NO ";
        }

        if(win)
        {
              direction = "    You've made it home!";
        }

        if(trapped)
        {
            direction = "    You're trapped!";
        }

		cout << direction << endl;
		if(win || trapped)
        {
            system("PAUSE");
            return 0;
        }
	}

	cin >> dummy;
	return 0;
}

void print_world(char Area[][10],const int number, int& the_x, int& the_y)
{
	for(int i=0;i<number;i++)
	{
		for(int j=0;j<number;j++)
		{
		    if(the_x==j && the_y==i)
            {
                cout << 'x';
            }
            else if(Area[i][j]=='0')
				cout << '\xB2';
			else
				cout << Area[i][j];

		}
		cout<<endl;
	}
}

bool check_collision(char Area[][10], int the_size, int dir1, int dir2,int the_x, int the_y)
{
	if(Area[the_y+dir1][the_x+dir2] == '0')
		return true;
	else
		return false;
}

bool check_win(char Area[][10],int the_size,int dir1, int dir2,int the_x,int the_y)
{
    if(Area[the_y+dir1][the_x+dir2] == 'H')
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool check_trapped(char Area[][10],int dir1,int dir2,int the_x,int the_y)
{
    if(Area[the_y+dir1][the_x+dir2] == '0' && Area[the_y+dir1][the_x-dir2] == '0' && Area[the_y+dir2][the_x+dir1] == '0' && Area[the_y+dir2][the_x-dir1] == '0' &&
       Area[the_y-dir1][the_x+dir2] == '0' && Area[the_y-dir1][the_x-dir2] == '0' && Area[the_y-dir2][the_x+dir1] == '0' && Area[the_y+dir2][the_x-dir1] == '0')
    {
        return true;
    }
    else
    {
        return false;
    }
}

void clear_screen()
{
	HANDLE handle_out;
	COORD position;

	handle_out=GetStdHandle(STD_OUTPUT_HANDLE);//std output handle is the monitor

	position.X=0;
	position.Y=0;

	SetConsoleCursorPosition(handle_out,position);//sets cursor in the specified position
}

void setcursor(bool visible, DWORD size)
{
	HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
	if(size == 0)
	{
		size = 20;	// default cursor size Changing to numbers from 1 to 20, decreases cursor width
	}
	CONSOLE_CURSOR_INFO lpCursor;
	lpCursor.bVisible = visible;
	lpCursor.dwSize = size;
	SetConsoleCursorInfo(console,&lpCursor);
}
Topic archived. No new replies allowed.