Home / utk / cs102 / fa16 / labh / code_snippets / map.cpp
Directory Listing
map.cpp
map.txt
matrix1.txt
matrix_example.txt
result.txt
vector1.txt
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
/*
 * COSC 102 - Map loading example
 * 
 * Description:
 *     Utilises 2D arrays to place entries in a "map" for people to play in.
 *     Also simulates the simplist of game design, just for brownie points. :P
 * 
 * Author:
 *     Clara Van Nguyen
 */

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

const int MAP_WIDTH  = 10;
const int MAP_HEIGHT = 10;

class player {
	public:
		void set_pos(int, int);
		int  get_X() const;
		int  get_Y() const;
		void go_left();
		void go_right();
		void go_up();
		void go_down();

	private:
		int x, y;
};

class gamemap {
	public:
		bool set(const int, const int, const char);
		char get(const int, const int) const;
		bool load(const string&);
		int get_spawn_x() const;
		int get_spawn_y() const;
		void print(player&);
	private:
		char arr[MAP_WIDTH][MAP_HEIGHT];
		int spawn_x, spawn_y;
};

int main() {
	char action;
	string fname;
	gamemap map;
	player me;
	cout << "Input map file: ";
	cin >> fname;
	map.load(fname);
	me.set_pos(map.get_spawn_x(), map.get_spawn_y());
	while (1) {
		system("clear");
		map.print(me);
		cout << "Where to go? (w/a/s/d): ";
		cin >> action;
		switch (action) {
			case 'w': me.go_up();    break;
			case 'a': me.go_left();  break;
			case 's': me.go_down();  break;
			case 'd': me.go_right(); break;
		}
	}
}

bool gamemap::set(const int a, const int b, const char val) {
	if (a < 0 || a >= MAP_WIDTH || b < 0 || b >= MAP_HEIGHT)
		return false;
	arr[a][b] = val;
	return true;
}

char gamemap::get(const int a, const int b) const {
	if (a < 0 || a >= MAP_WIDTH || b < 0 || b >= MAP_HEIGHT)
		return '\0';
	return arr[a][b];
}

bool gamemap::load(const string& fname) {
	ifstream fp;
	fp.open(fname); //C++11 only
	
	if (fp.fail()) {
		cout << "Unable to open map file" << endl;
		return false;
	}

	spawn_x = -1;
	spawn_y = -1;

	for (int b = 0; b < MAP_HEIGHT; b++) {
		for (int a = 0; a < MAP_WIDTH; a++) {
			if (!(fp >> arr[a][b])) {
				cout << "Unable to read from map file" << endl;
				return false;
			}
			//arr[a][b] = str[0];
			if (arr[a][b] == 'S') {
				spawn_x = a;
				spawn_y = b;
				arr[a][b] == 'B';
			}
		}
	}
	fp.close();
	return true;
}

int gamemap::get_spawn_x() const {
	return spawn_x;
}

int gamemap::get_spawn_y() const {
	return spawn_y;
}

void gamemap::print(player& me) {
	for (int b = 0; b < MAP_HEIGHT; b++) {
		for (int a = 0; a < MAP_WIDTH; a++) {
			if (me.get_X() == a && me.get_Y() == b)
				cout << "P ";
			else {
				switch (arr[a][b]) {
					case '#':
						//Wall
						cout << "# ";
						break;
					case 'B':
						//Blank
						cout << "  ";
						break;
					default:
						cout << "  ";
						break;
				}
			}
		}
		cout << endl;
	}
}

void player::set_pos(const int _x, const int _y) {
	x = _x;
	y = _y;
}

int player::get_X() const {
	return x;
}

int player::get_Y() const {
	return y;
}

void player::go_up() {
	y -= 1;
}

void player::go_down() {
	y += 1;
}

void player::go_left() {
	x -= 1;
}

void player::go_right() {
	x += 1;
}