Beginner Exercises

Pages: 1... 7891011... 16
No one's attempted the Dungeon Crawl challenge ?
This is my dungeon crawl code.

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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192

#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

#define FLOOR 0
#define HERO 1
#define MONSTER 2
#define GOLD 3
#define BOARD_H 12
#define BOARD_W 10

int getkey(void);
void print_board(int board[BOARD_H][BOARD_W],
                 int man[2], int gold[2],
                 int mon_one[2], int mon_two[2]);
char floor(int f);
void clear_screen(void);
char move_dir(int d);
void player_move(char input, int man[2], int play);
void monster_move(int move, int monster[2]);
int victory(int man[2], int gold[2], int mon_one[2],
                        int mon_two[2], int play);


int main() {
    srand(time(0));
    int x1 = rand() % 9, x2 = rand() % 9,
        x3 = rand() % 9, x4 = rand() % 9;
    int y1 = rand() % 3, y2 = rand() % 3,
        y3 = rand() % 3, y4 = rand() % 3;
    int mon_one_move, mon_two_move;
    char key;
    int play = 1;
    int man[2] = {y1, x1}, gold[2] = {9+y2, x2};
    int mon_one[2] = {3+y3, x3}, mon_two[2] = {6+y4, x4};
    int board [BOARD_H][BOARD_W];
    for (int r = 0; r < BOARD_H; r++) {
        for (int c = 0; c < BOARD_W; c++) {
            board[r][c] = 0;
        }
    }
    do {
        clear_screen();
        print_board(board, man, gold, mon_one, mon_two);
        key = getkey();
        player_move(key, man, play);

        if ((man[0] == gold[0]) && (man[1] == gold[1])) {
            play = 2;
        }
        if (((man[0] == mon_one[0]) && (man[1] == mon_one[1])) ||
            ((man[0] == mon_two[0]) && (man[1] == mon_two[1]))) {
            play = 0;
        }
        if (key == 'q')
            play = 3;

        monster_move(rand() % 5, mon_one);
        monster_move(rand() % 5, mon_two);
        clear_screen();
    } while (play == 1);
    switch (play) {
        case 0:
            cout << "You got eaten by a monster!" << endl;
            break;
        case 2:
            cout << "Yay you got the gold!" << endl;
            break;
        case 3:
            cout << "Thanks for playing, come again" << endl;
            break;
    }
    cin.get();
    return 0;
}

void print_board(int board[BOARD_H][BOARD_W],
                 int man[2], int gold[2],
                 int mon_one[2], int mon_two[2]) {
    for (int r = 0; r < BOARD_H; r++) {
        for (int c = 0; c < BOARD_W; c++) {
            if ((r == man[0]) && (c == man[1])) {
                cout << floor(HERO);
            } else if ((r == gold[0]) && (c == gold[1])) {
                cout << floor(GOLD);
            } else if ((r == mon_one[0]) && (c == mon_one[1])) {
                cout << floor(MONSTER);
            } else if ((r == mon_two[0]) && (c == mon_two[1])) {
                cout << floor(MONSTER);
            } else
            cout << floor(board[r][c]);
        }
        cout << endl;
    }
}

char floor(int f) {
    if (f == 0)
        return '.';
    if (f == 1)
        return 'H';
    if (f == 2)
        return 'M';
    if (f == 3)
        return 'G';
}

void clear_screen(void) {
    for (int i = 0; i < 25; i++) {
        cout << "\n";
    }
}

int getkey ( void ) {
    int ch;
    struct termios oldt, newt;
    tcgetattr ( STDIN_FILENO, &oldt );
    newt = oldt;
    newt.c_lflag &= ~( ICANON | ECHO );
    tcsetattr ( STDIN_FILENO, TCSANOW, &newt );
    ch = getchar();
    tcsetattr ( STDIN_FILENO, TCSANOW, &oldt );
    return ch;
}

char move_dir(int d) {
        switch (d) {
            case 0:
                return 'u';
            case 1:
                return 'l';
            case 2:
                return 'd';
            case 3:
                return 'r';
            case 4:
                return 'z';
        }
}

