Program not deleting something properly

Hello, I'm making a basic RPG, and at the moment, Im trying to make it so when I kill a goblin, its G on the char array is deleted!

Here are the parts of the code that matter:

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
int main()
{

    while(game_running == true){
        //SCREEN CLEARING
        system("cls");
        //GOBLIN DELETION
        if(battleHappened == 1){

            if(lastKeyPressed == 1){
                map[y + 1][x] = ' ';
            }
            else if(lastKeyPressed == 2){
                map[y - 1][x] = ' ';
            }
            else if(lastKeyPressed == 3){
                map[y][x + 1] = ' ';
                }
            else if(lastKeyPressed == 4){
                map[y][x - 1] = ' ';
            }

            battleHappened = 0;
        }

        for(int display = 0; display < 22; display++){
            cout << map[display] <<  endl;
        }

        //MAIN DISPLAYED INFORMATION
        cout << "Money:$" << points << endl;
        cout << "F1:Inventory" << endl;
        cout << "Health:" << health << endl;
        //PAUSE BEFORE ACTION
        system("pause>nul");

        //ARROW KEY ACTIONS
        if(GetAsyncKeyState(VK_DOWN)){
            if(map[y + 1][x] != '#' && map[y + 1][x] != 'D' && map[y + 1][x] != 'G'){
                map[y][x] = ' ';
                y++;
                map[y][x] = '@';
            }
            else if(map[y + 1][x] == 'G'){
                goblinBattle();
            lastKeyPressed = 1;
            }
            else if(map[y + 1][x] == '$'){}
        }
        if(GetAsyncKeyState(VK_UP)){
            if(map[y - 1][x] != '#' && map[y - 1][x] != 'D' && map[y -1][x] != 'G'){
                map[y][x] = ' ';
                y--;
                map[y][x] = '@';
            }
            else if(map[y - 1][x] == 'G'){
                goblinBattle();
            }
            lastKeyPressed = 2;
        }
        if(GetAsyncKeyState(VK_RIGHT)){
            if(map[y][x + 1] != '#' && map[y][x + 1] != 'D' && map[y][x + 1] != 'G'){
                map[y][x] = ' ';
                x++;
                map[y][x] = '@';
            }
            else if(map[y][x + 1] == 'G'){
                goblinBattle();
            }
            lastKeyPressed = 3;
        }
        if(GetAsyncKeyState(VK_LEFT)){
            if(map[y][x - 1] != '#' && map[y][x - 1] != 'D' && map[y][x - 1] != 'G'){
                map[y][x] = ' ';
                x--;
                map[y][x] = '@';
            }
            else if(map[y][x - 1] == 'G'){
                goblinBattle();
            }
            lastKeyPressed = 4;
        }

^the code in the main that matters.

Heres the goblin battle function
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
void goblinBattle(){
    srand(time(0));
    battle = true;
    system("cls");
    while(battle == true){
        if(goblinHealth > 0){
            cout << "Health:" << health << endl;
            cout << "Goblin's Health:" << goblinHealth << endl;
            cout << "1:Attack\n";
            cout << "2:Inventory\n";
            cout << "3:Run!\n";
            cin >> userInput;
            system("cls");
            if(userInput == 1){
                currenta = rand() % attackRating;
                currentb = rand() % 3;
                goblinHealth = goblinHealth - currenta;
                health = health - currentb;
                cout << "You hit for " << currenta << " damage." << endl;
                cout << "The goblin hit for " << currentb << " damage." << endl;
            }
            else if(userInput == 2){
                inventory();
            }
            else if(userInput == 3){
                chance[8] = rand() % 100;
                if(chance[8] < 50){
                battle = false;
                }
                else{
                cout << "You failed to run away!" << endl;
                currentb = rand() % 3;
                health = health - currentb;
                cout << "The goblin hit for " << currentb << " damage." << endl;
                }
            }
        }
        else{
            chance[0] = rand() % 100;
            chance[1] = rand() % 100;
            chance[2] = rand() % 100;
            chance[3] = rand() % 100;
            if(chance[0] < 30){
                inv[currentInv + 1] = itemNames[1];
                cout << "You got an " << itemNames[1] << endl;
                chance[0] = 1000;
            }
            if(chance[1] < 30){
                inv[currentInv + 1] = itemNames[3];
                cout << "You got an " << itemNames[3] << endl;
                chance[1] = 1000;
            }
            if(chance[2] < 10){
                inv[currentInv + 1] = itemNames[2];
                cout << "You got a " << itemNames[2] << endl;
                chance[2] = 1000;
                }
            if(chance[3] < 10){
                inv[currentInv + 1] = itemNames[4];
                cout << "You got a " << itemNames[4] << endl;
                chance[3] = 1000;
            }
            system("pause");
            battle = false;

            }
        }
    goblinHealth = 10;
    battleHappened = 1;
    main();
}


At the moment, when i move into a goblin from any direction EXCEPT up (so pressing down) it successfully deletes the goblin, but for some reason when i press down into a goblin it does not work.

Does anyone have any ideas? Thanks for the help.
Last edited on
I would seriously consider using classes to create this rpg instead of this method you're using now. It would eliminate problems like this happening in future, make the code shorter, faster to write, more manageable and easily expandable.

You also really need to get rid of that call to main(); at the end of void goblinbattle();. It will call up another copy of main. It's not good to do at all. =/ You also don't seem to have any bounds checks on your arrays (you still check y - 1 even if y = 0). Also not good.

EXCEPT up (so pressing down)
What? So confusing?

Onto your code:

I see a problem here (in all 4 cases):
1
2
3
4
5
if(map[y + 1][x] != '#' && map[y + 1][x] != 'D' && map[y + 1][x] != 'G'){
                map[y][x] = ' '; //Presumably this is the square your character just left? 
                y++;
                map[y][x] = '@'; //So this should update them to a new position? But it isn't...
            }


Could you be a little more specific as to what happens when you delete a goblin? I can't see anywhere in goblinbattle() that deletes the goblin?

Edit: Typo
Last edited on
Well in the bit of code there's the y++, which does make you move to the next y+,x+,x-, or y-, depending on which key you press.

Im going to start making classes, I'm starting to hate my code as well.

Also I've deleted that extra main and now everything seems to be working, thanks for the help :D
Well in the bit of code there's the y++, which does make you move to the next y+,x+,x-, or y-, depending on which key you press.


Oh I see now... I find that very confusingly placed. lol

Im going to start making classes.


Very good decision. : )
Last edited on
Ive made the classes, the program will still compile but now It wont delete the Goblins at all.
I have 4 classes; Inventory, Goblin, Maps, and User.

Here is the Goblin class and Main since those 2 are the most important at the moment.

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
#include <iostream>
#include "windows.h"
#include "math.h"
#include "time.h"
#include <string>
#include "Goblin.h"
#include "User.h"
#include "Inventory.h"
#include "Maps.h"

using namespace std;

Goblin::Goblin()
{
    //ctor
}

Goblin go;
User uo;
Inventory io;
Maps mo;

//GOBLIN BATTLE FUNCTION
void Goblin::goblinBattle(){
    srand(time(0));
    go.battle = true;
    system("cls");
    while(go.battle == true){
        if(go.goblinHealth > 0){
            cout << "Health:" << uo.health << endl;
            cout << "Goblin's Health:" << go.goblinHealth << endl;
            cout << "1:Attack\n";
            cout << "2:Inventory\n";
            cout << "3:Run!\n";
            cin >> uo.userInput;
            system("cls");
            if(uo.userInput == 1){
                go.currenta = rand() % uo.attackRating;
                go.currentb = rand() % 3;
                go.goblinHealth = go.goblinHealth - go.currenta;
                uo.health = uo.health - go.currentb;
                cout << "You hit for " << go.currenta << " damage." << endl;
                cout << "The goblin hit for " << go.currentb << " damage." << endl;
            }
            else if(uo.userInput == 2){
                io.inventoryOpen();
            }
            else if(uo.userInput == 3){
                io.chance[8] = rand() % 100;
                if(io.chance[8] < 50){
                go.battle = false;
                }
                else{
                cout << "You failed to run away!" << endl;
                go.currentb = rand() % 3;
                uo.health = uo.health - go.currentb;
                cout << "The goblin hit for " << go.currentb << " damage." << endl;
                }
            }
        }
        else{
            io.chance[0] = rand() % 100;
            io.chance[1] = rand() % 100;
            io.chance[2] = rand() % 100;
            io.chance[3] = rand() % 100;
            if(io.chance[0] < 30){
                io.inv[io.currentInv + 1] = io.itemNames[1];
                cout << "You got an " << io.itemNames[1] << endl;
                io.chance[0] = 1000;
            }
            if(io.chance[1] < 30){
                io.inv[io.currentInv + 1] = io.itemNames[3];
                cout << "You got an " << io.itemNames[3] << endl;
                io.chance[1] = 1000;
            }
            if(io.chance[2] < 10){
                io.inv[io.currentInv + 1] = io.itemNames[2];
                cout << "You got a " << io.itemNames[2] << endl;
                io.chance[2] = 1000;
                }
            if(io.chance[3] < 10){
                io.inv[io.currentInv + 1] = io.itemNames[4];
                cout << "You got a " << io.itemNames[4] << endl;
                io.chance[3] = 1000;
            }
            system("pause");
            go.battle = false;

            }
        }
    go.goblinHealth = 10;
//THIS IS THE DELETION CODE. It is now here instead of the Beginning of main.
            if(uo.lastKeyPressed == 1){
                mo.map[mo.currentMap][mo.y + 1][mo.x] = ' ';
            }
            else if(uo.lastKeyPressed == 2){
                mo.map[mo.currentMap][mo.y - 1][mo.x] = ' ';
            }
            else if(uo.lastKeyPressed == 3){
                mo.map[mo.currentMap][mo.y][mo.x + 1] = ' ';
                }
            else if(uo.lastKeyPressed == 4){
                mo.map[mo.currentMap][mo.y][mo.x - 1] = ' ';
            }
}

Main
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
#include <iostream>
#include "windows.h"
#include "math.h"
#include "time.h"
#include <string>
#include "Goblin.h"
#include "User.h"
#include "Inventory.h"
#include "Maps.h"

using namespace std;

void treasureGet();

Goblin goblinObject;
User userObject;
Inventory Io;
Maps MO;

int counter = 0;

bool gotten[20]{false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false};
int gottenCounter = 0;

bool game_running = true;

int main()
{

    while(game_running == true){
        //SCREEN CLEARING
        system("cls");


        for(int display = 0; display < 22; display++){
            cout << MO.map[MO.currentMap][display] <<  endl;
        }

        //MAIN DISPLAYED INFORMATION
        cout << "Money:$" << userObject.points << endl;
        cout << "F1:Inventory" << endl;
        cout << "Health:" << userObject.health << endl;
        //PAUSE BEFORE ACTION
        system("pause>nul");

        //ARROW KEY ACTIONS
        if(GetAsyncKeyState(VK_DOWN)){
            if(MO.map[MO.currentMap][MO.y + 1][MO.x] != '#' && MO.map[MO.currentMap][MO.y + 1][MO.x] != 'D' && MO.map[MO.currentMap][MO.y + 1][MO.x] != 'G'){
                MO.map[MO.currentMap][MO.y][MO.x] = ' ';
                MO.y++;
                MO.map[MO.currentMap][MO.y][MO.x] = '@';
            }
            else if(MO.map[MO.currentMap][MO.y + 1][MO.x] == 'G'){
                goblinObject.goblinBattle();
            userObject.lastKeyPressed = 1;
            }
        }
        if(GetAsyncKeyState(VK_UP)){
            if(MO.map[MO.currentMap][MO.y - 1][MO.x] != '#' && MO.map[MO.currentMap][MO.y - 1][MO.x] != 'D' && MO.map[MO.currentMap][MO.y - 1][MO.x] != 'G'){
                MO.map[MO.currentMap][MO.y][MO.x] = ' ';
                MO.y--;
                MO.map[MO.currentMap][MO.y][MO.x] = '@';
            }
            else if(MO.map[MO.currentMap][MO.y - 1][MO.x] == 'G'){
                goblinObject.goblinBattle();
            userObject.lastKeyPressed = 1;
            }
        }
        if(GetAsyncKeyState(VK_RIGHT)){
            if(MO.map[MO.currentMap][MO.y][MO.x + 1] != '#' && MO.map[MO.currentMap][MO.y][MO.x + 1] != 'D' && MO.map[MO.currentMap][MO.y][MO.x + 1] != 'G'){
                MO.map[MO.currentMap][MO.y][MO.x] = ' ';
                MO.x++;
                MO.map[MO.currentMap][MO.y][MO.x] = '@';
            }
            else if(MO.map[MO.currentMap][MO.y][MO.x + 1] == 'G'){
                goblinObject.goblinBattle();
            userObject.lastKeyPressed = 3;
            }
        }
        if(GetAsyncKeyState(VK_LEFT)){
            if(MO.map[MO.currentMap][MO.y][MO.x - 1] != '#' && MO.map[MO.currentMap][MO.y][MO.x - 1] != 'D' && MO.map[MO.currentMap][MO.y][MO.x - 1] != 'G'){
                MO.map[MO.currentMap][MO.y][MO.x] = ' ';
                MO.x--;
                MO.map[MO.currentMap][MO.y][MO.x] = '@';
            }
            else if(MO.map[MO.currentMap][MO.y][MO.x - 1] == 'G'){
                goblinObject.goblinBattle();
            userObject.lastKeyPressed = 4;
            }
        }
        if(GetAsyncKeyState(VK_F1)){
            Io.inventoryOpen();
        }

        if(counter == 9 && userObject.health < 20){
            counter = 0;
            userObject.health++;
        }
        else if(counter < 9){
            counter++;
        }

    }
    return 0;
}

Hope anyone can help :P
Last edited on
You could have saved a lot of typing with.
 
bool gotten[20] = {0}; 


Which would set all elements to zero (which is false).

I was envisioning you using the class to keep track of the goblin's co-ordinates.
1
2
3
4
5
6
7
 
class goblin
{
    int x; //Keep track of the goblin's x position on the map
    int y; //and y position on the map. 
    //other stuff like health etc... 
}; 


This way, all you need to do when a goblin dies is reset the map square on which the goblin is currently on.
1
2
3
char map[20][20]; 
//Somewhere here the goblin dies... 
map[mygoblin.y][mygoblin.x] = ' '; 
Last edited on
Topic archived. No new replies allowed.