Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* PROGRAM: turtle_A.c
- AUTHOR:
- DATE:
- TOPIC:
- PURPOSE: Simple Turtle Graphic
- LEVEL OF DIFFICULTY:
- CHALLENGES:
- HOURS SPENT:
- NOTES:
- */
- /**************************************************************************/
- /* Declare include files
- **************************************************************************/
- #include <stdio.h>
- /**************************************************************************/
- /* Defines
- **************************************************************************/
- #define TRUE 1
- #define FALSE 0
- #define MAX 200
- #define SIZE 40
- #define RIGHT 0
- #define DOWN 1
- #define LEFT 2
- #define UP 3
- #define TOTAL_DIR 3
- /**************************************************************************/
- /* Function Prototypes
- **************************************************************************/
- int readCommands( char c[ ] );
- int execCommands( char c[ ], int n );
- int turnRight( int d );
- int turnLeft( int d );
- void moveTurtle( int down, int a[][ SIZE ], int dir, int dist, int ver, int horz );
- void printFloor( int a[][ SIZE ], char c );
- /**************************************************************************/
- /* Main`
- /* Read commands from stdin or file
- /* Execute the commands
- **************************************************************************/
- int main( void ) {
- char commands[ MAX ] = { 0 };
- int ncmds;
- ncmds = readCommands( commands );
- int count = 0;
- for(count;count<ncmds;count++){
- fprintf(stdout,"%c\n",commands[count]);
- }
- execCommands( commands, ncmds );
- return 0;
- }
- /**************************************************************************
- readCommands
- Read one command at a time and store it in the commands arrays
- Reads until the EOF ( CTL-D ) or the MAX maximun number of commands is
- reached
- ARGUMENTS:
- commands: An empty array of chars to store the commands read
- commands is modified in this function
- RETURN:
- number of commands read
- **************************************************************************/
- int readCommands( char commands[] ) {
- int ncmds = 0;
- char val;
- while(fscanf(stdin,"%c\n",&val)!= EOF){
- commands[ncmds] = val;
- ++ncmds;
- }
- return ncmds;
- }
- /**************************************************************************
- execCommands
- execute one command at a time
- ARGUMENTS:
- commands: an array containing valid turtle commands
- ncmds : number of commands stored in commands
- 0 <= ncmds < MAX
- **************************************************************************/
- int execCommands( char commands[], int ncmds ) {
- int ver = 0;
- int horz = 0;
- int i = 0;
- char cmd;
- int dir = 0;
- int floor[ SIZE ][ SIZE ] = { { 0 } };
- int penDown = FALSE;
- cmd = commands[ i ];
- while ( ncmds-- ) {
- switch ( cmd ) {
- case 'U':
- penDown = FALSE;
- break;
- case 'D':
- penDown = TRUE;
- break;
- case 'R':
- dir = turnRight( dir );
- break;
- case 'L':
- dir = turnLeft( dir );
- break;
- case 'F':
- if(dir == UP){
- ver--;
- }
- if(dir == DOWN){
- ver++;
- }
- if(dir == RIGHT){
- horz++;
- }
- if(dir == LEFT){
- horz--;
- }
- moveTurtle( penDown, floor, dir, 1, ver, horz );
- break;
- case 'P':
- printFloor( floor, '*' );
- break;
- default:
- break;
- }
- cmd = commands[ i++ ];
- }
- return 0;
- }
- /**************************************************************************
- turnRight
- Turn the turtle direction to the right ( 90 degres counterclock wise )
- Think about 4 directions:
- > ( RIGHT )
- \/ ( DOWN )
- < ( LEFT }
- ^ ( UP )
- **************************************************************************/
- int turnRight( int d ) {
- if (d == DOWN){
- d = RIGHT;
- }
- if( d == RIGHT){
- d = UP;
- }
- if( d == UP ){
- d = LEFT;
- }
- if ( d == LEFT){
- d = DOWN;
- }
- else {
- printf("Out of Range!");
- }
- return d;
- }
- /**************************************************************************
- turnRight
- Turn the turtle direction to the left ( -90 degres counterclock wise )
- Think about 4 directions:
- > ( RIGHT )
- \/ ( DOWN )
- < ( LEFT }
- ^ ( UP )
- *************************************************************************/
- int turnLeft( int d ) {
- if( d == DOWN){
- d = LEFT;
- }
- if( d == LEFT){
- d = UP;
- }
- if( d == UP){
- d = RIGHT;
- }
- if(d == RIGHT){
- d = DOWN;
- }
- else {
- printf("Out of Range" );
- }
- return d;
- }
- /**************************************************************************
- movePen
- move the turtle a distance dis in the direction dir from current pos
- if the pen is
- down, place a 1 in floor position
- ARGUMENTS:
- a : floor
- down: 1
- if pen is down dir : direction turtle is facing
- dist: How far to walk
- *************************************************************************/
- void moveTurtle( int down, int a[][ SIZE ], int dir, int dist, int ver, int horz ) {
- if ( down == TRUE){
- a[ver][horz] = dist;
- }
- return;
- }
- /*************************************************************************
- printFloor
- Print the floor
- ARGUMENTS:
- a : floor
- c : char to use to print the floor
- *************************************************************************/
- void printFloor( int a[][ SIZE ], char c ){
- int j = 0;
- int i = 0;
- for (i;i<SIZE;i++){
- for(j;j<SIZE;j++){
- if(a[i][j] != 0){
- printf("%c\n", c);
- }
- }
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement