Advertisement
Broatlas

Turtle

Feb 26th, 2016
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.41 KB | None | 0 0
  1. /* PROGRAM:  turtle_A.c
  2.  
  3.    AUTHOR:
  4.  
  5.    DATE:
  6.  
  7.    TOPIC:
  8.  
  9.    PURPOSE:  Simple Turtle Graphic
  10.  
  11.    LEVEL OF DIFFICULTY:
  12.  
  13.    CHALLENGES:
  14.  
  15.    HOURS SPENT:
  16.  
  17.    NOTES:
  18.  
  19.  
  20.  
  21.  
  22.  
  23. */
  24.  
  25. /**************************************************************************/
  26.  
  27. /* Declare include files
  28.  
  29.  **************************************************************************/
  30.  
  31. #include <stdio.h>
  32.  
  33.  
  34.  
  35. /**************************************************************************/
  36.  
  37. /* Defines
  38.  
  39.  **************************************************************************/
  40.  
  41. #define TRUE 1
  42.  
  43. #define FALSE 0
  44.  
  45. #define MAX 200
  46.  
  47. #define SIZE 40
  48.  
  49.  
  50.  
  51. #define RIGHT 0
  52.  
  53. #define DOWN  1
  54.  
  55. #define LEFT  2
  56.  
  57. #define UP    3
  58.  
  59. #define TOTAL_DIR 3
  60.  
  61.  
  62.  
  63. /**************************************************************************/
  64.  
  65. /* Function Prototypes
  66.  
  67.  **************************************************************************/
  68.  
  69. int readCommands( char c[ ] );
  70.  
  71. int execCommands( char c[ ], int n );
  72.  
  73. int turnRight( int d );
  74.  
  75. int turnLeft( int d );
  76.  
  77. void moveTurtle( int down, int a[][ SIZE ], int dir, int dist, int ver, int horz );
  78.  
  79. void printFloor( int a[][ SIZE ], char c );
  80.  
  81.  
  82.  
  83. /**************************************************************************/
  84.  
  85. /* Main`
  86.  
  87. /* Read commands from stdin or file
  88.  
  89. /* Execute the commands
  90.  
  91.  **************************************************************************/
  92.  
  93. int main( void ) {
  94.  
  95.  
  96.  
  97.     char commands[ MAX ] = { 0 };
  98.  
  99.     int ncmds;
  100.  
  101.  
  102.  
  103.     ncmds = readCommands( commands );
  104.  
  105.     int count = 0;
  106.  
  107.     for(count;count<ncmds;count++){
  108.  
  109.         fprintf(stdout,"%c\n",commands[count]);
  110.  
  111.     }
  112.  
  113.     execCommands( commands, ncmds );
  114.  
  115.  
  116.  
  117.    return 0;
  118.  
  119. }
  120.  
  121.  
  122.  
  123. /**************************************************************************
  124.  
  125.  readCommands
  126.  
  127.  Read one command at a time and store it in the commands arrays
  128.  
  129.  Reads until the EOF ( CTL-D ) or the MAX maximun number of commands is
  130.  
  131.  reached
  132.  
  133.  
  134.  
  135.  ARGUMENTS:
  136.  
  137.     commands:  An empty array of chars to store the commands read
  138.  
  139.                commands is modified in this function
  140.  
  141.  
  142.  
  143.  RETURN:
  144.  
  145.      number of commands read
  146.  
  147. **************************************************************************/
  148.  
  149. int readCommands( char commands[] ) {
  150.  
  151.     int ncmds = 0;
  152.  
  153.     char val;
  154.  
  155.  
  156.  
  157.     while(fscanf(stdin,"%c\n",&val)!= EOF){
  158.  
  159.         commands[ncmds] = val;
  160.  
  161.         ++ncmds;
  162.  
  163.     }
  164.  
  165.  
  166.  
  167.     return ncmds;
  168.  
  169. }
  170.  
  171. /**************************************************************************
  172.  
  173.  execCommands
  174.  
  175.  execute one command at a time
  176.  
  177.  
  178.  
  179.  ARGUMENTS:
  180.  
  181.     commands:  an array containing valid turtle commands
  182.  
  183.     ncmds   :  number of commands stored in commands
  184.  
  185.               0 <= ncmds < MAX
  186.  
  187.  **************************************************************************/
  188.  
  189.  int execCommands( char commands[], int ncmds ) {
  190.  
  191.   int ver = 0;
  192.  
  193.   int horz = 0;
  194.  
  195.  
  196.  
  197.   int i = 0;
  198.  
  199.     char cmd;
  200.  
  201.     int dir = 0;
  202.  
  203.     int floor[ SIZE ][ SIZE ] = { { 0 } };
  204.  
  205.     int penDown = FALSE;
  206.  
  207.  
  208.  
  209.     cmd = commands[ i ];
  210.  
  211.  
  212.  
  213.     while ( ncmds-- ) {
  214.  
  215.             switch ( cmd ) {
  216.  
  217.                 case 'U':
  218.  
  219.                     penDown = FALSE;
  220.  
  221.                     break;
  222.  
  223.                     case 'D':
  224.  
  225.                         penDown = TRUE;
  226.  
  227.                         break;
  228.  
  229.                 case 'R':
  230.  
  231.                     dir = turnRight( dir );
  232.  
  233.                         break;
  234.  
  235.                 case 'L':
  236.  
  237.                     dir = turnLeft( dir );
  238.  
  239.                         break;
  240.  
  241.                 case 'F':
  242.  
  243.             if(dir == UP){
  244.  
  245.               ver--;
  246.  
  247.             }
  248.  
  249.             if(dir == DOWN){
  250.  
  251.               ver++;
  252.  
  253.             }
  254.  
  255.             if(dir == RIGHT){
  256.  
  257.               horz++;
  258.  
  259.             }
  260.  
  261.             if(dir == LEFT){
  262.  
  263.               horz--;
  264.  
  265.             }
  266.  
  267.                 moveTurtle( penDown, floor, dir, 1, ver, horz );
  268.  
  269.                         break;
  270.  
  271.                 case 'P':
  272.  
  273.                     printFloor( floor, '*' );
  274.  
  275.                         break;
  276.  
  277.                     default:
  278.  
  279.                         break;
  280.  
  281.             }
  282.  
  283.             cmd = commands[ i++ ];
  284.  
  285.     }
  286.  
  287.  
  288.  
  289.     return 0;
  290.  
  291.  
  292.  
  293.  }
  294.  
  295. /**************************************************************************
  296.  
  297.  turnRight
  298.  
  299.  Turn the turtle direction to the right ( 90 degres counterclock wise )
  300.  
  301.  Think about 4 directions:
  302.  
  303.  >  ( RIGHT )
  304.  
  305.   \/ ( DOWN )
  306.  
  307.   <  ( LEFT }
  308.  
  309.   ^  ( UP )
  310.  
  311.  **************************************************************************/
  312.  
  313. int turnRight( int d ) {
  314.  
  315.  
  316.  
  317.   if (d == DOWN){
  318.  
  319.     d = RIGHT;
  320.  
  321.   }
  322.  
  323.   if( d == RIGHT){
  324.  
  325.     d = UP;
  326.  
  327.   }
  328.  
  329.   if( d == UP ){
  330.  
  331.     d = LEFT;
  332.  
  333.   }
  334.  
  335.   if ( d == LEFT){
  336.  
  337.     d = DOWN;
  338.  
  339.   }
  340.  
  341.   else {
  342.  
  343.     printf("Out of Range!");
  344.  
  345.   }
  346.  
  347.   return d;
  348.  
  349.  
  350.  
  351. }
  352.  
  353.  
  354.  
  355.  
  356.  
  357. /**************************************************************************
  358.  
  359.  turnRight
  360.  
  361.  Turn the turtle direction to the left ( -90 degres counterclock wise )
  362.  
  363.  Think about 4 directions:
  364.  
  365.   >  ( RIGHT )
  366.  
  367.   \/ ( DOWN )
  368.  
  369.   <  ( LEFT }
  370.  
  371.   ^  ( UP )
  372.  
  373.  *************************************************************************/
  374.  
  375. int turnLeft( int d ) {
  376.  
  377.     if( d == DOWN){
  378.  
  379.       d = LEFT;
  380.  
  381.     }
  382.  
  383.     if( d == LEFT){
  384.  
  385.       d = UP;
  386.  
  387.     }
  388.  
  389.     if( d == UP){
  390.  
  391.       d = RIGHT;
  392.  
  393.     }
  394.  
  395.     if(d == RIGHT){
  396.  
  397.       d = DOWN;
  398.  
  399.     }
  400.  
  401.     else {
  402.  
  403.       printf("Out of Range" );
  404.  
  405.     }
  406.  
  407.     return d;
  408.  
  409.  
  410.  
  411. }
  412.  
  413.  
  414.  
  415. /**************************************************************************
  416.  
  417.  movePen
  418.  
  419.  move the turtle a distance dis in the direction dir from current pos
  420.  
  421.  if the pen is
  422.  
  423.  down, place a 1 in floor position
  424.  
  425.  ARGUMENTS:
  426.  
  427.     a   :  floor
  428.  
  429.     down:  1
  430.  
  431.     if pen is down    dir :  direction turtle is facing
  432.  
  433.     dist:  How far to walk
  434.  
  435.  *************************************************************************/
  436.  
  437. void moveTurtle( int down, int a[][ SIZE ], int dir, int dist, int ver, int horz ) {
  438.  
  439.  
  440.  
  441. if ( down == TRUE){
  442.  
  443.   a[ver][horz] = dist;
  444.  
  445. }
  446.  
  447.  
  448.  
  449.     return;
  450.  
  451. }
  452.  
  453.  
  454.  
  455. /*************************************************************************
  456.  
  457.  printFloor
  458.  
  459.  Print the floor
  460.  
  461.  ARGUMENTS:
  462.  
  463.     a   :  floor
  464.  
  465.     c   :  char to use to print the floor
  466.  
  467.  *************************************************************************/
  468.  
  469. void printFloor( int a[][ SIZE ], char c ){
  470.  
  471.     int j = 0;
  472.  
  473.     int i = 0;
  474.  
  475.     for (i;i<SIZE;i++){
  476.  
  477.  
  478.  
  479.       for(j;j<SIZE;j++){
  480.  
  481.         if(a[i][j] != 0){
  482.  
  483.           printf("%c\n", c);
  484.  
  485.         }
  486.  
  487.       }
  488.  
  489.     }
  490.  
  491.  
  492.  
  493.  
  494.  
  495.     return;
  496.  
  497. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement