aboutsummaryrefslogtreecommitdiffstats
path: root/ee1301/wk5/hw5_directory/mazeRunner_v1.cpp
blob: d915549cf9c1dcec56ecf40c6c91cfb9221650f6 (plain) (blame)
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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

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 initBoard(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;

    initBoard(board, xPos, yPos);

    while(hasChar(board, GOAL))
    {
        char action;
        clearScreen();
        showGrid(board);
        cout << "Where would you like to go? (l, r, u, d) " << endl;
        cin >> action;

        updateGrid(board, xPos, yPos, action);
    }

    clearScreen();
    showGrid(board);
    cout << "You found the cookie!  Congratulations!\n";

    return 0;
}

void initBoard(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;
                        }
            }
        }
        

        board[0][0] = GOAL;
        board[lengthX/2][lengthY/2] = ROBOT;

        xPos = lengthX/2;
        yPos = lengthY/2;
}

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<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++) {
            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++;
    }
    board[xPos][yPos] = ROBOT;
}