void player_move(char input, int man[2], int play) {
    if ((input == 'w') && (man[0] > 0))
        man[0]--;
    if ((input == 'a') && (man[1] > 0))
        man[1]--;
    if ((input == 's') && (man[0] < BOARD_H - 1))
        man[0]++;
    if ((input == 'd') && (man[1] < BOARD_W - 1))
        man[1]++;
}

void monster_move(int move, int monster[2]) {
    switch (move) {
        case 0:
            if (monster[0] > 0)
                monster[0]--;
            break;
        case 1:
            if (monster[1] > 0)
                monster[1]--;
            break;
        case 2:
            if (monster[0] < BOARD_H - 1)
                monster[0]++;
            break;
        case 3:
            if (monster[1] < BOARD_W - 1)
                monster[1]++;
            break;
        case 4:
            break;
    }
}

int victory(int man[2], int gold[2], int mon_one[2],
                        int mon_two[2], int play) {
    if ((man[0] == gold[0]) && (man[1] == gold[1])) {
        cout << "Yay you got the gold!" << endl;
        play = 2;
    }
    if (((man[0] == mon_one[0]) && (man[1] == mon_one[1])) ||
        ((man[0] == mon_two[0]) && (man[1] == mon_two[1]))) {
        cout << "You got eaten by a monster!" << endl;
        play = 0;
    }
}
I didn't read your code fully,
1
2
3
4
#define FLOOR 0
#define HERO 1
#define MONSTER 2
#define GOLD 3 

but these should be powers of two if you want to do with them what I think you do.
Are you implying that the floor can be the hero, a monster, and gold at the same time?
I think I'm thinking of something else, actually...

I didn't read your code fully,
1
2
3
4
#define FLOOR 0
#define HERO 1
#define MONSTER 2
#define GOLD 3  


but these should be powers of two if you want to do with them what I think you do.


I was just experimenting with constants. It works with these two functions, and prints the board, I'm sure there is a better way of doing this, but I'm still learning and don't know it yet.

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
void print_board(int board[BOARD_H][BOARD_W],
                 int man[2], int gold[2],
                 int mon_one[2], int mon_two[2]) {
    for (int r = 0; r < BOARD_H; r++) {
        for (int c = 0; c < BOARD_W; c++) {
            if ((r == man[0]) && (c == man[1])) {
                cout << floor(HERO);
            } else if ((r == gold[0]) && (c == gold[1])) {
                cout << floor(GOLD);
            } else if ((r == mon_one[0]) && (c == mon_one[1])) {
                cout << floor(MONSTER);
            } else if ((r == mon_two[0]) && (c == mon_two[1])) {
                cout << floor(MONSTER);
            } else
            cout << floor(board[r][c]);
        }
        cout << endl;
    }
}

char floor(int f) {
    if (f == 0)
        return '.';
    if (f == 1)
        return 'H';
    if (f == 2)
        return 'M';
    if (f == 3)
        return 'G';
}
Last edited on
Never mind. Ignore that post; I was thinking of something else entirely. Like helios said, the way I was thinking would allow you to do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#define FLOOR 0
#define HERO 2
#define MONSTER 4
#define GOLD 6

void what_is(int thing)
{
    if ((thing & FLOOR) == FLOOR)
        /* thing is a FLOOR */

    if ((thing & HERO) == HERO)
        /* thing is a HERO (and can be a FLOOR too) */

    if ((thing & MONSTER) == MONSTER)
        /* thing is a MONSTER (and could be a HERO and/or a FLOOR too) */

    if ((thing & GOLD) == GOLD)
        /* thing is a GOLD (and could be a MONSTER and/or HERO and/or FLOOR too) */
}


If you called what_is thus:
what_is(FLOOR | HERO | MONSTER | GOLD);

then what_is would decide that thing is a floor, hero, monster and gold at the same time.
6 is not a power of 2; unless you're saying that gold is a type of hero-monster thing. Also, no love for 1?
Ok, that should be 0, 1, 2, 4 then. I can't think of anyone to blame, so I'll have to blame those aliens running the hypercomputer we're a simulation on, or whatever it was...
I think I might be storing the positions of the hero and monsters differently than intended by the problem.


HINT: Don't let the player move off the gameboard! You program will crash if they move off the top or bottom of the board!
(the same holds true for enemies)


My game didn't crash if they went off the board.

