### 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.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970`` ``````#include #include 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<> 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:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980`` ``````#include #include 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<> 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

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889`` ``````#include #include 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<> 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:

 ``1234567891011121314151617181920212223242526`` ``````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

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114`` ``````#include #include #include #include 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"<> 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!!!!!!!
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184`` ``````#include #include #include #include using namespace std; class player { public: int hit () { health=health-10; if (health>10) { cout << "your dead innit"<10) { cout << "you got the wanker"<> 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 (posxbosy) { bosy++; } if (posy

if you help me and you live in london i will by you icecream
Last edited on
 ``12345`` `````` 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;

 ``12`` `````` 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
 ``12345`` ``````if (posx||posy==z) { posx=5; posy=5; } ``````
Topic archived. No new replies allowed.