|
1 | 1 | //============================================================================ |
2 | 2 | // Name : mmSim.cpp |
3 | 3 | // Author : Elis Pogace |
4 | | -// Version : 0.1 |
| 4 | +// Version : 1.0 |
5 | 5 | // Created on : Jun 11, 2014 |
6 | 6 | // Description : Micro mouse simulation software for SFHS robotics students |
7 | 7 | //============================================================================ |
8 | 8 |
|
9 | | - |
10 | 9 | #include"mazeBase.h" |
11 | 10 | #include"mazeConst.h" |
12 | 11 | #include"gui.h" |
13 | 12 | #include"studentAi.h" |
14 | 13 | #include"time.h" |
15 | | - |
16 | | -#define win32 1 |
| 14 | +#include"string.h" |
17 | 15 |
|
18 | 16 | #include <dirent.h> |
19 | 17 | #include<iostream> |
|
23 | 21 |
|
24 | 22 | using namespace cv; |
25 | 23 |
|
26 | | -static int filter(const struct dirent* input) |
27 | | -{ |
28 | | - const char* ext = string(".maz").c_str(); |
29 | | - |
30 | | - if (input->d_name[0] == '.') |
31 | | - return 0; |
32 | | - else |
33 | | - { |
34 | | - size_t j = (strlen(input->d_name)-4); |
35 | | - for (int i = 0; j < (strlen(input->d_name)); i++, j++) |
36 | | - { |
37 | | - if (ext[i] != (input->d_name[j])) |
38 | | - return 0; |
39 | | - } |
40 | | - return 1; |
41 | | - } |
42 | | -} |
43 | 24 |
|
44 | 25 | int main() |
45 | 26 | { |
| 27 | + |
46 | 28 | int userInput; |
47 | 29 |
|
48 | 30 | struct baseMapNode start[MAZE_WIDTH][MAZE_HEIGHT]; |
49 | 31 | struct mouseData mouse; |
50 | | - FILE *mazeFile; |
| 32 | + FILE *mazeFile = NULL; |
51 | 33 |
|
52 | 34 | //new maze construction |
53 | 35 | newMazeArea(start, MAZE_WIDTH, MAZE_HEIGHT); |
@@ -103,78 +85,48 @@ int main() |
103 | 85 | } |
104 | 86 | else if(userInput == 'l') |
105 | 87 | { |
106 | | -#ifdef win32 |
107 | 88 | DIR *local = opendir("."); |
108 | 89 | struct dirent *dircontents = readdir(local); |
109 | 90 | int selection; |
| 91 | + int fileNum =0; |
| 92 | + vector<std::string> fileNames; |
110 | 93 | std::ostringstream buff (""); |
111 | 94 |
|
| 95 | + fileNames.clear(); |
112 | 96 | do{ |
113 | | - buff << telldir(local) << ": " << dircontents->d_name; //setup output string |
114 | | - cvPrint(&guiText,buff.str()); |
115 | | - cout << buff.str() <<endl; |
116 | | - buff.str("");//clear string buffer for next loop |
117 | | - buff.clear(); |
| 97 | + if(strstr(dircontents->d_name, ".maz")) //check to see if it is a maze file |
| 98 | + { |
| 99 | + //print selection number and filename |
| 100 | + buff << fileNum+1 << ": " << dircontents->d_name; |
| 101 | + fileNames.push_back(dircontents->d_name); |
| 102 | + cvPrint(&guiText,buff.str().c_str()); |
| 103 | + fileNum++; |
| 104 | + |
| 105 | + //clear string buffer for next loop |
| 106 | + buff.str(""); |
| 107 | + buff.clear(); |
| 108 | + } |
118 | 109 | dircontents = readdir(local); |
119 | 110 | }while(dircontents != NULL); |
120 | 111 |
|
121 | 112 | do { |
122 | 113 | cout << endl << "Select file to load: " << endl; |
123 | | - selection = atoi(cvIn(&guiText).c_str()); |
124 | | - rewinddir(local); |
125 | | - seekdir(local, selection - 1); |
126 | | - dircontents = readdir(local); |
| 114 | + selection = atoi(cvIn(&guiText).c_str())-1; |
| 115 | + if(selection >= 0 && selection < fileNum) //check if user input is within range |
| 116 | + mazeFile = fopen(fileNames.at(selection).c_str(), "r"); |
| 117 | + else |
| 118 | + mazeFile = NULL; |
127 | 119 |
|
128 | 120 | if(!cvGetWindowHandle(DISPLAY_WINDOW_NAME)) break; //if the user closed the window break |
129 | | - } while (dircontents == NULL || dircontents->d_type != 0); |
| 121 | + } while (mazeFile == NULL); |
130 | 122 | cvPrint(&guiText, ""); |
131 | | - mazeFile = fopen(dircontents->d_name, "r"); |
132 | | - if(mazeFile == NULL) |
133 | | - { |
134 | | - exit(101); |
135 | | - } |
| 123 | + |
136 | 124 | readMazeFromFile(mazeFile, start); |
| 125 | + |
137 | 126 | cout << "Maze loaded" << endl; |
138 | 127 |
|
139 | 128 | fclose(mazeFile); |
140 | 129 | redrawMaze(&image, start); |
141 | | -#else |
142 | | - struct dirent **dircontents; |
143 | | - string choices[20]; |
144 | | - int dirnum, selection; |
145 | | - |
146 | | - cout << endl << "Select file to load: " << endl; |
147 | | - |
148 | | - dirnum = scandir("./", &dircontents, filter, alphasort); |
149 | | - if (dirnum >= 0) |
150 | | - { |
151 | | - for (int i = 0; i < dirnum; i++) |
152 | | - { |
153 | | - choices[i].assign(dircontents[i]->d_name); |
154 | | - cout << i << ") " << choices[i] << endl; |
155 | | - } |
156 | | - } |
157 | | - |
158 | | - cout << endl; |
159 | | - |
160 | | - //load file |
161 | | - cin >> selection; |
162 | | - |
163 | | - if (selection <= dirnum && selection <= 20) |
164 | | - { |
165 | | - mazeFile = fopen(choices[selection].c_str(), "r"); |
166 | | - if(mazeFile != NULL) |
167 | | - { |
168 | | - readMazeFromFile(mazeFile, start); |
169 | | - cout << "Maze loaded" << endl; |
170 | | - } |
171 | | - fclose(mazeFile); |
172 | | - redrawMaze(&image, start); |
173 | | - } |
174 | | - |
175 | | - else |
176 | | - cout << "Invalid selection, try another action" << endl; |
177 | | -#endif |
178 | 130 | } |
179 | 131 |
|
180 | 132 | else if(userInput == 'r') |
|
0 commit comments