aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ee1301/wk5/hw5_directory/mazeRunner_v1.cpp15
-rw-r--r--ee1301/wk5/hw5_directory/strap012_HW5A.cpp123
-rw-r--r--maze.txt10
3 files changed, 104 insertions, 44 deletions
diff --git a/ee1301/wk5/hw5_directory/mazeRunner_v1.cpp b/ee1301/wk5/hw5_directory/mazeRunner_v1.cpp
index c761e76..a938386 100644
--- a/ee1301/wk5/hw5_directory/mazeRunner_v1.cpp
+++ b/ee1301/wk5/hw5_directory/mazeRunner_v1.cpp
@@ -85,6 +85,11 @@ void initBoardCustom(char board[lengthX][lengthY], int &xPos, int &yPos)
for (int curCol = 0; curCol < lengthX; curCol++)
{
fin >> board[curCol][curRow];
+ if (board[curCol][curRow] == 'X') {
+ board[curCol][curRow] = ROBOT;
+ xPos = curCol;
+ yPos = curRow;
+ }
}
}
@@ -118,7 +123,7 @@ void showGrid(char board[lengthX][lengthY]) {
}
void updateGrid(char board[lengthX][lengthY],int & xPos, int & yPos,char action) {
- board[xPos][yPos] = BLANK;
+ int oldX = xPos; int oldY = yPos;
if (action == 'l' && xPos > 0) {
xPos--;
} else if(action == 'r' && xPos < lengthX - 1) {
@@ -128,5 +133,13 @@ void updateGrid(char board[lengthX][lengthY],int & xPos, int & yPos,char action)
} else if(action == 'd' && yPos < lengthY - 1) {
yPos++;
}
+ //Hit detection
+ if (board[xPos][yPos] == WALL) {
+ xPos = oldX;
+ yPos = oldY;
+ board[xPos][yPos] = WALL;
+ } else {
+ board[oldX][oldY] = BLANK;
+ }
board[xPos][yPos] = ROBOT;
}
diff --git a/ee1301/wk5/hw5_directory/strap012_HW5A.cpp b/ee1301/wk5/hw5_directory/strap012_HW5A.cpp
index 95a80ad..4332d2d 100644
--- a/ee1301/wk5/hw5_directory/strap012_HW5A.cpp
+++ b/ee1301/wk5/hw5_directory/strap012_HW5A.cpp
@@ -1,4 +1,5 @@
#include <iostream>
+#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
@@ -10,8 +11,8 @@ 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
+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
// |
@@ -33,23 +34,24 @@ int main()
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
+ if (!fin.is_open()) //If a custom maze does not exist, generate one automatically
{
initBoardDefault(board, xPos, yPos);
- } else {
+ }
+ else
+ {
initBoardCustom(board, xPos, yPos);
}
- while(hasChar(board, GOAL))
+ 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;
+ cout << "Where would you like to go? (l, r, u, d) " << endl;
cin >> action;
updateGrid(board, xPos, yPos, action);
- }
-
+ }
clearScreen();
showGrid(board);
@@ -59,23 +61,28 @@ int main()
}
//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;
- }
+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;
+ board[0][0] = GOAL;
+ board[lengthX / 2][lengthY / 2] = ROBOT;
- xPos = lengthX/2;
- yPos = lengthY/2;
+ xPos = lengthX / 2;
+ yPos = lengthY / 2;
}
void initBoardCustom(char board[lengthX][lengthY], int &xPos, int &yPos)
@@ -85,52 +92,82 @@ void initBoardCustom(char board[lengthX][lengthY], int &xPos, int &yPos)
for (int curCol = 0; curCol < lengthX; curCol++)
{
fin >> board[curCol][curRow];
- if (board[curCol][curRow] == 'X') {
+ if (board[curCol][curRow] == 'X')
+ {
+ board[curCol][curRow] = ROBOT;
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) {
+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
+ } // end curRow loop
return false;
}
-void clearScreen() {
- for(int i = 0;i<screen_num_lines;i++) {
+void clearScreen()
+{
+ for (int i = 0; i < screen_num_lines; i++)
+ {
cout << endl;
}
}
-void showGrid(char board[lengthX][lengthY]) {
- for(int curRow=0; curRow < lengthY; curRow++) {
- for(int curCol=0; curCol < lengthX; curCol++) {
+void showGrid(char board[lengthX][lengthY])
+{
+ for (int curRow = 0; curRow < lengthY; curRow++)
+ {
+ for (int curCol = 0; curCol < lengthX; curCol++)
+ {
cout << board[curCol][curRow];
} // end curCol loop
cout << endl;
} // end curRow loop
}
-void updateGrid(char board[lengthX][lengthY],int & xPos, int & yPos,char action) {
- board[xPos][yPos] = BLANK;
- if (action == 'l' && xPos > 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++;
+void updateGrid(char board[lengthX][lengthY], int &xPos, int &yPos, char action)
+{
+ int oldX = xPos;
+ int oldY = yPos;
+ if (action == 'l' && xPos > 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++;
+ }
+ //Hit detection
+ if (board[xPos][yPos] == WALL)
+ {
+ xPos = oldX;
+ yPos = oldY;
+ board[xPos][yPos] = WALL;
+ }
+ else
+ {
+ board[oldX][oldY] = BLANK;
}
board[xPos][yPos] = ROBOT;
}
diff --git a/maze.txt b/maze.txt
new file mode 100644
index 0000000..cd95590
--- /dev/null
+++ b/maze.txt
@@ -0,0 +1,10 @@
+WWWW-O-WWW
+---W------
+--WWWWWW-W
+----------
+WWWWWWWWW-
+----------
+-WW-WW-WW-
+----------
+WW-WW-WW-W
+X--------- \ No newline at end of file