# Thread: Moving through matrices?

1. ## Moving through matrices?

I'm trying to create a simple find-the-item game using a matrix as the area. You start out at a fixed point and have to find an item at a random point. The problem is, I can't get the player's character to move.I'm using a switch statement with the following code.

char direction;
switch (direction){
case('N'||'n'):
Guy[Row][Col]=Gamearea[Row+1][Col];
case('S'||'s'):
Guy[Row][Col]=Guy[Row-1][Col];
case('W||'w):
Guy[Row][Col]=Guy[Row][Col-1];
case('E'||'e'):
Guy[Row][Col]=Guy[Row][Col+1];
default:
cout<< "Enter a random move, please";
}
Can anyone help here? I'm also trying to create something that moves randomly through the matrix, one space at a time.

2. I think you need to put some breaks in your code. Also check if you're not running out of your array bounds...

Code:
```char direction;
switch (direction)
{
case('N'||'n'):
if(Row < MaxRow)
Guy[Row][Col]=Guy[Row+1][Col];
break;
case('S'||'s'):
if(Row > 0)
Guy[Row][Col]=Guy[Row-1][Col];
break;
case('W||'w):
if(Col > 0)
Guy[Row][Col]=Guy[Row][Col-1];
break;
case('E'||'e'):
if(Col < MaxCol)
Guy[Row][Col]=Guy[Row][Col+1];
break;
default:
cout<< "Enter a random move, please";
break;
}```

3. ## Re: Moving through matrices?

Originally posted by Unregistered
I'm also trying to create something that moves randomly through the matrix, one space at a time.
Try searching the board. Search for rand or srand.

4. Still no good. That section of the code is still generating 24 errors. 16 of those are "subscript requires array or pointer type", with four generated from the same line.The rest are '=' left operand must be l-values and case value 1 already useds.

5. Sorry about that, you can't place the || (OR) in a case statement. And you forgot the closing ' when checking for the letter w (and W).

Code:
```char direction;
switch (direction)
{
case 'N':
case 'n':
if(Row < MaxRow)
Guy[Row][Col]=Guy[Row+1][Col];
break;
case 'S':
case 's':
if(Row > 0)
Guy[Row][Col]=Guy[Row-1][Col];
break;
case 'W':
case 'w':
if(Col > 0)
Guy[Row][Col]=Guy[Row][Col-1];
break;
case 'E':
case 'e':
if(Col < MaxCol)
Guy[Row][Col]=Guy[Row][Col+1];
break;
default:
cout<< "Enter a random move, please";
break;
}```
I don't know about the rest of your errors but if you post the complete code (or attach the file) someone will solve your errors.

6. Okay, here's the rest of the code.
//Quest for the Golden Thingy
#include <iostream.h>
#include <lvp/matrix.h>
#include <lvp/random.h>
#include <lvp/string.h>
#include <fstream.h>
typedef matrix<int> Dungeon;
void generate(Dungeon &Gamearea)
/* creates the area*/
{
for (int Row=0; Row<Gamearea.numrows(); Row++){
for(int Col=0; Col<Gamearea.numcols(); Col++)
cout<<"["<<Gamearea[Row][Col]<<"]";
cout<< endl;
}

}
int helpfile()
{
ifstream InFile("help.txt", ios::nocreate);
if (InFile.fail()){
cout<< "Help file unavailable. Figure it out for yourself, dumbass!";
return (0);
}
else{
String S;
while(getline(InFile,S))
cout<< S ;
return(0);
}
}
int move(int Row, int Col, const Dungeon &Gamearea, int &Guy)
/* Gets your move and moves your character accordingly*/
{
cout<<" Which way? N "<<endl;
cout<<" W E"<<endl;
cout<<" S "<<endl;
cout<<"Press H for help"<<endl;
char direction;
switch (direction){
case 'N':
case 'n':
if(Row<11)
Guy[Row][Col]=' ';
Guy=Gamearea[Row++][Col];
break;
case 'S':
case 's':
if (Row>0)
Guy[Row][Col]=' ';
Guy=Gamearea[Row--][Col];
break;
case 'W':
case 'w':
if (Col<11)
Guy[Row][Col]=' ';
Guy=Gamearea[Row][Col--];
break;
case 'E':
case 'e':
if (Col>0)
Guy[Row][Col]=' ';
Guy=Gamearea[Row][Col++];
break;
case 'H':
case 'h':
helpfile();
break;
default:
cout<< "Enter a valid move, please";
break;
}
return 0;
}
int Winner(const Dungeon &Gamearea,int &Guy,int &Goldenthingy)
{/*Checks location against the Golden thingy's. If the 2 match, a value is returned. Otherwise
a blank is returned*/
int Row, Col;
if(Guy==Goldenthingy)
return 0;
else return ' ';
}

int main()
{
Dungeon Gamearea(10,10,' ');
int Guy=Gamearea [0],[0];
int Goldenthingy=Gamearea [random(11)][random(11)];
int Row, Col =0;
do{
generate(Gamearea);
move(Row,Col,Gamearea,Guy);
}while ((Winner(Gamearea,Guy,Goldenthingy) == ' '));
generate(Gamearea);
cout<< "Congratulations! You have found the legendary Golden Thingy!!!!!!!!";
return(0);
}
Hope someone can figure this out.

