Why doesn't my code work to move my character for dungeon claw?

1
2
3
4
5
6
7
8
9
10
11
12
13
cin>>answer;
if (answer == "w"){
    playery++;
}
else if (answer == "s"){
    playery--;
}
else if (answer == "a"){
    playerx--;
}
else if (answer == "d"){
    playerx++;
}


When i run my program with this code to move my character it doesn't change whatsoever. Why doesn't this work???
Heres the full 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
#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;


int main(){
    srand(time(NULL));
    
    char player   = '@';
    int playerx   = 1;
    int playery   = 1;
    char enemy1   = 'M';
    char enemy2   = 'M';
    int enemy1x   = rand()%10;
    int enemy1y   = rand()%7;
    int enemy2x   = rand()%10;
    int enemy2y   = rand()%7;
    char bomb1    = '|';
    int bomb1x    = 5;
    int bomb1y    = 4;
    char bomb2    = '|';
    int bomb2x    = 8;
    int bomb2y    = 3;
    char bomb3    = '|';
    int bomb3x    = 5;
    int bomb3y    = 2;
    char bomb4    = '|';
    int bomb4x    = 3;
    int bomb4y    = 5;
    char treasure = 'X';
    int treasure1x = 8;
    int treasure1y = 5;
    string answer;
    int con1;
    int con2;
    int con3;
    int con4;
    
    /* Control for intitial enemy coordinates */
    while(enemy1x == playerx && enemy1y ==playery){
        enemy1x = rand()%10;
        enemy1y = rand()%7;
    }
    while(enemy2x == playerx && enemy2y == playery){
        enemy2x = rand()%10;
        enemy2y = rand()%7;
    }
    while(enemy1x == bomb1x && enemy1y == bomb1y){
        enemy1x = rand()%10;
        enemy1y = rand()%7;
    }
    while(enemy2x == bomb1x && enemy2y == bomb1y){
        enemy2x = rand()%10;
        enemy2y = rand()%7;
    }
    while(enemy1x == bomb2x && enemy1y == bomb2y){
        enemy1x = rand()%10;
        enemy1y = rand()%7;
    }
    while(enemy2x == bomb2x && enemy2y == bomb2y){
        enemy2x = rand()%10;
        enemy2y = rand()%7;
    }
    while(enemy1x == bomb3x && enemy1y == bomb3y){
        enemy1x = rand()%10;
        enemy1y = rand()%7;
    }
    while(enemy2x == bomb3x && enemy2y == bomb3y){
        enemy2x = rand()%10;
        enemy2y = rand()%7;
    }
    while(enemy1x == bomb4x && enemy1y == bomb4y){
        enemy1x = rand()%10;
        enemy1y = rand()%7;
    }
    while(enemy2x == bomb4x && enemy2y == bomb4y){
        enemy2x = rand()%10;
        enemy2y = rand()%7;
    }
    while(enemy1x == treasure1x && enemy1y == treasure1y){
        enemy1x = rand()%10;
        enemy1y = rand()%7;
    }
    while(enemy2x == treasure1x && enemy2y == treasure1y){
        enemy2x = rand()%10;
        enemy2y = rand()%7;
    }



    char board[7][10] = {{'.','.','.','.','.','.','.','.','.','.'},
                        {'.','.','.','.','.','.','.','.','.','.'},
                        {'.','.','.','.','.','.','.','.','.','.'},
                        {'.','.','.','.','.','.','.','.','.','.'},   /* Multi-dimensional array */
                        {'.','.','.','.','.','.','.','.','.','.'},   /* form of the board       */
                        {'.','.','.','.','.','.','.','.','.','.'},
                        {'.','.','.','.','.','.','.','.','.','.'}};
    int control[] = {0,1};
    
    board[playery][playerx] = player;
    board[enemy1y][enemy1x] = enemy1;
    board[enemy2y][enemy2x] = enemy2;
    
    board[treasure1y][treasure1x] = treasure;
    board[bomb1y][bomb1x] = bomb1;
    board[bomb2y][bomb2x] = bomb2;
    board[bomb3y][bomb3x] = bomb3;
    board[bomb4y][bomb4x] = bomb4;

    
    cout<<"Welcome to Dungeon Claw!!!"<<endl<<endl;
    cout<<"Objective: "<<endl;
    cout<<"- Retrieve the treasure marked by the \"X\" by avoiding the"<<endl<<"  traps and \"M\"onsters."<<endl;
    cout<<"Enter \"Y\" when ready to start."<<endl;
    cin>>answer;
    do{
        if (answer == "Y"||answer == "y"){
            cout<<endl<<"BEGIN!!!"<<endl;
            do{
                con1 = rand()%10;
                con2 = rand()%10;
                con3 = rand()%10;
                con4 = rand()%10;

                do{
                    
                    for(int row = 0; row < 7; row++){
                        for(int col = 0; col < 10; col++){
                            cout<<board[row][col];
                        }
                        cout<<endl;
                    }
                    cin>>answer;
                    if (answer == "w"){
                        playery++;
                    }
                    else if (answer == "s"){
                        playery--;
                    }
                    else if (answer == "a"){
                        playerx--;
                    }
                    else if (answer == "d"){
                        playerx++;
                    }
                    
                    // Control for enemy coordinates
                    //enemy1x and enemy1y
                    do{
                        if(con1 % 2 == 0){
                            enemy1x++;
                        }
                        else{
                            enemy1x--;
                        }
                    }while(enemy1x == playerx && enemy1y == playery);
                    do{
                        if(con2 % 2 == 0){
                            enemy1y++;
                        }
                        else{
                            enemy1y--;
                        }
                    }while(enemy1x == playerx && enemy1y == playery);
    
                    //enemy2x and enemy2y
                    do{
                        if(con3 % 2 == 0){
                            enemy2x++;
                        }
                        else{
                            enemy2x--;
                        }
                    }while(enemy2x == playerx && enemy2y == playery);
                    do{
                        if(con4 % 2 == 0){
                            enemy2y++;
                        }
                        else{
                            enemy2y--;
                        }
                    }while(enemy2x == playerx && enemy2y == playery);

                }while(10>1);
            }while(1<10);
        }
        
        else{
            cout<<"Please enter \"Y\" to continue."<<endl;
            cin>>answer;
        }
    }while(10>1);
}


