can you help me with another very simple array problem, then thats it for my frazzled brain for todai

so i want to move you (the '^'dude) up in the board, i been trying to move yous position in brdray, now how do i get his position and in case 1 move one of his two index numbers up 1, done roughly what ive been tring with \\ but maybe ive done enough for today because i know im being silly trying different things.



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
#include <iostream>
#include <cmath>

using namespace std;




char you = '^';

char z = '*';
char x = ' ';
char brdray [8] [8]= { {z,z,z,z,z,z,z,z} ,{z,x,x,x,x,x,x,z} ,{z,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,z} ,{z,x,x,x,x,x,x,z} ,{z,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,z}, {z,z,z,z,z,z,z,z} };
void move();
void start();
void getboard();
int main ()
{
start();
getboard ();
move();
system ("cls");
getboard();

     return 0;
}

void getboard()
{
for (int a = 0;a<8;a++)
 {
     for (int b =0;b<8;b++)
        cout << brdray [a][b];
        cout<<endl;
 }

}

void start ()
{
brdray [3][3]= you;
}





void move ()
{
    int mv;
    cout << "1 is up, 2 is down, 3 is left and 4 is right"<< endl;
    cin >> mv;
    char w;
    char a,x,d,n;

    switch (mv)
    {
    case 1:
    \\ int pos;
     \\ pos[w][x]=brdray.find ('^');
      \\you=pos[w]+=[x];
\\something on thos grounds...i also getting you and plusing the empty indexes
\\im reviewing arrays know...then going to gym thenreading the answer\\
  

    break;
    }
}


fanks guys...the internet is a great too lfor teaching, just shame you dont get paid...though i bet knowledge is good for the economy and it all pays off in the long run...hrmmm
I would do something like this:

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
#include <iostream>
#include <cmath>

using namespace std;

char you = '^';
char z = '*';
char x = ' ';
char brdray [8][8]= { {z,z,z,z,z,z,z,z} ,{z,x,x,x,x,x,x,z} ,{z,x,x,x,x,x,x,z},
                        {z,x,x,x,x,x,x,z} ,{z,x,x,x,x,x,x,z} ,{z,x,x,x,x,x,x,z},
                        {z,x,x,x,x,x,x,z}, {z,z,z,z,z,z,z,z} };
unsigned int startX=3, startY=3; //here i declare two new variables that will represent the x and y position of the you variable in the brdray array; we can then increment and decrement them as we please 

void move();
void start();
void getboard();

int main ()
{
    while(1) { // i assume you want this to be an indefinite process?
        system ("cls"); //move this to the top so we clear the screen before we do any rendering of the board, otherwise you end up with a before and after effect which i assume you do not want?
        start();
        getboard ();
        move();
        getboard();
    }

    system("pause");
    return 0;
}

void getboard()
{
for (int a = 0;a<8;a++)
 {
     for (int b =0;b<8;b++)
        cout << brdray [a][b];
        cout<<endl;
 }

}

void start ()
{
    brdray [startX][startY]= you; //set the starting position of "you" to the integers we defined above in startX and startY
}

void move ()
{
    int mv;
    int pos;
    cout << "1 is up, 2 is down, 3 is left and 4 is right"<< endl;
    cin >> mv;
    char w;
    char a,x,d,n;

    switch (mv)
    {
    case 1:
        brdray[startX][startY] = x; //here we clear the current position of "you" before we adjust it's position
        startX=startX-1; //minus one which is basically saying brdray[startX-1][startY]
        brdray[startX][startY] = you; //set the new position of "you"
        break;
    case 2:
        brdray[startX][startY] = x;
        startX=startX+1;
        brdray[startX][startY] = you;
        break;
    case 3:
        brdray[startX][startY] = x;
        startY=startY-1;
        brdray[startX][startY] = you;
        break;
    case 4:
        brdray[startX][startY] = x;
        startY=startY+1;
        brdray[startX][startY] = you;
        break;
    }
}


