| | |
moving around in an array with arrow keys
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2005
Posts: 91
Reputation:
Solved Threads: 1
i am totally stumped on what to do here. basically whats goin on is i have a 3D dynamic array and i need to have a cursor inside of it and move around on the inside. i do i go about doing that? like i said im completely stumped.
here is my code:
here is my code:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <conio.h> #include "Definitions.h" using namespace std; 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) { destroy(); DEPTH=depth; ROW=row; COL=col; create(); init(); } /***********************************************************************************/ class Grid : public Darray { private: int currentRow, currentDepth, currentCol; public: Grid(); void arrow_keys(); void UsDsFunction(int DEPTH, int ROW, int COL); 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::UsDsFunction(int DEPTH, int ROW, int COL) { int num, num2; cout << "would you like to upsize, downsize or keep the dimensions the same? " << "\n\n"; cout << "Choose an option: " << endl; cout << "1.) Upsize" << endl; cout << "2.) Downsize" << endl; cout << "3.) Keep the same" << endl; cin >> num; switch (num) { case 1: resetSize(DEPTH, ROW, COL); break; case 2: resetSize(DEPTH, ROW, COL); break; case 3: cout << "Grid staying the same" << endl; break; } } void Grid::arrow_keys() { char key; bool flag = false; do { key = getch(); switch (toascii(key)) { case 96: case 224: flag = true; break; case 72: // up arrow if (flag) { cout << ACTIVE; cout << endl; flag = false; } else cout << "try again. " << endl; break; case 77: if (flag) { cout << ACTIVE; cout << endl; flag = false; } else cout << "try again. " << endl; break; case 75: if (flag) { cout << ACTIVE; cout << endl; flag = false; } else cout << "try again. " << endl; break; case 80: if (flag) { cout << ACTIVE; cout << endl; flag = false; } else cout << "try again. " << endl; break; case 13: cout << "Enter" << endl; break; default: cout << "try again. " << endl; break; } } while (key != 13); } /******************************************************************************/ class Cell{ private: bool visit, active; public: Cell(); ~Cell(); void fill(); }; Cell::Cell() { visit = 0; active = 0; } Cell::~Cell() { } //fill cell when visited void fill() { } /*******************************************************************************/ void main() { //Darray d; Grid g; Cell c; //d.getDimensions(depth, row, col); g.print(); g.arrow_keys(); //g.UsDsFunction(depth, row, col); g.print(); }
•
•
•
•
Originally Posted by tyczj
i am totally stumped on what to do here. basically whats goin on is i have a 3D dynamic array and i need to have a cursor inside of it and move around on the inside. i do i go about doing that? like i said im completely stumped.
here is my code:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <conio.h> #include "Definitions.h" using namespace std; 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) { destroy(); DEPTH=depth; ROW=row; COL=col; create(); init(); } /***********************************************************************************/ class Grid : public Darray { private: int currentRow, currentDepth, currentCol; public: Grid(); void arrow_keys(); void UsDsFunction(int DEPTH, int ROW, int COL); 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::UsDsFunction(int DEPTH, int ROW, int COL) { int num, num2; cout << "would you like to upsize, downsize or keep the dimensions the same? " << "\n\n"; cout << "Choose an option: " << endl; cout << "1.) Upsize" << endl; cout << "2.) Downsize" << endl; cout << "3.) Keep the same" << endl; cin >> num; switch (num) { case 1: resetSize(DEPTH, ROW, COL); break; case 2: resetSize(DEPTH, ROW, COL); break; case 3: cout << "Grid staying the same" << endl; break; } } void Grid::arrow_keys() { char key; bool flag = false; do { key = getch(); switch (toascii(key)) { case 96: case 224: flag = true; break; case 72: // up arrow if (flag) { cout << ACTIVE; cout << endl; flag = false; } else cout << "try again. " << endl; break; case 77: if (flag) { cout << ACTIVE; cout << endl; flag = false; } else cout << "try again. " << endl; break; case 75: if (flag) { cout << ACTIVE; cout << endl; flag = false; } else cout << "try again. " << endl; break; case 80: if (flag) { cout << ACTIVE; cout << endl; flag = false; } else cout << "try again. " << endl; break; case 13: cout << "Enter" << endl; break; default: cout << "try again. " << endl; break; } } while (key != 13); } /******************************************************************************/ class Cell{ private: bool visit, active; public: Cell(); ~Cell(); void fill(); }; Cell::Cell() { visit = 0; active = 0; } Cell::~Cell() { } //fill cell when visited void fill() { } /*******************************************************************************/ void main() { //Darray d; Grid g; Cell c; //d.getDimensions(depth, row, col); g.print(); g.arrow_keys(); //g.UsDsFunction(depth, row, col); g.print(); }
Supply the file if it has changed from your previous post. Some of your for loops... the variable should be initialised as int c. You've missed that.
Change void main to int main.
What exactly do you mean have a cursor in the 3d array?
*Voted best profile in the world*
•
•
Join Date: Mar 2005
Posts: 91
Reputation:
Solved Threads: 1
thats right i forgot about the definitions file
and i mean its like ur playing a video game where u have a player(cursor) and you move the player(cursor) around in the array
C++ Syntax (Toggle Plain Text)
#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);
and i mean its like ur playing a video game where u have a player(cursor) and you move the player(cursor) around in the array
This code has it's problems but it's the best I could come up with on the fly.
It doesn't check beyond the bounds of the grid, so be careful of overflow.
The letter 'P' indicates the player.
Use the arrow keys to move it up and down. Maybe?
It doesn't check beyond the bounds of the grid, so be careful of overflow.
The letter 'P' indicates the player.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <conio.h> #include <windows.h> #include <stdio.h> //#include "Definitions.h" using namespace std; void clear_da_screen(void); /* System dependent key codes */ 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; } int main() { char crap[5][5]; for(int i=0; i<5; i++) { for(int j=0; j<5; j++) { crap[i][j]='-'; } } int x,y; x=0; y=0; int ch; while ( ( ch = get_code() ) != KEY_ESC ) { clear_da_screen(); switch ( ch ) { case ARROW_UP: printf ( "UP\n" ); y=y-1; break; case ARROW_DOWN: printf ( "DOWN\n" ); y=y+1; break; case ARROW_LEFT: printf ( "LEFT\n" ); x=x-1; break; case ARROW_RIGHT: printf ( "RIGHT\n" ); x=x+1; break; } crap[y][x]='P'; for(int i=0; i<5; i++) { for(int j=0; j<5; j++) { cout<<crap[i][j]<<" "; }cout<<"\n"; } crap[y][x]='-'; //break; } cin.get(); cin.get(); } void 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); }
Use the arrow keys to move it up and down. Maybe?
*Voted best profile in the world*
•
•
•
•
Originally Posted by Narue
>static int get_code ( void )
It's good to know that someone gets some use out of that function.
:cheesy:
[edit] Actually I think some of that directional key moving stuff
was written, or credited to you [/edit]
*Voted best profile in the world*
•
•
Join Date: Mar 2005
Posts: 91
Reputation:
Solved Threads: 1
ok so i tried rewriting it to fit what im trying to do but im gettin a linking error
error LNK2001: unresolved external symbol "public: void __thiscall Grid::clear_da_screen(void)" (?clear_da_screen@Grid@@QAEXXZ)
fatal error LNK1120: 1 unresolved externals
thats all that u gave me and my attempt at rewriting it
[edit]
never mind stupid mistake on my part it compiles and links fine but it crashes now when i run it
[/edit]
error LNK2001: unresolved external symbol "public: void __thiscall Grid::clear_da_screen(void)" (?clear_da_screen@Grid@@QAEXXZ)
fatal error LNK1120: 1 unresolved externals
C++ Syntax (Toggle Plain Text)
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]=' '; } } } int r,c; r=0; c=0; int ch; while ( ( ch = get_code() ) != KEY_ESC ) { clear_da_screen(); switch ( ch ) { case ARROW_UP: //printf ( "UP\n" ); r=r-1; break; case ARROW_DOWN: //printf ( "DOWN\n" ); r=r+1; break; case ARROW_LEFT: //printf ( "LEFT\n" ); c=c-1; break; case ARROW_RIGHT: //printf ( "RIGHT\n" ); c=c+1; break; } array[d][r][c]=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[d][r][c]=VISIT; //break; } cin.get(); cin.get(); } void 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); }
thats all that u gave me and my attempt at rewriting it
[edit]
never mind stupid mistake on my part it compiles and links fine but it crashes now when i run it
[/edit]
![]() |
Similar Threads
- How do you check if arrow keys are pressed? (C)
- SQL Plus command line: Arrow keys not giving previous commands back (Oracle)
- Find if user hit arrow keys (C++)
- need help with the use of arrow keys (C)
Other Threads in the C++ Forum
- Previous Thread: Have I gone totally brain dead? cin.get()??
- Next Thread: Simulation of Main memory compression
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline graph homeworkhelper iamthwee ifstream input int integer java lib linux list loop looping loops map math matrix memory multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