Its not complete. I'm just trying to get the movement section down first.
Last edited on
You change the values of playerx and playery based on the user input. But you never actually do anything to update the board based on those changed values. At line 102, you update the board to reflect the initial player position, but you never update it after that.
closed account (j3Rz8vqX)
Exactly what MikeyBoy said.

Normally a game proceeds in this fashion: (after default setup)

1)Listen for input from user
2)Process input (move, restrictions, etc)
3)Update screen (draw player and enemies)

Process 2, you would just erase the player and enemies from their previous spots and placed them in their new spots.

You might want to keep track of their current location and their new location.

Array[old_y][old_x] = blank

Array[new_y][new_x] = player

Same can be applied to your enemies.
It moves now but how would i erase the previous move.

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
                            if (answer == "w"){
                                playery = playery - 1;
                                board[playery][playerx] = player;
                                for(int row = 0; row<7; row++){
                                    for(int col = 0; col<10; col++){
                                        cout<<board[row][col];
                                    }
                                    cout<<endl;
                                }
                                break;
                            }
                            else if (answer == "s"){
                                playery = playery + 1;
                                board[playery][playerx] = player;
                                for(int row = 0; row<7; row++){
                                    for(int col = 0; col<10; col++){
                                        cout<<board[row][col];
                                    }
                                    cout<<endl;
                                }
                                break;
                                
                            }
                            else if (answer == "a"){
                                playerx = playerx - 1;
                                board[playery][playerx] = player;
                                for(int row = 0; row<7; row++){
                                    for(int col = 0; col<10; col++){
                                        cout<<board[row][col];
                                    }
                                    cout<<endl;
                                }
                                break;
                            }
                            else if (answer == "d"){
                                playerx = playerx + 1;
                                board[playery][playerx] = player;
                                for(int row = 0; row<7; row++){
                                    for(int col = 0; col<10; col++){
                                        cout<<board[row][col];
                                    }
                                    cout<<endl;
                                }
                                break;
                            }
                            else{
                                cout<<"Please input a valid move: "<<endl;
                                cin>>answer;



BEGIN!!!
..........
.@........
.....|....
.M......|.
.....|....
...|....X.
.......M..
s
..........
.@........
.@...|....
.M......|.
.....|....
...|....X.
.......M..
d
..........
.@........
.@@..|....
.M......|.
.....|....
...|....X.
.......M..
d
..........
.@........
.@@@.|....
.M......|.
.....|....
...|....X.
.......M..
Last edited on
It moves now but how would i erase the previous move.

My making the effort to read properly what people write when they answer your questions:

You might want to keep track of their current location and their new location.

Array[old_y][old_x] = blank

Array[new_y][new_x] = player
I dont understand how that will help delete the previous move that was executed from the board.
closed account (j3Rz8vqX)
1
2
3
4
5
6
7
8
9
10
11
12
                            if (answer == "w"){
                                board[playery][playerx] = '.';//Remove player at previous spot
                                playery = playery - 1;//Adjust Players location
                                board[playery][playerx] = player;//Add player at new spot
                                for(int row = 0; row<7; row++){
                                    for(int col = 0; col<10; col++){
                                        cout<<board[row][col];
                                    }
                                    cout<<endl;
                                }
                                break;
                            }
Last edited on
-__-... Thanks i feel so stupid that I couldn't figure that out. Now i know something new.
Topic archived. No new replies allowed.