One thing you may want to note is that there is no bounds checking. This means you can increment and decrement over the array bounds. You will have to do some logic checking for that though.
Last edited on
wow thank you for doing so much mr error, i was going to stop for the day but you got me back into it...i was running out of brainpower too, i find the trick is to start early
whts so strange is that our codes are identical yet the outcome is different...you get funny point signs and i get extra ^' guys when i go left and right


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
#include <iostream>
#include <cmath>

using namespace std;




char you = '^';

char z = '*';
char x = ' ';
char brdray [8] [8]= { {z,z,z,z,z,z,z,z} ,{z,x,x,x,x,x,x,z} ,{z,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,z} ,{z,x,x,x,x,x,x,z} ,{z,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,z}, {z,z,z,z,z,z,z,z} };
void move();
void start();
void getboard();
int posx=4;
int posy=4;
int main ()
{
while (1)
{

system ("cls");
start();
getboard();
move();

}
     return 0;
}

void getboard()
{
for (int a = 0;a<8;a++)
 {
     for (int b =0;b<8;b++)
        cout << brdray [a][b];
        cout<<endl;
 }

}

void start ()
{
brdray [posx] [posy]=you;

}



void move ()
{
    int mv;
    cout << "1 is up, 2 is down, 3 is left and 4 is right"<< endl;
    cin >> mv;
    char w;
    char a,x,d,n;

    switch (mv)
    {
    case 1:
    brdray [posx] [posy] = x;
    posx=posx-1;
    brdray [posx] [posy] = you;
    break;
    case 2:
    brdray [posx] [posy] = x;
    posx=posx+1;
    brdray [posx] [posy] = you;
    break;
    case 3:
    brdray [posx] [posy] = x;
    posy=posy-1;
    brdray [posy] [posy] = you;
    break;
    case 4:
    brdray [posx] [posy] = x;
    posy=posy+1;
    brdray [posy] [posy] = you;
    break;


    }


}
Check lines 77 and 82. You have done:
brdray [posy] [posy] = you;

Think about what you are doing here compared to what you have done on lines 67 and 72:
brdray [posx] [posy] = you;


Also in retrospect you dont need to clear the prior position of the ^ and set the current you position in each case statement. Just do:

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
void move ()
{
    int mv;
    cout << "1 is up, 2 is down, 3 is left and 4 is right"<< endl;
    cin >> mv;
    char w;
    char a,x,d,n;

    brdray [posx] [posy] = x;
    switch (mv)
    {
    case 1:
        posx=posx-1;
        break;
    case 2:
        posx=posx+1;
        break;
    case 3:
        posy=posy-1;
        break;
    case 4:
        posy=posy+1;
        break;
    }
    brdray [posx] [posy] = you;
}
Last edited on
oh yes very good
can someone tell me why i am FORBIDDEN to initialize member variables in this constructor...i thought i had this down

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
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>


using namespace std;

class player
{
private:

 int health =100;
 int name;
 int attackpower = 10;

int hit ()
{
    health=health-10;
    if (health>10)
    {
        cout << "your  dead innit"<<endl;
        return 1;
    }
}

};











char z = '*';
char x = ' ';
char brdray [10] [10] = {  {z,z,z,z,z,z,z,z,z,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z}, {z,z,z,z,z,z,z,z,z,z} };
int posx=5;
int posy=5;

char you = 'O';
void start ();
void getbrd ();
void move ();

int main ()
{
start();

while (1)
{



getbrd();
move();
system ("CLS");
}
return 0;
}

void getbrd ()
{
for (int a = 0;a<10;a++)
 {
     for (int b =0;b<10;b++)
        cout << brdray [a][b];
        cout<<endl;
 }

}

