Thread: Moving through matrices?

  1. #1
    Unregistered
    Guest

    Question 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. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    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. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680

    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. #4
    Unregistered
    Guest
    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. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    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. #6
    Unregistered
    Guest
    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. #7
    Unregistered
    Guest
    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. #8
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    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. #9
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    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. #10
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    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. #11
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    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. #12
    Unregistered
    Guest
    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. #13
    Unregistered
    Guest
    Besides which, Monster's code doesn't work. getchar is an undeclared identifier.

  14. #14
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    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 subscribe to a feed

Similar Threads

  1. C simple Matrices
    By Cyberman86 in forum C Programming
    Replies: 3
    Last Post: 05-07-2009, 05:20 PM
  2. adding matrices, help with switches
    By quiet_forever in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2007, 08:21 AM
  3. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  4. 3D moving
    By bluehead in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2005, 05:46 AM
  5. Problem multiplying rotation matrices together
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 03-04-2003, 09:20 AM