arrow keys in arrays

This is a discussion on arrow keys in arrays within the C++ Programming forums, part of the General Programming Boards category; i got this code from the FAQ and i tried adopting it to my code i already had but i ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    44

    arrow keys in arrays

    i got this code from the FAQ and i tried adopting it to my code i already had but i cant get it to work.

    basically what i need to do is i made a grid using ascii characters and i have to have a cursor move about that grid but my program always crashes when it gets to that part cause i know i did something wrong there

    here is all the code i have so far

    Code:
    #include <iostream>
    #include <conio.h>
    #include <windows.h> 
    #include <stdio.h> 
    #include "Definitions.h"
    using namespace std;
    
    enum
    {
      KEY_ESC     = 27,
      ARROW_UP    = 256 + 72,
      ARROW_DOWN  = 256 + 80,
      ARROW_LEFT  = 256 + 75,
      ARROW_RIGHT = 256 + 77
    };
    
    static int get_code ( void )
    {
      int ch = getch();
    
      if ( ch == 0 || ch == 224 )
        ch = 256 + getch();
    
      return ch;
    }
    
    class Darray{
    protected:
    		int DEPTH,
    			  ROW,
    			  COL;
    		int*** array;
    
    public:
    	
    	Darray();
    	Darray(int depth, int row, int col);
    	~Darray();
    	Darray(const Darray& darray);						//copy constructor
    	Darray& Darray::operator= (const Darray& darray);	//assignment operator
    
    	void getDimensions(int DEPTH, int ROW, int COL);
    	void destroy();
    	void create();
    	void init();
    	void resetSize(int DEPTH, int ROW, int COL);
    };
    
    Darray::Darray()
    {
    	DEPTH=0;
    	ROW=0;
    	COL=0;
    	array=NULL;
    }
    
    void Darray::create()
    {
    	array = new int** [DEPTH];
    	for(int d=0; d<DEPTH; d++) {
    		*(array+d) = new int* [ROW];
    		for (int r=0; r<ROW; r++)
    			*(*(array+d)+r) = new int [COL];
    	}
    }
    
    Darray::Darray(int depth, int row, int col)
    {
    	DEPTH = depth;
    	ROW = row;
    	COL = col;
    	create();
    	init();
    
    }
    
    Darray::~Darray()
    {
    	destroy();
    }
    
    Darray::Darray(const Darray& darray)
    {
    	DEPTH = darray.DEPTH;
    	ROW = darray.ROW;
    	COL = darray.COL;
    	create();
    
    	for(int d=0; d<DEPTH; d++)
    		for(int r=0; r<ROW; r++)
    			for(int c=0; c<COL; c++)
    				array[d][r][c] = darray.array[d][r][c];
    }
    
    Darray& Darray::operator= (const Darray& darray)
    {
    	resetSize(darray.DEPTH, darray.ROW, darray.COL);
    
    	for(int d=0; d<DEPTH; d++)
    		for(int r=0; r<ROW; r++)
    			for(int c=0; c<COL; c++)
    				array[d][r][c] = darray.array[d][r][c];
    			
    			return *this;	
    }
    
    void Darray::destroy()
    {
    	 for (int d=0; d<DEPTH; d++)
    	 {
    		 for(int r=0; r<ROW; r++)
    			 delete [] *(*(array+d)+r);
    		 delete [] *(array+d);
    	 }
    	 delete [] array;
     }
    
    
    void Darray::init()
    {
    	for(int d=0; d<DEPTH; d++)
    		for(int r=0; r<ROW; r++)
    			for(int c=0; c<COL; c++)
    				array[d][r][c] = 0;
    }
    
    void Darray::getDimensions(int DEPTH, int ROW, int COL)
    {
    
    	cout << "The Depth is: " << DEPTH << "  The Row is: " << ROW
    		<< "  The Columns is: " << COL << endl;	
    }
    
    void Darray::resetSize(int depth, int row, int col)
    {
    	Darray temp (*this);
    	destroy();
    	DEPTH=depth;
    	ROW=row;
    	COL=col;
    	create();
    	init();
    }
    
    /***********************************************************************************/
    class Grid : public Darray {
    private:
    	int currentRow,
    		currentDepth,
    		currentCol;
    
    public:
    	Grid();
    	void arrow_keys();
    	void UsFunction();
    	void DsFunction();
    	void clear_da_screen(void);
    	void print () const;
    };
    
    Grid::Grid()
    {
    	int depth,
    		row,
    		col;
    
    
    	cout << "Enter the grid dimensions: " << endl;
    	cin >> depth >> row >> col;
    
    	// set up dimensions
    	DEPTH = depth;
    	ROW = row;
    	COL = col;
    
    	// set up position
    	currentRow = ROW / 2 + 1;
    	currentDepth = DEPTH / 2 + 1;
    	currentCol = COL / 2 + 1;
    
    	create();
    
    }
    
    void Grid::print () const
    {
    	
    	// makes grid
    
    	//makes top row or grid
    	for(int d=0; d<DEPTH; d++)
    	{
    			if(d<DEPTH)
    			{				
    					cout << U_L;
    					cout << LINE;
    			
    				for(int c=0; c<COL-1; c++)
    				{						
    						cout << T_M;
    						cout << LINE;
    						
    				}// end for
    									
    					cout << U_R;					
    					cout << "\n";
    
    					for( c=0; c<COL+1; c++)
    					{
    
    					cout << VERT_LINE;
    					cout << " ";
    
    					}// end vertical line first row
    
    			}cout << "\n";// end top of grid
    
    		// makes gird middle	
    		for(int r=0; r<ROW-1; r++)		
    			if(r<ROW)
    			{
    				cout << L_SM;
    				cout << LINE;
    		
    				for(int c=0; c<COL-1; c++)
    					{
    						
    						cout << M;
    						cout << LINE;
    					}// end for
    				
    				
    				cout << R_SM;
    				cout << "\n";
    
    				for( c=0; c<COL+1; c++)
    				{
    					cout << VERT_LINE;
    					cout << " ";
    				}cout << endl;
    			}// end middle of grid
    			
    			// makes last line
    			if(d<DEPTH)
    			{
    				
    				cout << L_L;
    				cout << LINE;
    
    				for(int c=0; c<COL-1; c++)
    					{						
    						cout << L_MT;
    						cout << LINE;				
    					}
    			
    				cout << L_R;			
    			}
    			cout << endl;	
    			
    	}//end last row
    	
    	
    }
    void Grid::UsFunction()
    {
    
    		resetSize(DEPTH, ROW, COL);
    
    			for(int d=0; d<DEPTH; d++)
    				for(int r=0; r<ROW; r++)
    					for(int c=0; c<COL; c++)
    					{
    						array[d][r][c] = array[currentDepth][currentRow][currentCol];
    					}
    
    
    }
    
    void Grid::DsFunction()
    {
    	resetSize(DEPTH, ROW, COL);
    
    		for(int d=0; d<DEPTH; d++)
    				for(int r=0; r<ROW; r++)
    					for(int c=0; c<COL; c++)
    					{
    						array[d][r][c] = array[currentDepth][currentRow][currentCol];
    					}
    }
    
    
    void Grid::arrow_keys()
    {
    
    
    
    	 array[DEPTH][ROW][COL];
        for(int d=0; d<DEPTH; d++)
        {
            for(int r=0; r<ROW; r++)
            {
    			for(int c=0; c<COL; c++)
    			{
                array[d][r][c]=' ';
    			}
            }
        }   
      
    	print();
      
      int d,r,c;
      d=0;
      r=0;
      c=0;
      
        
      int ch;
    
      while ( ( ch = get_code() ) != KEY_ESC ) {
         // clear_da_screen();
        switch ( ch ) 
        {
        case ARROW_UP:
          //printf ( "UP\n" );
          c=c-1;
          break;
        case ARROW_DOWN:
          //printf ( "DOWN\n" );
          c=c+1;
          break;
        case ARROW_LEFT:
          //printf ( "LEFT\n" );
          r=r-1;
          break;
        case ARROW_RIGHT:
          //printf ( "RIGHT\n" );
          r=r+1;
          break;
          
        
        }
        
        array[c][r][d]=ACTIVE;
           for(int d=0; d<DEPTH; d++)
          {
            for(int r=0; r<ROW; r++)
            {
    			for(int c=0; c<COL; c++)
    			{
                cout<<array[d][r][c]<<"";
    			}
            }cout<<"\n";
          } 
          array[c][r][d]=VISIT;
          //break;
          
          
          
      }      
         
         cin.get();
         cin.get(); 
    } 
    
    /*void Grid::clear_da_screen(void)
    {
        COORD                       coordScreen = { 0, 0 };
        DWORD                       cCharsWritten;
        CONSOLE_SCREEN_BUFFER_INFO  csbi;
        DWORD                       dwConSize;
        HANDLE                      hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        GetConsoleScreenBufferInfo(hConsole, &csbi);
        dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
        FillConsoleOutputCharacter(hConsole, TEXT(' '), 
                                   dwConSize, coordScreen, &cCharsWritten);
        GetConsoleScreenBufferInfo(hConsole, &csbi);
        FillConsoleOutputAttribute(hConsole, csbi.wAttributes, 
                                   dwConSize, coordScreen, &cCharsWritten);
        SetConsoleCursorPosition(hConsole, coordScreen);
    }
    */
    /******************************************************************************/
    class Cell{
    	private:
    		 bool visit,
    			 active;
    
    	public:
    		Cell();
    		~Cell();
    		void fill();
    
    };
    
    Cell::Cell()
    {
    	visit = 0;
    	active = 0;
    }
    
    Cell::~Cell()
    {
    
    }
    //fill cell when visited
    void Cell::fill()
    {
    
    }
    
    
    /*******************************************************************************/
    void main()
    {
    	
    
    	//Darray d;
    	Grid g;
    	Cell c;
    	//d.getDimensions(depth, row, col);
    
    	
    	g.arrow_keys();
    	g.print();
    
    }
    here is my definitions file:

    Code:
    #define M char (197);
    #define U_L char (218);
    #define L_L char (192);
    #define U_R char (191);
    #define L_R char (217);
    #define T_M char (194);
    #define L_MT char (193);
    #define L_SM char (195);
    #define R_SM char (180);
    #define VERT_LINE char (179);
    #define VISIT char (219);
    #define LINE  char (196);
    #define ACTIVE char (248);
    what is it that im doing wrong i have never done this before

  2. #2
    Registered User
    Join Date
    Mar 2006
    Posts
    44
    here is the function it is in so i dont have to search for it

    Code:
    void Grid::arrow_keys()
    {
    
    
    
    	 array[DEPTH][ROW][COL];
        for(int d=0; d<DEPTH; d++)
        {
            for(int r=0; r<ROW; r++)
            {
    			for(int c=0; c<COL; c++)
    			{
                array[d][r][c]=' ';
    			}
            }
        }   
      
    	print();
      
      int d,r,c;
      d=0;
      r=0;
      c=0;
      
        
      int ch;
    
      while ( ( ch = get_code() ) != KEY_ESC ) {
         // clear_da_screen();
        switch ( ch ) 
        {
        case ARROW_UP:
          //printf ( "UP\n" );
          c=c-1;
          break;
        case ARROW_DOWN:
          //printf ( "DOWN\n" );
          c=c+1;
          break;
        case ARROW_LEFT:
          //printf ( "LEFT\n" );
          r=r-1;
          break;
        case ARROW_RIGHT:
          //printf ( "RIGHT\n" );
          r=r+1;
          break;
          
        
        }
        
        array[c][r][d]=ACTIVE;
           for(int d=0; d<DEPTH; d++)
          {
            for(int r=0; r<ROW; r++)
            {
    			for(int c=0; c<COL; c++)
    			{
                cout<<array[d][r][c]<<"";
    			}
            }cout<<"\n";
          } 
          array[c][r][d]=VISIT;
          //break;
          
          
          
      }      
         
         cin.get();
         cin.get(); 
    }

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Quote Originally Posted by tunerfreak
    here is my definitions file:

    Code:
    #define M char (197);
    #define U_L char (218);
    #define L_L char (192);
    #define U_R char (191);
    #define L_R char (217);
    #define T_M char (194);
    #define L_MT char (193);
    #define L_SM char (195);
    #define R_SM char (180);
    #define VERT_LINE char (179);
    #define VISIT char (219);
    #define LINE  char (196);
    #define ACTIVE char (248);
    what is it that im doing wrong i have never done this before
    Try
    Code:
    #define M 197
    #define U_L 218
    #define L_L 192
    #define U_R 191
    etc...
    #define replaces everything after the name in the code so each time you used R_SM, you really added to the code char (180);. Both char and ; are causing your problems.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    44
    no i need thoes parentheses i get errors if they are not there. but i took out the ' ; ' though and its fine but it still crashes when i hit the arrow keys.
    Last edited by tunerfreak; 03-05-2006 at 08:17 AM.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    44
    is it because i have an int array and im making it equal to ACTIVE and VISIT which is a char

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    44
    i figured out how to get the arrow keys to work but no how to i print the cursors to move around.

    this is my attempt at doig it but i am gettint 90 some errors can u help me out here

    redone grid class

    Code:
    class Grid : public Darray {
    private:
    	int currentRow,
    		currentDepth,
    		currentCol;
    	bool visit,
    		active;
    
    public:
    	Grid();
    	void arrow_keys();
    	void UsFunction();
    	void DsFunction();
    	void clear_screen(void);
    	void print () const;
    	void A();
    	void V();
    	void setact(bool act) {this->active = act;} 
    	void setvis(bool vis) {this->visit = vis;}
    	void setpoint();
    };
    
    Grid::Grid()
    {
    	int depth,
    		row,
    		col;
    
    
    	cout << "Enter the grid dimensions: " << endl;
    	cin >> depth >> row >> col;
    
    	// set up dimensions
    	DEPTH = depth;
    	ROW = row;
    	COL = col;
    
    	// set up position
    	currentRow = ROW / 2 + 1;
    	currentDepth = DEPTH / 2 + 1;
    	currentCol = COL / 2 + 1;
    
    	create();
    
    }
    
    
    void Grid::print () const
    {
    	
    	// makes grid
    
    	//makes top row or grid
    	for(int d=0; d<DEPTH; d++)
    	{
    			if(d<DEPTH)
    			{				
    					cout << U_L;
    					cout << LINE;
    			
    				for(int c=0; c<COL-1; c++)
    				{						
    						cout << T_M;
    						cout << LINE;
    						
    				}// end for
    									
    					cout << U_R;					
    					cout << "\n";
    
    					for( c=0; c<COL+1; c++)
    					{
    
    					cout << VERT_LINE;
    					cout << " ";
    
    					}// end vertical line first row
    
    			}cout << "\n";// end top of grid
    
    		// makes gird middle	
    		for(int r=0; r<ROW-1; r++)		
    			if(r<ROW)
    			{
    				cout << L_SM;
    				cout << LINE;
    		
    				for(int c=0; c<COL-1; c++)
    					{
    						
    						cout << M;
    						cout << LINE;
    					}// end for
    				
    				
    				cout << R_SM;
    				cout << "\n";
    
    				for( c=0; c<COL+1; c++)
    				{
    					cout << VERT_LINE;
    					cout << " ";
    				}cout << endl;
    			}// end middle of grid
    			
    			// makes last line
    			if(d<DEPTH)
    			{
    				
    				cout << L_L;
    				cout << LINE;
    
    				for(int c=0; c<COL-1; c++)
    					{						
    						cout << L_MT;
    						cout << LINE;				
    					}
    			
    				cout << L_R;			
    			}
    			cout << endl;	
    			
    	}//end last row
    	
    	
    }
    
    void Grid::UsFunction()
    {
    
    		resetSize(DEPTH, ROW, COL);
    
    			COL = COL + 2;
    			ROW = ROW + 2;
    
    
    }
    
    void Grid::DsFunction()
    {
    	resetSize(DEPTH, ROW, COL);
    
    		for(int d=0; d<DEPTH; d++)
    				for(int r=0; r<ROW; r++)
    					for(int c=0; c<COL; c++)
    					{
    						array[d][r][c] = array[currentDepth][currentRow-2][currentCol-2];
    					}
    }
    
    
    
    void Grid::A()
    {
    		array[currentDepth][currentRow][currentCol] = setact(this->true);
    		array[currentDepth][currentRow][currentCol] = setpoint(ACTIVE);
    		array[currentDepth][currentRow][currentCol] = setvis(this->false);
    }
    
    void Grid::V()
    {
    		array[currentDepth][currentRow][currentCol] = setact(this->false);
    		array[currentDepth][currentRow][currentCol] = setpoint(VISIT);
    		array[currentDepth][currentRow][currentCol] = setvis(this->true);
    }
    
    
    
    void Grid::arrow_keys()
    {
      
        
      int ch;
    
      while ( ( ch = get_code() ) != KEY_ESC ) {
          clear_screen();
    	  print();
        switch ( ch ) 
        {
        case ARROW_UP:
          printf ( "UP\n" );
    			
    	  if((currentRow = currentRow-1 >= 0))
    	{
    		A();
    		currentRow = currentRow - 1;
    		V();
    	}
    	  else if((currentRow = currentRow-1 < 0))
    	{
    		A();
    		currentRow = currentRow - 1;
    		V();
    	}
    			  
          break;
    
        case ARROW_DOWN:
          printf ( "DOWN\n" );
    
    		if(( currentRow = currentRow + 1 <= ROW-1 ))
    	{
    		A();
    		currentRow = currentRow + 1;
    		V();
    	}
    	else if((currentRow = currentRow + 1 >ROW-1 ))
    	{
    		A();
    		currentRow = 0;
    		V();
    	}
    
          break;
    
        case ARROW_LEFT:
          printf ( "LEFT\n" );
    
    	  if(( currentCol = currentCol - 1 <= COL-1 ))
    	{
    		A();
    		currentCol = currentCol - 1;
    		V();
    	}
    	else if((currentCol = currentCol - 1 > COL-1))
    	{
    		A();
    		currentCol = 0;
    		V();
    	}
          
          break;
    
        case ARROW_RIGHT:
          printf ( "RIGHT\n" );
    	  
    	  if((currentCol = currentCol - 1 <= COL-1))
    	{
    		A();
    		currentCol = currentCol - 1;
    		V();
    	}
    	else if((currentCol = currentCol - 1 > COL - 1))
    	{
    		A();
    		currentCol = 0;
    		V();
    	}
    	  
          break;
          
        
        }
       
          
      }      
         
         cin.get();
         cin.get(); 
    
    }
    
    void Grid::clear_screen(void)
    {
        COORD                       coordScreen = { 0, 0 };
        DWORD                       cCharsWritten;
        CONSOLE_SCREEN_BUFFER_INFO  csbi;
        DWORD                       dwConSize;
        HANDLE                      hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        GetConsoleScreenBufferInfo(hConsole, &csbi);
        dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
        FillConsoleOutputCharacter(hConsole, TEXT(' '), 
                                   dwConSize, coordScreen, &cCharsWritten);
        GetConsoleScreenBufferInfo(hConsole, &csbi);
        FillConsoleOutputAttribute(hConsole, csbi.wAttributes, 
                                   dwConSize, coordScreen, &cCharsWritten);
        SetConsoleCursorPosition(hConsole, coordScreen);
    }
    section of code that im talkin about

    Code:
    void Grid::A()
    {
    		array[currentDepth][currentRow][currentCol] = setact(this->true);
    		array[currentDepth][currentRow][currentCol] = setpoint(ACTIVE);
    		array[currentDepth][currentRow][currentCol] = setvis(this->false);
    }
    
    this is my attempt at getting the cursors goin
    
    void Grid::V()
    {
    		array[currentDepth][currentRow][currentCol] = setact(this->false);
    		array[currentDepth][currentRow][currentCol] = setpoint(VISIT);
    		array[currentDepth][currentRow][currentCol] = setvis(this->true);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Movement with arrow keys
    By louis_mine in forum C Programming
    Replies: 3
    Last Post: 02-06-2005, 04:35 PM
  2. Interfacing with arrow keys...
    By adityakarnad in forum Game Programming
    Replies: 1
    Last Post: 08-30-2003, 11:25 PM
  3. Ascii code for arrow keys
    By beginner in forum C Programming
    Replies: 1
    Last Post: 11-07-2002, 01:29 PM
  4. msdos arrow keys?
    By seditee in forum Game Programming
    Replies: 3
    Last Post: 05-08-2002, 12:29 AM
  5. Arrow keys
    By Nutshell in forum C Programming
    Replies: 5
    Last Post: 03-27-2002, 11:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21