Trouble with pointers and references. Also looking for some general feedback :)

Hello everyone,

A while back, we had a course on C++ in my studies. While the course was complete crap, i kind of liked programming itself, and decided to learn it for real, so i started with the beginner exercises in the forums here. Now i'm pretty stuck with the dungeon crawl one, and would very hope for someone to help me out. Also, all feedback on whatever you can find that's wrong or could be better in my code is welcomed with open arms :)

The main problem is this: it won't compile due to the function ComputerMove (saying "declaration of 'anMonsterX' as array of reference. Expected ')' before ',' token."). Basically; i've got no clue why this is wrong or how i can get past this. Of course i could make a different X and Y coordinate for every single monster, but i'd expect arrays to work the best.

And below this are a couple more 'general c++ programming' questions for anyone in a helpful mood :)

Questions:
- in main: Is it possible to do all sorts of initialization in a function, without declaring them over here?
Stuff like: the 'game board', the players starting position, etc.. All i know is i can pass them back with a function, but that does mean
you need to declare them in your main as well. Looks ugly to me :/ Same goes for the traps and the monsters really
- in func UserMove / Progress: Why does the program not crash (like said in the exercise), but just sends me up/down a line when i get to the side of the board?
For me it only crashes when i'm like way WAY off the board (going up for ~20+ times after getting off the board).
- in func ComputerMove: What's the best way to return all the new monster positions, without calling the function for every single monster by itself?
- in func UserMove: If the input is wrong, i'm going back to the input part with a goto statement. I've never seen goto statements used a lot before tho; is this bad programming?


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
#include <iostream>
#include <ctime> // for time()
#include <cstdlib> // for rand() and srand()
using namespace std;

void Progress(int anField[][10], int nPlayerX, int nPlayerY, bool &bContinue);
void UserMove(int &nPlayerX, int &nPlayerY);

int main()
{

//Game initialization.
    cout << "Move to the treasure (9) to win. Watch out for traps (and hopefully monsters soon) :) " << endl;
    int anField[10][10] = {0};
    int nPlayerX = 3, nPlayerY = 4;
    int nTreasureX = 8, nTreasureY = 9;
    anField[nTreasureY][nTreasureX] = 9;
    bool bContinue = true;


//Implementation of traps.
    const int nAmountTraps = 3;
    int anTrapsX[nAmountTraps] = {4, 5, 6};
    int anTrapsY[nAmountTraps] = {6, 7, 8};
    for(int jjj=0;jjj<nAmountTraps;jjj++)
    {
    anField[anTrapsY[jjj]][anTrapsX[jjj]] = 1;
    }

//Implementation of monsters
    const int nAmountMonsters = 3;
    int anMonsterX[nAmountMonsters] = {6, 7, 8};
    int anMonsterY[nAmountMonsters] = {4, 5, 6};

//Looping an overview and the option for the user to move, until we hit a trap or the treasure
do
{
Progress(anField, nPlayerX, nPlayerY, bContinue);
UserMove(nPlayerX, nPlayerY);
}while(bContinue);


    return 0;
}


void Progress(int anField[][10], int nPlayerX, int nPlayerY, bool &bContinue)
{

//Detecting if the player moves to a treasure (win), trap(lose) or free spot (nothing happens, go again)
    if(anField[nPlayerY-1][nPlayerX-1] == 1)
    {
        cout << "Trap! " << endl;
        bContinue = false;
        exit(0);
    }
    else if(nPlayerX == 9 && nPlayerY == 10)
    {
        cout << "Got treasure bitch!" << endl;
        bContinue = false;
        exit(0);
    }

//Keeping track or player position. The cout is to check during development
anField[nPlayerY-1][nPlayerX-1] = 5;
//cout << "Player x,y: " << nPlayerX << ", " << nPlayerY << endl;

//Shows the map
    for(int iii = 0; iii<10;iii++)
    {
        for(int jjj=0;jjj<10;jjj++)
        {
            cout << anField[iii][jjj] << " ";
        }
        cout << endl;
    }

//Resetting the value of where the player was (else we'd leave a trace of 5's through the map where we were)
anField[nPlayerY-1][nPlayerX-1] = 0;
cout << endl;
}


