Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- walk_through_the_matrix_with_arrow_keys_v1.c
- Task:
- https://web.facebook.com/photo.php?fbid=674784286651178&set=pcb.1568892626602915&type=3&theater&ifg=1
- Walk through the matrix with arrow keys.
- Start position is on the upper left corner.
- End position is on the down right corner.
- Author Dragan Milicev
- https://web.facebook.com/dmilicev
- Tested on Windows 7.
- Let me know of any errors (bugs) in the code you encountered, please.
- */
- // ----------BEGIN OF PROGRAM--------------------------------------------------
- #include <stdio.h>
- //#include <conio.h> // for _getch() and _getche()
- #include <string.h> // for puts()
- #include <windows.h>
- // tracking cursor all the time, for cursor position functions
- // gotoxy(x,y) , wherex() , wherey(), showcursor(), hidecursor()
- #define UP 72 // up arrow
- #define DOWN 80 // down arrow
- #define LEFT 75 // left arrow
- #define RIGHT 77 // right arrow
- #define ESC 27 // Escape key
- // Global variables
- char trace[1000]; // array for trace
- int movements = 0; // movements counter
- int finish = 0; // flag, if 1 then cursor is on finish down right position
- /*
- In array trace[]
- 1 - Up
- 2 - Down
- 3 - Left
- 4 - Right
- */
- // ----------CURSOR CONTROL FUNCTIONS------------------------------------------
- // place cursor at position ( x, y ) = ( column, row )
- void gotoxy( int column, int row )
- {
- COORD coord;
- coord.X = column;
- coord.Y = row;
- SetConsoleCursorPosition( GetStdHandle(STD_OUTPUT_HANDLE), coord );
- }
- // return x coordinate (column) of current cursor position
- // on failure return -1
- int wherex()
- {
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- if (!GetConsoleScreenBufferInfo( GetStdHandle(STD_OUTPUT_HANDLE), &csbi ) )
- return -1;
- return csbi.dwCursorPosition.X;
- }
- // return y coordinate (row) of current cursor position
- // on failure return -1
- int wherey()
- {
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- if (!GetConsoleScreenBufferInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &csbi ) )
- return -1;
- return csbi.dwCursorPosition.Y;
- }
- // END OF-----CURSOR CONTROL FUNCTIONS------------------------------------------
- // print matrix dimension x,y
- void print_matrix( int width, int height )
- {
- int x, y;
- char str[100];
- sprintf(str,"%c",'o');
- for( y=0; y<height; y++ )
- for( x=0; x<width; x++ )
- {
- gotoxy(x,y);
- fputs(str, stdout);
- }
- gotoxy(width,height); // finish position
- putchar('F');
- gotoxy(0,0); // first position
- putchar('S');
- gotoxy(0,0);
- } // print_matrix()
- // ----------FUNCTIONS TO MOVE CURSOR -----------------------------------------
- // Move cursor up, print 1, trace[movements++]=1
- void move_up( int x, int y, int width, int heigth )
- {
- if( y>0 && y<heigth )
- {
- gotoxy(x,y-1);
- putchar('1');
- gotoxy(x,y-1);
- trace[movements++] = 1;
- }
- }
- // Move cursor down, print 1, trace[movements++]=2
- void move_down( int x, int y, int width, int heigth )
- {
- if( y>=0 && y<heigth-1 )
- {
- gotoxy(x,y+1);
- putchar('2');
- gotoxy(x,y+1);
- trace[movements++] = 2;
- }
- }
- // Move cursor left, print 1, trace[movements++]=3
- void move_left( int x, int y, int width, int heigth )
- {
- if( x>0 && x<width )
- {
- gotoxy(x-1,y);
- putchar('3');
- gotoxy(x-1,y);
- trace[movements++] = 3;
- }
- }
- // Move cursor right, print 1, trace[movements++]=4
- void move_right( int x, int y, int width, int heigth )
- {
- if( x>=0 && x<width-1 )
- {
- gotoxy(x+1,y);
- putchar('4');
- gotoxy(x+1,y);
- trace[movements++] = 4;
- }
- }
- // END OF----FUNCTIONS TO MOVE CURSOR -----------------------------------------
- // ----------KEYBOARD CONTROL--------------------------------------------------
- /*
- Keyboard control:
- UP up arrow
- DOWN down arrow
- LEFT left arrow
- RIGHT right arrow
- Key Escape, ESC (27) for quit.
- */
- int keyboard_control( int width, int heigth )
- {
- int choice; // for pressed key
- while( 1 ) { // an infinite loop that exits with choice Ecsape, ESC (27)
- choice = _getch(); // read the keyboard
- if( choice == 0 || choice == 224 ) // if is pressed function key with leading 0 or 224
- {
- choice = _getch(); // let's empty that 0 or 224
- if( choice == UP ) // if pressed up arrow
- {
- move_up( wherex(), wherey() , width, heigth );
- }
- if( choice == DOWN ) // if pressed down arrow
- {
- move_down( wherex(), wherey() , width, heigth );
- }
- if( choice == LEFT ) // if pressed left arrow
- {
- move_left( wherex(), wherey() , width, heigth );
- }
- if( choice == RIGHT ) // if pressed right arrow
- {
- move_right( wherex(), wherey() , width, heigth );
- }
- } // end of: if is pressed function key with leading 0 or 224
- else // if pressed ordinary keys
- {
- if( choice == ESC ) // if is pressed ESC
- {
- return( choice ); // end of work
- }
- } // end of: if pressed ordinary keys
- if( (wherex()==width-1) && (wherey()==heigth-1) )
- return( choice ); // end of work, cursor is on down right position
- } // end of: while( 1 )
- return( choice ); // have to return something
- }
- // END OF----KEYBOARD CONTROL--------------------------------------------------
- int main(void)
- {
- int i, width=10, height=10, left=0, right=0, up=0, down=0;
- print_matrix(width,height);
- keyboard_control(width,height);
- gotoxy( 0, height+3 );
- printf(" There was %d movements. \n\n", movements);
- printf(" Trace is: ");
- for(i=0; i<movements; i++)
- {
- printf("%2d", trace[i]);
- if(trace[i]==1)
- up++;
- if(trace[i]==2)
- down++;
- if(trace[i]==3)
- left++;
- if(trace[i]==4)
- right++;
- }
- printf("\n\n There are %2d up (1) movements. \n", up);
- printf("\n There are %2d down (2) movements. \n", down);
- printf("\n There are %2d left (3) movements. \n", left);
- printf("\n There are %2d right (4) movements. \n\n", right);
- return 0;
- }
Add Comment
Please, Sign In to add comment