From 1f0036528393478007640698858eb6079dc8d2fe Mon Sep 17 00:00:00 2001 From: RossTheRoss Date: Mon, 1 Apr 2019 11:06:07 -0500 Subject: Start HW --- ee1301/wk5/hw5_directory/maze.txt | 10 --- ee1301/wk5/hw5_directory/mazeRunner_v1.cpp | 31 +++++-- ee1301/wk5/hw5_directory/strap012_HW5A.cpp | 136 +++++++++++++++++++++++++++++ ee1301/wk5/hw5_directory/strap012_HW5B.cpp | 30 +++++++ 4 files changed, 191 insertions(+), 16 deletions(-) delete mode 100644 ee1301/wk5/hw5_directory/maze.txt create mode 100644 ee1301/wk5/hw5_directory/strap012_HW5A.cpp create mode 100644 ee1301/wk5/hw5_directory/strap012_HW5B.cpp (limited to 'ee1301/wk5') diff --git a/ee1301/wk5/hw5_directory/maze.txt b/ee1301/wk5/hw5_directory/maze.txt deleted file mode 100644 index cd95590..0000000 --- a/ee1301/wk5/hw5_directory/maze.txt +++ /dev/null @@ -1,10 +0,0 @@ -WWWW-O-WWW ----W------ ---WWWWWW-W ----------- -WWWWWWWWW- ----------- --WW-WW-WW- ----------- -WW-WW-WW-W -X--------- \ No newline at end of file diff --git a/ee1301/wk5/hw5_directory/mazeRunner_v1.cpp b/ee1301/wk5/hw5_directory/mazeRunner_v1.cpp index d915549..c761e76 100644 --- a/ee1301/wk5/hw5_directory/mazeRunner_v1.cpp +++ b/ee1301/wk5/hw5_directory/mazeRunner_v1.cpp @@ -3,6 +3,8 @@ #include #include using namespace std; +ifstream fin; +ofstream fout; const char BLANK = '-'; const char ROBOT = 'X'; @@ -18,7 +20,8 @@ const int lengthY = 10; // it "should" work with non-square, YMMV // V // +Y -void initBoard(char board[lengthX][lengthY], int &xPos, int &yPos); +void initBoardDefault(char board[lengthX][lengthY], int &xPos, int &yPos); +void initBoardCustom(char board[lengthX][lengthY], int &xPos, int &yPos); void clearScreen(); void showGrid(char board[lengthX][lengthY]); @@ -30,9 +33,13 @@ int main() srand(time(NULL)); char board[lengthX][lengthY] = {0}; int xPos = 0, yPos = 0; - - initBoard(board, xPos, yPos); - + fin.open("maze.txt"); //maze.txt is in the directory that it is run in + if (!fin.is_open()) //If a custom maze does not exist, generate one automatically + { + initBoardDefault(board, xPos, yPos); + } else { + initBoardCustom(board, xPos, yPos); + } while(hasChar(board, GOAL)) { char action; @@ -51,11 +58,11 @@ int main() return 0; } -void initBoard(char board[lengthX][lengthY], int &xPos, int &yPos) { +//The default (no custom file) maze is procedurally generated +void initBoardDefault(char board[lengthX][lengthY], int &xPos, int &yPos) { for(int curRow=0; curRow < lengthY; curRow++) { for(int curCol=0; curCol < lengthX; curCol++) { if ( (rand() % 5) + 1 == 4 || (rand() % 10) + 1 == 7) { - //Procedural generation is best for a game like this even if it makes the game impossible board[curCol][curRow] = WALL; } else { board[curCol][curRow] = BLANK; @@ -71,6 +78,18 @@ void initBoard(char board[lengthX][lengthY], int &xPos, int &yPos) { yPos = lengthY/2; } +void initBoardCustom(char board[lengthX][lengthY], int &xPos, int &yPos) +{ + for (int curRow = 0; curRow < lengthY; curRow++) + { + for (int curCol = 0; curCol < lengthX; curCol++) + { + fin >> board[curCol][curRow]; + } + } + +} + bool hasChar(char board[lengthX][lengthY], char findMe) { for(int curRow=0; curRow < lengthY; curRow++) { for(int curCol=0; curCol < lengthX; curCol++) { diff --git a/ee1301/wk5/hw5_directory/strap012_HW5A.cpp b/ee1301/wk5/hw5_directory/strap012_HW5A.cpp new file mode 100644 index 0000000..95a80ad --- /dev/null +++ b/ee1301/wk5/hw5_directory/strap012_HW5A.cpp @@ -0,0 +1,136 @@ +#include +#include +#include +using namespace std; +ifstream fin; +ofstream fout; + +const char BLANK = '-'; +const char ROBOT = 'X'; +const char GOAL = 'O'; +const char WALL = 'W'; +const int screen_num_lines = 25; +const int lengthX = 10; // for now we only support square boards +const int lengthY = 10; // it "should" work with non-square, YMMV +// Board is described by the following coordinate system: +// 0 --> +X +// | +// | +// V +// +Y + +void initBoardDefault(char board[lengthX][lengthY], int &xPos, int &yPos); +void initBoardCustom(char board[lengthX][lengthY], int &xPos, int &yPos); +void clearScreen(); +void showGrid(char board[lengthX][lengthY]); + +bool hasChar(char board[lengthX][lengthY], char findMe); +void updateGrid(char board[lengthX][lengthY], int &xPos, int &yPos, char action); + +int main() +{ + srand(time(NULL)); + char board[lengthX][lengthY] = {0}; + int xPos = 0, yPos = 0; + fin.open("maze.txt"); //maze.txt is in the directory that it is run in + if (!fin.is_open()) //If a custom maze does not exist, generate one automatically + { + initBoardDefault(board, xPos, yPos); + } else { + initBoardCustom(board, xPos, yPos); + } + while(hasChar(board, GOAL)) + { + char action; + clearScreen(); + showGrid(board); + cout << "Where would you like to go? (l, r, u, d) " << endl << "Wall Boolean " << hasChar(board, GOAL) << endl; + cin >> action; + + updateGrid(board, xPos, yPos, action); + } + + + clearScreen(); + showGrid(board); + cout << "You found the cookie! Congratulations!\n"; + + return 0; +} + +//The default (no custom file) maze is procedurally generated +void initBoardDefault(char board[lengthX][lengthY], int &xPos, int &yPos) { + for(int curRow=0; curRow < lengthY; curRow++) { + for(int curCol=0; curCol < lengthX; curCol++) { + if ( (rand() % 5) + 1 == 4 || (rand() % 10) + 1 == 7) { + board[curCol][curRow] = WALL; + } else { + board[curCol][curRow] = BLANK; + } + } + } + + + board[0][0] = GOAL; + board[lengthX/2][lengthY/2] = ROBOT; + + xPos = lengthX/2; + yPos = lengthY/2; +} + +void initBoardCustom(char board[lengthX][lengthY], int &xPos, int &yPos) +{ + for (int curRow = 0; curRow < lengthY; curRow++) + { + for (int curCol = 0; curCol < lengthX; curCol++) + { + fin >> board[curCol][curRow]; + if (board[curCol][curRow] == 'X') { + xPos = curCol; + yPos = curRow; + } + } + } + +} + +bool hasChar(char board[lengthX][lengthY], char findMe) { + for(int curRow=0; curRow < lengthY; curRow++) { + for(int curCol=0; curCol < lengthX; curCol++) { + if(board[curCol][curRow] == findMe) { + return true; + } + } //end curCol loop + } // end curRow loop + + return false; +} + +void clearScreen() { + for(int i = 0;i 0) { + xPos--; + } else if(action == 'r' && xPos < lengthX - 1) { + xPos++; + } else if(action == 'u' && yPos > 0) { + yPos--; + } else if(action == 'd' && yPos < lengthY - 1) { + yPos++; + } + board[xPos][yPos] = ROBOT; +} diff --git a/ee1301/wk5/hw5_directory/strap012_HW5B.cpp b/ee1301/wk5/hw5_directory/strap012_HW5B.cpp new file mode 100644 index 0000000..4576e59 --- /dev/null +++ b/ee1301/wk5/hw5_directory/strap012_HW5B.cpp @@ -0,0 +1,30 @@ +#include +#include +using namespace std; + +int main(int argc, char *argv[]) +{ + if (argc != 4) { + cout << "Invalid input!" << endl << " USAGE: ./rand-array m n max" << endl; + return 2; + } + srand(time(NULL)); + int m, n, max; + m = atoi(argv[1]); //number of rows + n = atoi(argv[2]); //number of columns + int randArray[m][n]; + max = atoi(argv[3]); //Maximum number + for (int curRow = 0; curRow < m; curRow++) + { + for (int curCol = 0; curCol < n; curCol++) + { + if (rand() % 2 == 1) { + randArray[curRow][curCol] = -1 * (rand() % max); + } else { + randArray[curRow][curCol] = (rand() % max); + } + } + } + + cout << endl; +} \ No newline at end of file -- cgit v1.2.3