Help trying to get Player to interact with Monster

Hey everyone, I've been trying to create my roguelike type of game for C++ using the ncurses library. After following many different tutorials, I have ended myself with a completion of the Player character, the map, the code to prevent the Player from going through walls and the random movements for the Monster character.

The next problem I'm encountering is implementing a boolean so whenever the Player character interacts with the Monster character, the game will quit (much like the roguelike games). However, I can't seem to get it to function the way I wanted. I think it has to do with the coordinates that I have set for the Player and the Monster. I'm open for any suggestions!

Here is the 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
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
#include <iostream>
#include <ncurses.h>

#define MAP_WIDTH 22
#define MAP_HEIGHT 15

#define TILE_FLOOR 0
#define TILE_WALL 1

int PlayerX, PlayerY;

void erase (int y, int x) {
    mvaddch(y, x, '.');
}

int nMapArray[MAP_HEIGHT][MAP_WIDTH] = {
    { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
    { 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
    { 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
    { 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
    { 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
    { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1 },
    { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
    { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
    { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
    { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
    { 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
    { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
    { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
    { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
    { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
};

bool IsPassable (int nMapX, int nMapY) {            //prevents from walking into walls
    
    if (nMapX < 0 || nMapX >= MAP_WIDTH || nMapY < 0 || nMapY >= MAP_HEIGHT)
        return false;
    
    int nTileValue = nMapArray[nMapY][nMapX];
    
    if( nTileValue == TILE_FLOOR) {
        return true;
    }
    
    return false;
}

class Monster {
public:
    void Appearance(char monster) {
        this->Monster = monster;
    }
    
    void SetPos(int x, int y) {
        this->PosX = x;
        this->PosY = y;
    }
   
    void Movement(int &MonsX, int &MonsY) {
        int x = (rand() % 3 - 1);
        int y = (rand() % 3 - 1);
        
        if (IsPassable(this->PosX+x, this->PosY+y)) {
            erase(PosY, PosX);
            MonsX = this->PosX += x;
            mvaddch(this->PosY, MonsX, this->Monster);
            refresh();
            
            erase(PosY, PosX);
            MonsY = this->PosY += y;
            mvaddch(MonsY, this->PosX, this->Monster);
            refresh();
        }
    }
    
    
protected:
    int PosX;
    int PosY;
    char Monster;
};

bool MonsterContact (int nMapY, int nMapX, int x, int y) {
    
    
    if (nMapArray[nMapY][nMapX] == nMapArray[y][x]) {
        return true;
    }
    
    return false;
}

void map() {
    for (int y = 0; y < MAP_HEIGHT; y++) {          //loops to print the map
        
        move(y,0);
        
        for (int x = 0; x < MAP_WIDTH; x++) {
            switch (nMapArray[y][x]) {
                case TILE_FLOOR:
                    printw(".");
                    break;
                    
                case TILE_WALL:
                    printw("#");
                    break;
            }
        }
    }
};

void init() {               //starts the ncurses screen.
    initscr();
    clear();
    noecho();
    raw();
    keypad(stdscr, TRUE);
    curs_set(0);
}

void game_loop (char Player, int row, int col, int ch) {
    
    Monster npc;
    npc.SetPos(7, 8);
    npc.Appearance('g');
    int MonsX,MonsY;
    
    mvaddch(row,col, Player);                   //player movement
    refresh();
    
    while(true) {
        
        npc.Movement(MonsX, MonsY);
        
        ch = getch();
        
        switch (ch) {
            
            case 'w':
                if (IsPassable(col, row-1)) {
                erase(row,col);
                row = row - 1;
                mvaddch(row, col, Player);
                    refresh();
                }
                
                if (MonsterContact(col, row, MonsX, MonsY)) {
                    return();
                }
                break;
                
            case 's':
                if (IsPassable(col, row+1)) {
                erase(row, col);
                row = row + 1;
                mvaddch(row, col, Player);
                    refresh();
                }
                
                if (MonsterContact(col, row, MonsX, MonsY)) {
                    return();
                }

                break;
                
            case 'a':
                if (IsPassable(col-1, row)) {
                erase(row,col);
                col = col - 1;
                mvaddch(row, col, Player);
                    refresh();
                }
                
                if (MonsterContact(col, row, MonsX, MonsY)) {
                    return();
                }

                break;
                
            case 'd':
                if (IsPassable(col+1, row)) {
                erase(row,col);
                col = col + 1;
                mvaddch(row,col, Player);
                    refresh();
                }
                
                if (MonsterContact(col, row, MonsX, MonsY)) {
                    return();
                }

                break;
                
            case 'q':
                return;
            
            default:
                break;
        }
        
    }
}

int main(int argc, const char * argv[]) {
    
    PlayerX = 2, PlayerY = 1;        //Player initial position.
    char Player = '@';
    
    init();                     //starts the ncurses screen.
    
    printw("Press any key to start the game");
    int ch = getch();
    clear();
    
    map();
    game_loop(Player, PlayerY, PlayerX, ch);
    
    endwin();
    
    return 0;
}

Last edited on
Topic archived. No new replies allowed.