I'm using a separate array to basically hold x and y coordinates:
1
2
int man[2] = {y1, x1}, gold[2] = {9+y2, x2};
int mon_one[2] = {3+y3, x3}, mon_two[2] = {6+y4, x4};


Then I compare those to the the matrix of the board.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for (int r = 0; r < BOARD_H; r++) {
    for (int c = 0; c < BOARD_W; c++) {
        if ((r == man[0]) && (c == man[1])) {
            cout << floor(HERO);
        } else if ((r == gold[0]) && (c == gold[1])) {
            cout << floor(GOLD);
        } else if ((r == mon_one[0]) && (c == mon_one[1])) {
            cout << floor(MONSTER);
        } else if ((r == mon_two[0]) && (c == mon_two[1])) {
            cout << floor(MONSTER);
        } else
        cout << floor(board[r][c]);
    }
    cout << endl;
}


Try using a class or struct instead

Try using a class or struct instead


Yeah those are things I don't really have down yet. I guess no better way to learn than to just do it though...

Are you saying use a struct/class for the board, monsrer/hero, or both?
Last edited on
At least GameObjects (monster and hero), possibly the board depending on where you want to go with it.
Could anyone maybe review my programs for these exercises?

http://www.cplusplus.com/forum/beginner/20952/

I didn't want to post them here as they would take up a lot of space.
I have not posted here in ages but here is my code for the Dungeon Crawler written in early 2009. Its a freaking mess full of horrid coding practices but it works... mostly (The monster refuse to kill you..)

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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include <cstdlib>
#include <iostream>

using namespace std;
char user = 'P'; // player
char trap = 'T'; // trap one
char trap2 = 'T'; // trap 2
char trap3 = 'T'; // trap 3
char gold = 'G';
char enemy = 'E';
char enemy2 = 'E';
// players starting location
int cordx = 1;
int cordy = 0;
//trap ones location
int trapx = 3;
int trapy = 6;
//trap twos location
int trap2x = 5;
int trap2y = 8;
//trap threes location
int trap3x = 7;
int trap3y = 6;
// gold
int goldx = 4;
int goldy = 7;
//enemy1
int enemy1x = 3;
int enemy1y = 8;
//enemy2
int enemy2x = 8;
int enemy2y = 7;

bool gamewin = false;
bool deathcheck = false;
// checks to see if player is dead (if players cord's are same as trap)

bool gameover() {

    if ((cordx == trapx && cordy == trapy) || (cordx == trap2x && cordy
            == trap2y) || (cordx == trap3x && cordy == trap3y)) {
        cout << "You hit a trap, game over!\n";
        deathcheck = true;
    }
    if (cordx == enemy1x && cordy == enemy1y) {
        cout << "You hit a enemy, game over!\n";
        deathcheck = true;
    }
    if (cordx == enemy2x && cordy == enemy2y) {
        cout << "You hit a enemy, game over!\n";
        deathcheck = true;
    }
}
// player win?
bool gameisover() {

    if (cordx == goldx && cordy == goldy) {
        cout << "You found the gold!\n";
        gamewin = true;
    }
}
//draws dungeon
int drawDungeon() {
    int dungeon[10][10];
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {

            if (gamewin == true) {
                if (i == goldx && j == goldy) {
                    cout << '!';
                } else {
                    cout << '.';
                }

            } else if (deathcheck == true) {
                if (i == cordx && j == cordy) {
                    cout << 'X';
                } else {
                    cout << '.';
                }
            } else { //else if deathceck is false
                if (i == cordx && j == cordy)
                    cout << user;
                else if (i == trapx && j == trapy) {
                    cout << trap;
                } else if (i == trap2x && j == trap2y) {
                    cout << trap2;
                } else if (i == trap3x && j == trap3y) {
                    cout << trap3;
                } else if (i == goldx && j == goldy) {
                    cout << gold;
                } else if (i == enemy1x && j == enemy1y) {
                    cout << enemy;
                } else if (i == enemy2x && j == enemy2y) {
                    cout << enemy2;
                } else {
                    cout << '.';
                }
            }
        }
        cout << endl;

    }
}

int moveRight() {
    cordy = cordy + 1;
    drawDungeon();

}

int moveLeft() {
    cordy = cordy - 1;
    drawDungeon();

}

int moveUp() {
    cordx = cordx - 1;
    drawDungeon();

}