// Changing users position according to input.
void UserMove(int &nPlayerX, int &nPlayerY)
{
cout << "Pick one: move up (u), down (d), left (l) or right (r). ";
char nUserMove;
TryAgain:
cin >> nUserMove;
    switch(nUserMove)
    {
        case 'u':
            nPlayerY -= 1;
            break;
        case 'd':
            nPlayerY += 1;
            break;
        case 'l':
            nPlayerX -= 1;
            break;
        case 'r':
            nPlayerX += 1;
            break;
        default:
            cout << "Only udlr" << endl;
            goto TryAgain;
    }
}


// Inserting monsters in the game. Function will make one random move (up down left or right) for every monster in the game.
// Al least, it should; haven't been able to test this yet.

int ComputerMove(int &anMonsterX[], int &anMonsterY[], int nAmountMonsters)
{
    int nComputerMove;
    srand(time(0));

    for(int iii = 0; iii<nAmountMonsters; iii++)
    {
        nComputerMove = rand() % 4 + 1;
        switch(nComputerMove)
        {
            case 1:
                anMonsterY[iii] -= 1;
            case 2:
                anMonsterY[iii] += 1;
            case 3:
                anMonsterX[iii] -= 1;
            case 4:
                anMonsterX[iii] += 1;
        }
    }

// Should return the new postions and stuff like that, but since i can't test it i haven't done that yet. (Maybe different approach is needed?)
return 0;
}


Thank you very much in advance,
Tijs
You don't use this variables in program.
1
2
int anMonsterX[nAmountMonsters] = {6, 7, 8};
int anMonsterY[nAmountMonsters] = {4, 5, 6};


and
int ComputerMove(int &anMonsterX[], int &anMonsterY[], int nAmountMonsters)
use
int ComputerMove(int anMonsterX[], int anMonsterY[], int nAmountMonsters)

use
int anField[10][10];

it is recomended to use return; not exit(0);
Last edited on
And your array anField have blank fields in it. So you cant use
cout << anField[iii][jjj] << " ";
you need to fill blank fields first.

your output (map)
1
2
3
4
5
6
7
8
9
10
-1 0 4195773 0 -1521937568 32565 -1518938104 32565 3054912 0 
0 0 -1518962048 32565 -1518936392 32565 -1521999872 32565 -1518932048 32565 
-1525885112 32565 -1516619336 32565 -1518958976 32565 -1518973712 32565 -1518936392 32565 
-1521262030 32565 5 0 2034041696 32767 -1518932048 32565 0 0 
6299672 0 0 0 2034042248 32767 2034042264 32767 0 0 
-1518794892 32565 1 32565 0 0 -1525885112 32565 -1521493434 32565 
-1518936192 32565 0 0 1 32767 6299120 0 1 0 
-1518767563 32565 4196400 0 6 1 6299752 0 4196400 0 
6300352 0 -1525674984 32565 0 0 1 32767 6299120 0 
4197956 0 1 32565 65535 1 2034041952 32767 9 0 
Last edited on
Questions:
- in main: Is it possible to do all sorts of initialization in a function, without declaring them over here?
Stuff like: the 'game board', the players starting position, etc.. All i know is i can pass them back with a function, but that does mean
you need to declare them in your main as well. Looks ugly to me :/ Same goes for the traps and the monsters really
- in func UserMove / Progress: Why does the program not crash (like said in the exercise), but just sends me up/down a line when i get to the side of the board?
For me it only crashes when i'm like way WAY off the board (going up for ~20+ times after getting off the board).
- in func ComputerMove: What's the best way to return all the new monster positions, without calling the function for every single monster by itself?
- in func UserMove: If the input is wrong, i'm going back to the input part with a goto statement. I've never seen goto statements used a lot before tho; is this bad programming?


-You need to declare variables in main or make them global to be able to initialize them in main function. Global variables are not recommended. Variable declaration means memory allocation. If you don't allocate memory you don't have where to save value of variable.

-I don't get this too :D

-I don't get what you want.

-I don't use switch to often, and goto I don't use at all.
I finished this game. But it is very different from your code :S
Thanks Shinigami!

I thought (or at least tried) using the anMonsterX[] and anMonsterY[], by sending those arrays to the ComputerMove function. This way you can easily call back where the monsters are right now if you want to change whatever (for example easy keeping track of them if they are different types of monsters). Isn't this possible this way?

Also; weird that you get such a messed up map! The line int anField[10][10] = {0}; in game initialization in the main function is supposed to set every value to 0 (and does so for me).

Topic archived. No new replies allowed.