#include #include "network.h" using namespace std; // some xterm color definitions #define RED "\033[31m" #define NORM "\33[0m" #define GREEN "\033[32m" #define BLUE "\033[34m" using namespace std; //==myPause=========================================================== // pre: unix environment // post: causes execution to wait until key pressed //==================================================================== void myPause() { char ch = getchar(); } //==clearScreen======================================================= // pre: xterm environment // post: clears the xterm //==================================================================== void clearScreen() { cout << char(27) << "[;H" << char(27) << "[2J" << '\n'; } //==genNetwork======================================================== // pre: network is a char array of size (maxRow, maxCol); // and seed is for the rand() function // post: network is filled with random '0' and spaces //==================================================================== void genNetwork(char** network, int maxRow, int maxCol, unsigned int seed) { srand(seed); for(int i = 0; i < maxRow; i++) for(int j = 0; j < maxCol; j++) if(rand()%2 == 0) network[i][j] = '0'; else network[i][j] = ' '; network[0][0] = '0'; network[maxRow - 1][maxCol - 1] = '0'; } //==stepPossible====================================================== // pre: network is char matrix of size (maxRow and maxCol); r and c // are indices in the matrix (current location) // post: returns true is there is a step from the (r, c) location //==================================================================== bool stepPossible(char** network, int maxRow, int maxCol, int r, int c) { // east return ((c + 1 < maxCol && network[r][c + 1] == '0') || // northeast (r > 0 && c + 1 < maxCol && network[r - 1][c + 1] == '0') || // north (r - 1 >= 0 && c < maxCol && network[r - 1][c] == '0') || // northwest (r - 1 >= 0 && c -1 >= 0 && network[r-1][c-1] == '0') || // west (r > 0 && c - 1 >= 0 && network[r][c - 1] == '0') || // southwest (r + 1 < maxRow && c - 1 >= 0 && network[r + 1][c - 1] == '0') || // south (r + 1 < maxRow && c < maxCol && network[r + 1][c] == '0') || // southeast (r+1< maxRow && c+1< maxCol && network[r + 1][c + 1] == '0')); } //==determineRoute==================================================== // pre: network is a char array; maxRow, maxCol is the size of matrix; // r and c is the current location in array; found is true if // path found // post: set found to true is path is found //==================================================================== void determineRoute (char** network, int maxRow, int maxCol, int r, int c, bool& found) { // mark your current step... network[r][c] = '*'; // determine if you are at the exit if (r == maxRow - 1 && c == maxCol - 1 || found) { found = true; return; } int step = 0; // the next step to attempt // while there are possible forward steps, take them recursively while(stepPossible(network, maxRow, maxCol, r, c) && !found) { step = rand()%8; // step randomly assigned // Assume the following // 0 = East, 1 = Northeast, 2 = North, 3 = Northwest, 4 = West // 5 = Southwest, 6 = South, 7 = Southeast // // if the step is legal, then take it... recursively } // if exit from previous loop, must not be any possible steps if(!found) { cout << "backtrack " << r << ' ' << c << '\n'; // backtracking, do something here... } return; } //==printNetwork====================================================== // pre: network is char array of size (maxRow, maxCol) represents // computer network // post: displays the matrix in color if xterm is used //==================================================================== void printNetwork(char** network, int maxRow, int maxCol) { for(int i = 0; i < maxRow; i++) { for(int j = 0; j < maxCol; j++) { if(network[i][j] == '0') cout << GREEN << '0'; // I use '2' for backtracking... else if(network[i][j] == '2') cout << NORM << '2'; else cout << BLUE << '*'; cout << NORM << ' ' << flush; } cout << NORM << '\n'; } cout << NORM << '\n' << flush; }