int moveDown() {
    cordx = cordx + 1;
    drawDungeon();
}

char move() {
    char refresh;
    cin >> refresh;
    if (refresh == 'd') {
        moveRight();
    }

    else if (refresh == 'a') {
        moveLeft();
    } else if (refresh == 'w') {
        moveUp();
    } else if (refresh == 's') {
        moveDown();
    }
}

int enemymove1() {

    srand(time(NULL));
    int e1Move = rand() % 4;
    cout << e1Move;
    if (e1Move == 0) {
        cout << endl;
        enemy1y = enemy1y - 1;
        drawDungeon();
    } else if (e1Move == 1) {
        cout << endl;
        enemy1y = enemy1y + 1;
        drawDungeon();

    } else if (e1Move == 2) {
        cout << endl;
        enemy1x = enemy1x - 1;
        drawDungeon();

    } else if (e1Move == 3) {
        cout << endl;
        enemy1x = enemy1x + 1;
        drawDungeon();

    }

}
int enemymove2() {

    srand(time(NULL));
    int e2Move = rand() % 4;
    if (e2Move == 0) {
        cout << endl;
        enemy2y = enemy2y - 1;
        drawDungeon();
    } else if (e2Move == 1) {
        cout << endl;
        enemy2y = enemy2y + 1;
        drawDungeon();

    } else if (e2Move == 2) {
        cout << endl;
        enemy2x = enemy2x - 1;
        drawDungeon();

    } else if (e2Move == 3) {
        cout << endl;
        enemy2x = enemy2x + 1;
        drawDungeon();

    }
}

int onboardcheck() {
    // first to for player check
    if (cordx < 1) {
        cordx = 1;
    } else if (cordx > 8) {
        cordx = 8;
    } else if (cordy < 1) {
        cordy = 1;
    } else if (cordy > 8) {
        cordy = 8;
    }
    //enemy1 check
    if (enemy1x < 1) {
        enemy1x = 1;
    } else if (enemy1x > 8) {
        enemy1x = 8;
    } else if (enemy1y < 1) {
        enemy1x = 1;
    } else if (enemy1y > 8) {
        enemy1x = 8;
    } else if (enemy1x == enemy2x) {
        enemy1x = enemy1x;
    } else if (enemy1y == enemy2y) {
        enemy1y = enemy1y;
//enemy2 check
         if (enemy2x < 1) {
            enemy1x = 1;
        } else if (enemy2x > 8) {
            enemy1x = 8;
        } else if (enemy2y < 1) {
            enemy1x = 1;
        } else if (enemy2y > 8) {
            enemy1x = 8;
        }

        else if (enemy2x == enemy1x) {
            enemy2x = enemy2x;
        } else if (enemy2y == enemy1y) {
            enemy2y = enemy2y;
        }
    }
}
int main(int argc, char *argv[]) {
    drawDungeon();
    while (deathcheck == false && gamewin == false) {
        onboardcheck();
        move();
        enemymove1();
        ;
        enemymove2();
        cout << endl;
        gameisover();
        gameover();
        drawDungeon();
    }

    system("PAUSE");
    return EXIT_SUCCESS;
}
Due to a coding bug, the monster appears to be a vegetarian...
Yup, I noted that at the top. That code, btw, is absolutely horrid and was started at the end of 2008 when I did not even know how do use 40% of what is in there correctly.

Right now working on making a swap function that will switch around the values of a and b in
int swap(int a, intb) using pointers. Fun Fun.
Blitz Coder wrote:
Yup, I noted that at the top.

I know, I was joking :)

Right now working on making a swap function that will switch around the values of a and b in
1
2
3
4
5
6
void swap(void* a, void* b)
{
    char tmp = *(char*)b;
    *(char*)a = *(char*)b;
    *(char*)b = tmp;
}


Welcome back, by the way.
Thanks man but... you just did my HOMEWORK for me... bad bad.. I am going to ignore that little tidbit and struggle through it myself :) Thought is much appreciated though.
Oh, no. Not a swap function!
*Recoils in horror*
And an incorrect one, at that.

So what's the point of passing void * if you're just going to cast to char *? It's not like you're making it generic by passing also the size of the pointed type.
Pages: 1... 7891011... 16