void move ()
{
    int mv;
    cout << "1 is up, 2 is down, 3 is left and 4 is right"<< endl;
    cin >> mv;
    char w;
    char a,x,d,n;

    brdray [posx] [posy] = x;
    switch (mv)
    {
    case 1:
        posx=posx-1;
        break;
    case 2:
        posx=posx+1;
        break;
    case 3:
        posy=posy-1;
        break;
    case 4:
        posy=posy+1;
        break;
    }
    brdray [posx] [posy] = you;
}
void start()
{
brdray [posx] [posy]=you;
}
Ok so now your getting into classes. The error that you are receiving is because in your player class you are trying to set members (i.e health, name, attackpower) to a value.

You may wish to review how classes work:
http://www.cplusplus.com/doc/tutorial/classes/
http://www.cplusplus.com/doc/tutorial/classes2/

In short you define the class and its member types and then create an "instance" of that class which you manipulate.

It's kind of like telling a painter what kind of paint they should use on a particular surface but not specifically the colors.
Last edited on
i sussed out i can initialize them between the brackets with a :
i hope im not supposed to understand why and can jus accept it, now i have to use the <- thing!

Hey why on earth does this happen, the pointer doesnt move despite the fact that posx or y are not equal to z!!!!!!!
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
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>


using namespace std;

class player
{
public:



int hit ()
{
    health=health-10;
    if (health>10)
    {
        cout << "your  dead innit"<<endl;
        return 1;
    }
}
 int health;
 string name;
 int attackpower;

 char you;
};

class baddy
{
public:



int takehit ()
{
    bealth=bealth-10;
    if (bealth>10)
    {
        cout << "you got the wanker"<<endl;
        return 2;
    }
}
 int bealth;
 string bame;
 int battackpower;
 int bosx;
 int bosy;
 char bad;
};



char z = '*';
char x = ' ';
char brdray [10] [10] = {  {z,z,z,z,z,z,z,z,z,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z}, {z,z,z,z,z,z,z,z,z,z} };
int bosx=2;
int bosy=2;
char them = '!';
char you = 'O';
void start ();
void getbrd ();
void bdymove ();
void move ();
int posx = 5;
int posy = 5;










int main ()
{
start();
while (1)
{
getbrd();
move();
bdymove();
system ("CLS");
}
return 0;
}












void getbrd ()
{
for (int a = 0;a<10;a++)
 {
     for (int b =0;b<10;b++)
        cout << brdray [a][b];
        cout<<endl;
 }

}

void move ()
{
    int mv;
    cout << "1 is up, 2 is down, 3 is left and 4 is right"<< endl;
    cin >> mv;
    char w;
    char a,x,d,n;

    brdray [posx] [posy] = x;
    switch (mv)
    {
    case 1:
    posx=posx-1;
    break;
    case 2:
    posx=posx+1;
    break;
    case 3:
    posy=posy-1;
    break;
    case 4:
    posy=posy+1;
    break;
    }
    if (posx||posy=z)  //remove this line to.....
    {
        posx=5;
        posy=5;
    }                  //...this line if you want to see what the program does.
    brdray [posx] [posy] =you;
}


void start()
{
brdray [posx] [posy]=you;
brdray [bosx] [bosy]=them;
}

void bdymove ()
{
brdray [bosx] [bosy] = x;

if (posx>bosx)
{
    bosx++;
}
if (posx<bosx)
{
    bosx--;
}
if (posy>bosy)
{
    bosy++;
}
if (posy<bosy)
{
    bosy--;
}


brdray [bosx] [bosy]=them;
}


if you help me and you live in london i will by you icecream
Last edited on
1
2
3
4
5
    if (posx||posy=z)  //remove this line to.....
    {
        posx=5;
        posy=5;
    }                  //...this line if you want to see what the program does. 


is equivalent to;

1
2
    if ( posx != 0  ||  (posy=z) != 0 )
        posx = posy = 5 ;


Note that posy=z is assignment and not comparison.
thas still no workee workee five dollar


ahh i forgot an = what i meant was
1
2
3
4
5
if (posx||posy==z)  
    {
        posx=5;
        posy=5;
    }           
Topic archived. No new replies allowed.