-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
130 lines (116 loc) · 3.19 KB
/
main.cc
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
#include <iostream>
#include <array>
#include <unistd.h>
#include <fstream>
//#include <ncurses.h>
std::array<char, 111> maze_arr{};
const char WALL = '#';
struct player{
const char PLAYER = 'M';
char location = 48;
};
unsigned int index_num = 0;
void load_maze(std::array<char, 111> &maze, std::ifstream &file);
void clear();
char get_input();
void char_swap(char &first, char last, std::array<char, 111> &arr);
short position_checker(unsigned char location, char next);
player me{};
int main(){
maze_arr.fill(' ');
//maze_arr[10] = '\n';
/*code for basic empty maze
for(index_num; index_num < 10; ++index_num)
{
if(index_num > 0){
maze_arr[(index_num * 11) - 2] = WALL;
maze_arr[(index_num * 11)] = WALL;
maze_arr[110 - (11 - index_num )] = WALL;
maze_arr[(index_num * 11) - 1] = '\n';
}
maze_arr[index_num] = WALL;
//std::cout << index_num << '\n';
}*/
std::ifstream maze_file{"maze.txt"};
load_maze(maze_arr, maze_file);
maze_arr[me.location] = me.PLAYER;
//maze_arr[110] = '\n';
clear();
for(unsigned int i=0; i < 110; ++i){
std::cout << maze_arr[i];
}
std::cout << '\n';
char maze_input;
// unsigned char player_step=0;
//maze_arr[78] = 'F';
while(1)
{
maze_input = get_input();
//switch maze_input
std::cout << maze_input << '\n';
char_swap(me.location, me.location+maze_input, maze_arr);
me.location += maze_input;
clear();
for(unsigned int i=0; i < 110; ++i){
std::cout << maze_arr[i];
}
}
return 0;
}
void clear() {
// CSI[2J clears screen, CSI[H moves the cursor to top-left corner
std::cout << "\x1B[2J\x1B[H";
};
char get_input()
{
char i;
std::cin >> i;
switch(i)
{
case 'w':
if(position_checker(me.location, -11) == 0) break;
if(position_checker(me.location, -11) == 2){std::cout<<"You did it!" << std::endl; usleep(5000000); break;}
i = -11;
return i;
case 'a':
if(position_checker(me.location, -1) == 0) break;
if(position_checker(me.location, -1) == 2){std::cout<<"You did it!" << std::endl; usleep(5000000); break;}
i = -1;
return i;
case 's':
if(position_checker(me.location, 11) == 0) break;
if(position_checker(me.location, 11) == 2){std::cout<<"You did it!" << std::endl; usleep(5000000); break;}
i = 11;
return i;
case 'd':
if(position_checker(me.location, 1) == 0) break;
if(position_checker(me.location, 1) == 2){std::cout<<"You did it!" << std::endl; usleep(5000000); break;}
i = 1;
return i;
default:
get_input();
}
return 0;
};
void char_swap(char &first, char last, std::array<char, 111> &arr){
char last_cp = arr[last];
arr[last] = arr[first];
arr[first] = last_cp;
};
short position_checker(unsigned char location, char next){
if(maze_arr[location+next] == '#') return 0;
else if(maze_arr[location+next] == 'F') return 2;
else return 1;
};
void load_maze(std::array<char, 111> &maze, std::ifstream &file){
//file.open();
char c;
for(unsigned short i=0; i < 111; ++i)
{
c=file.get();
std::cout << c << i << std::endl;
maze[i] = c;
}
usleep(5000000);
file.close();
};