7. HI,me again. I have just one problem with the program. I need it to output a symbol at where my character is. Like if my guy is at 5,5, the symbol is output at 5,5. I've tried to fix it, but I can't seem to get it to work.

8. I'm not sure what you mean with this. Just loop through the complete matrix and print the value.

Here's a small example that works on my system:
Code:
```#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define MAX_ROW 10
#define MAX_COL 10

#define EMPTY 0
#define GOAL 1

int matrix[MAX_ROW][MAX_COL];

void show(int Row, int Col)
{
system("cls"); // works for me, not on all systems

for(int r = 0; r < MAX_ROW; r++)
{
for(int c = 0; c < MAX_COL; c++)
{
if(c == Col && r == Row)
cout << "[*]";
else if(matrix[r][c] == GOAL)
cout << "[\$]";
else
cout << "[ ]";
}
cout<< endl;
}
}

int move(int *Row, int *Col)
{
char direction;

cout << "Which way? (N, E, S, W) :";
cin >> direction;

switch(toupper(direction))
{
case 'N': if(*Row > 0) *Row--; break;
case 'E': if(*Col > 0) *Col--; break;
case 'S': if(*Row < MAX_ROW-1) *Row++; break;
case 'W': if(*Col < MAX_COL-1) *Col++; break;
case 'Q': return -1;
}
return 0;
}

int main(void)
{
int Row = 0;
int Col = 0;

// Initialize board with zero's
//for(int i = 0; i < MAX_ROW; i++)
//	for(int j = 0; j < MAX_COL; j++)
//		matrix[i][j] = 0;
memset(&matrix, EMPTY, MAX_ROW * MAX_COL * sizeof(int));

srand(time(NULL));

matrix[rand()%MAX_ROW][rand()%MAX_COL] = GOAL;

show_board(Row, Col);

while(matrix[Row][Col] != GOAL)
{
if(move(&Row, &Col) == -1)
return -1;

show(Row, Col);
}

cout << "Congratulations! You have found the legendary Golden Thingy!!!!!!!!" << endl;
getchar();

return 0;
}```

9. Sorry forget about the previous post (I pressed the submit button in stead of the preview button).
This is the correct code:
Code:
```#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define MAX_ROW 10
#define MAX_COL 10

#define EMPTY 0
#define GOAL 1

int matrix[MAX_ROW][MAX_COL];

void show(int Row, int Col)
{
system("cls"); // works for me, not on all systems

for(int r = 0; r < MAX_ROW; r++)
{
for(int c = 0; c < MAX_COL; c++)
{
if(c == Col && r == Row)
cout << "[*]";
else if(matrix[r][c] == GOAL)
cout << "[\$]";
else
cout << "[ ]";
}
cout<< endl;
}
}

int move(int *Row, int *Col)
{
char direction;

cout << "Which way? (N, E, S, W) :";
cin >> direction;

switch(toupper(direction))
{
case 'N': if(*Row > 0) (*Row)--; break;
case 'E': if(*Col > 0) (*Col)--; break;
case 'S': if(*Row < MAX_ROW-1) (*Row)++; break;
case 'W': if(*Col < MAX_COL-1) (*Col)++; break;
case 'Q': return -1;
}
return 0;
}

int main(void)
{
int Row = 0;
int Col = 0;

// Initialize board with zero's
//for(int i = 0; i < MAX_ROW; i++)
//	for(int j = 0; j < MAX_COL; j++)
//		matrix[i][j] = 0;
memset(&matrix, EMPTY, MAX_ROW * MAX_COL * sizeof(int));

srand(time(NULL));

matrix[rand()%MAX_ROW][rand()%MAX_COL] = GOAL;

show(Row, Col);

while(matrix[Row][Col] != GOAL)
{
if(move(&Row, &Col) == -1)
return -1;

show(Row, Col);
}

cout << "Congratulations! You have found the legendary Golden Thingy!!!!!!!!" << endl;
getchar();

return 0;
}```

10. I'm not sure if it's been mentioned, but I'm pretty sure switch statements will not work with chars.

Try something like switch((int)toupper(mychar)) and test for the ascii equivalent inside the switch body. Refer to www.asciitable.com if you need to find the values.

Although I could be wrong (rather, the person who told me this could be wrong)

11. Originally posted by Dual-Catfish
I'm not sure if it's been mentioned, but I'm pretty sure switch statements will not work with chars.
And I'm pretty sure it does work with chars.
It only doesn't work with strings (except if you want to check the pointers and not the string itself).

12. I mean that it's supposed to display where Guy is in the matrix. Whenever I loop through and display the value, it either outputs all squares as blank, or all squares as having the guy in them.

13. Besides which, Monster's code doesn't work. getchar is an undeclared identifier.

14. Sorry about the getchar function. That's because I use VC++.
When running the application a dos box is opened and the applicatin is executed.
If the application is done the dos box is also closed and I can't see the result (the congratulations part).
That's why I put the getchar function at the end (something like: system("PAUSE"); )
So it doesn't belong there....

Popular pages Recent additions