dmilicev

walk_through_the_matrix_with_arrow_keys_v1.c

May 2nd, 2020
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.72 KB | None | 0 0
  1. /*
  2.  
  3.     walk_through_the_matrix_with_arrow_keys_v1.c
  4.  
  5.     Task:
  6. https://web.facebook.com/photo.php?fbid=674784286651178&set=pcb.1568892626602915&type=3&theater&ifg=1
  7.  
  8.     Walk through the matrix with arrow keys.
  9.     Start position is on the upper left corner.
  10.     End position is on the down right corner.
  11.  
  12. Author Dragan Milicev
  13. https://web.facebook.com/dmilicev
  14.  
  15. Tested on Windows 7.
  16.  
  17. Let me know of any errors (bugs) in the code you encountered, please.
  18.  
  19. */
  20.  
  21.  
  22. // ----------BEGIN OF PROGRAM--------------------------------------------------
  23.  
  24. #include <stdio.h>
  25. //#include <conio.h>          // for _getch() and _getche()
  26. #include <string.h>         // for puts()
  27. #include <windows.h>
  28. // tracking cursor all the time, for cursor position functions
  29. // gotoxy(x,y) , wherex() , wherey(), showcursor(), hidecursor()
  30.  
  31. #define UP          72      // up arrow
  32. #define DOWN        80      // down arrow
  33. #define LEFT        75      // left arrow
  34. #define RIGHT       77      // right arrow
  35. #define ESC         27      // Escape key
  36.  
  37.  
  38. // Global variables
  39. char trace[1000];           // array for trace
  40. int  movements = 0;         // movements counter
  41. int finish = 0;             // flag, if 1 then cursor is on finish down right position
  42.  
  43. /*
  44. In array trace[]
  45. 1 - Up
  46. 2 - Down
  47. 3 - Left
  48. 4 - Right
  49. */
  50.  
  51.  
  52. // ----------CURSOR CONTROL FUNCTIONS------------------------------------------
  53.  
  54. // place cursor at position ( x, y ) = ( column, row )
  55. void gotoxy( int column, int row )
  56. {
  57.     COORD coord;
  58.  
  59.     coord.X = column;
  60.     coord.Y = row;
  61.  
  62.     SetConsoleCursorPosition( GetStdHandle(STD_OUTPUT_HANDLE), coord );
  63. }
  64.  
  65. // return x coordinate (column) of current cursor position
  66. // on failure return -1
  67. int wherex()
  68. {
  69.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  70.  
  71.     if (!GetConsoleScreenBufferInfo( GetStdHandle(STD_OUTPUT_HANDLE), &csbi ) )
  72.         return -1;
  73.  
  74.     return csbi.dwCursorPosition.X;
  75. }
  76.  
  77. // return y coordinate (row) of current cursor position
  78. // on failure return -1
  79. int wherey()
  80. {
  81.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  82.  
  83.     if (!GetConsoleScreenBufferInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &csbi ) )
  84.         return -1;
  85.  
  86.     return csbi.dwCursorPosition.Y;
  87. }
  88.  
  89. // END OF-----CURSOR CONTROL FUNCTIONS------------------------------------------
  90.  
  91.  
  92. // print matrix dimension x,y
  93. void print_matrix( int width, int height )
  94. {
  95.     int x, y;
  96.     char str[100];
  97.  
  98.     sprintf(str,"%c",'o');
  99.     for( y=0; y<height; y++ )
  100.         for( x=0; x<width; x++ )
  101.         {
  102.             gotoxy(x,y);
  103.             fputs(str, stdout);
  104.         }
  105.  
  106.     gotoxy(width,height);   // finish position
  107.     putchar('F');
  108.     gotoxy(0,0);            // first position
  109.     putchar('S');
  110.     gotoxy(0,0);
  111. } // print_matrix()
  112.  
  113.  
  114. // ----------FUNCTIONS TO MOVE CURSOR -----------------------------------------
  115.  
  116. // Move cursor up, print 1, trace[movements++]=1
  117. void move_up( int x, int y, int width, int heigth )
  118. {
  119.     if( y>0 && y<heigth )
  120.     {
  121.         gotoxy(x,y-1);
  122.         putchar('1');
  123.         gotoxy(x,y-1);
  124.         trace[movements++] = 1;
  125.     }
  126. }
  127.  
  128. // Move cursor down, print 1, trace[movements++]=2
  129. void move_down( int x, int y, int width, int heigth )
  130. {
  131.     if( y>=0 && y<heigth-1 )
  132.     {
  133.         gotoxy(x,y+1);
  134.         putchar('2');
  135.         gotoxy(x,y+1);
  136.         trace[movements++] = 2;
  137.     }
  138. }
  139.  
  140. // Move cursor left, print 1, trace[movements++]=3
  141. void move_left( int x, int y, int width, int heigth )
  142. {
  143.     if( x>0 && x<width )
  144.     {
  145.         gotoxy(x-1,y);
  146.         putchar('3');
  147.         gotoxy(x-1,y);
  148.         trace[movements++] = 3;
  149.     }
  150. }
  151.  
  152. // Move cursor right, print 1, trace[movements++]=4
  153. void move_right( int x, int y, int width, int heigth )
  154. {
  155.     if( x>=0 && x<width-1 )
  156.     {
  157.         gotoxy(x+1,y);
  158.         putchar('4');
  159.         gotoxy(x+1,y);
  160.         trace[movements++] = 4;
  161.     }
  162. }
  163.  
  164. // END OF----FUNCTIONS TO MOVE CURSOR -----------------------------------------
  165.  
  166.  
  167. // ----------KEYBOARD CONTROL--------------------------------------------------
  168.  
  169. /*
  170.     Keyboard control:
  171.  
  172. UP          up arrow
  173. DOWN        down arrow
  174. LEFT        left arrow
  175. RIGHT       right arrow
  176.  
  177. Key Escape, ESC (27) for quit.
  178. */
  179.  
  180. int keyboard_control( int width, int heigth )
  181. {
  182.     int choice;                                 // for pressed key
  183.  
  184.     while( 1 ) {                    // an infinite loop that exits with choice Ecsape, ESC (27)
  185.  
  186.         choice = _getch();                      // read the keyboard
  187.  
  188.         if( choice == 0 || choice == 224 )      // if is pressed function key with leading 0 or 224
  189.         {
  190.             choice = _getch();                  // let's empty that 0 or 224
  191.  
  192.             if( choice == UP )                  // if pressed up arrow
  193.             {
  194.                 move_up( wherex(), wherey() , width, heigth );
  195.             }
  196.  
  197.             if( choice == DOWN )                // if pressed down arrow
  198.             {
  199.                 move_down( wherex(), wherey() , width, heigth );
  200.             }
  201.  
  202.             if( choice == LEFT )                // if pressed left arrow
  203.             {
  204.                 move_left( wherex(), wherey() , width, heigth );
  205.             }
  206.  
  207.             if( choice == RIGHT )               // if pressed right arrow
  208.             {
  209.                 move_right( wherex(), wherey() , width, heigth );
  210.             }
  211.  
  212.         } // end of: if is pressed function key with leading 0 or 224
  213.         else    // if pressed ordinary keys
  214.         {
  215.             if( choice == ESC )                 // if is pressed ESC
  216.             {
  217.                 return( choice );               // end of work
  218.             }
  219.  
  220.         } // end of: if pressed ordinary keys
  221.  
  222.         if( (wherex()==width-1) && (wherey()==heigth-1) )
  223.             return( choice );                   // end of work, cursor is on down right position
  224.  
  225.     } // end of: while( 1 )
  226.  
  227.     return( choice );                           // have to return something
  228. }
  229.  
  230. // END OF----KEYBOARD CONTROL--------------------------------------------------
  231.  
  232.  
  233. int main(void)
  234. {
  235.     int i, width=10, height=10, left=0, right=0, up=0, down=0;
  236.  
  237.     print_matrix(width,height);
  238.  
  239.     keyboard_control(width,height);
  240.  
  241.     gotoxy( 0, height+3 );
  242.  
  243.     printf(" There was %d movements. \n\n", movements);
  244.     printf(" Trace is: ");
  245.  
  246.     for(i=0; i<movements; i++)
  247.     {
  248.         printf("%2d", trace[i]);
  249.         if(trace[i]==1)
  250.             up++;
  251.         if(trace[i]==2)
  252.             down++;
  253.         if(trace[i]==3)
  254.             left++;
  255.         if(trace[i]==4)
  256.             right++;
  257.     }
  258.  
  259.     printf("\n\n There are %2d up    (1) movements. \n", up);
  260.     printf("\n There are %2d down  (2) movements. \n", down);
  261.     printf("\n There are %2d left  (3) movements. \n", left);
  262.     printf("\n There are %2d right (4) movements. \n\n", right);
  263.  
  264.     return 0;
  265. }
Add Comment
Please, Sign In to add comment