Hello,
First post to the board, though I've lurked for a while after becoming interested in programming. Started with C, and I am going through some exercises in a textbook I have. The program I am working on will take user input in a basic form, and convert it to a simple graphics display based on the input.
My problems are twofold:
1) I cannot figure out how to make the second argument of the "move forward" command take more than a single digit number
2) The program produces unpredicted output, and though I've modified the code over and over, I cannot figure out exactly where the problem lies. When feeding it slightly different input, sometimes it will segfault. Sometimes it will print absolutely nothing. And sometimes it will print the asterisks, but not properly.
So I call on the expertise of these boards to help me to through it. I have not learned much (if anything) beyond arrays, so my knowledge is very limited. Also, I'm sure my code is much longer than it should be, but I wanted to get the program fully functional first. Any code-related tips are appreciated also.
Code:
/************************************************************
Turtle Graphics: turtle.c
Use 50x50 array initialized to zeros to simulate a floor.
Turtle starts at 0,0 with pen up. When pen is down, set
array elements to 1. Display asterisk wherever there is
a 1, when program is complete.
Command list:
1 Pen up
2 Pen down
3 Turn right
4 Turn left
5,10 Move forward 10 spaces
6 Print the array
9 End of data (sentinel)
************************************************************/
#include <stdio.h>
#define ARRAYMAX 50 /* grid length and width */
#define COMMANDMAX 20 /* max amount of prog commands */
#define BUFMAX 10 /* max length of a command */
enum Direction {NORTH, EAST, SOUTH, WEST};
enum Position {UP, DOWN};
int readProg(int prog[][2], int max); /* read the program */
void doCommand(int command1, int command2, int floor[][ARRAYMAX]);
/* process command to the array */
enum Direction dir = NORTH; /* direction turtle is facing */
enum Position pos = UP; /* position of the pen */
int turtlePos[2] = {0}; /* position of the turtle */
int main(void)
{
int floor[ARRAYMAX][ARRAYMAX] = {{0}, {0}}; /* grid to print */
int prog[COMMANDMAX][2] = {{0}, {0}}; /* program commands */
int commands = 0; /* number of commands in program */
int i = 0;
commands = readProg(prog, COMMANDMAX);
while (i <= commands) { /* process program */
doCommand(prog[i][0], prog[i][1], floor);
i++;
}
return 0;
}
int readProg(int prog[][2], int max)
{
int getCommand(char buffer[], int lim); /* get one command */
void clearBuf(char buffer[], int lim); /* clear temp buffer */
char buffer[BUFMAX];
int i = 0;
while (i < max && getCommand(buffer, BUFMAX)) { /* process until sentinel */
prog[i][0] = buffer[0];
if (buffer[0] == '5') /* if command 5, get the argument also */
prog[i][1] = buffer[2];
i++;
clearBuf(buffer, BUFMAX);
}
return i; /* the number of commands, minus sentinel */
}
int getCommand(char buffer[], int lim)
{
int c, i;
for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; i++)
buffer[i] = c;
if (buffer[0] == '9')
return 0;
else
return 1;
}
void clearBuf(char buffer[], int lim)
{
int i;
for (i = 0; i < lim; i++)
buffer[i] = 0;
}
void doCommand(int command1, int command2, int floor[][ARRAYMAX])
{
void printFloor(int floor[][ARRAYMAX]); /* print the finished grid */
int i;
switch(command1) {
case '1': /* put pen up */
pos = UP;
break;
case '2': /* put pen down */
pos = DOWN;
floor[turtlePos[0]][turtlePos[1]] = 1; /* activate current position */
break;
case '3': /* turn right */
if (dir == WEST)
dir = NORTH;
else
dir += 1;
break;
case '4': /* turn left */
if (dir == NORTH)
dir = WEST;
else
dir -= 1;
break;
case '5': /* move forward amount of spaces in command2 */
if (dir == NORTH)
for (i = 1; i <= command2; i++) {
turtlePos[1]++; /* go up on y-axis */
if (pos == DOWN) /* if pen is down, activate each passing spot */
floor[turtlePos[0]][turtlePos[1]] = 1;
}
else if (dir == EAST) /* go right on x-axis */
for (i = 1; i <= command2; i++) {
turtlePos[0]++;
if (pos == DOWN)
floor[turtlePos[0]][turtlePos[1]] = 1;
}
else if (dir == SOUTH) /* go down on y-axis */
for (i = 1; i <= command2; i++) {
turtlePos[1]--;
if (pos == DOWN)
floor[turtlePos[0]][turtlePos[1]] = 1;
}
else if (dir == WEST) /* go left on x-axis */
for (i = 1; i <= command2; i++) {
turtlePos[0]--;
if (pos == DOWN)
floor[turtlePos[0]][turtlePos[1]] = 1;
}
break;
case '6':
printFloor(floor); /* print the grid */
break;
default:
break;
}
}
void printFloor(int floor[][ARRAYMAX])
{ /* position 0,0 will be lower left corner */
int i, j;
printf("\n");
for (i = ARRAYMAX - 1; i >= 0; i--) { /* in each row... */
for (j = ARRAYMAX - 1; j >= 0; j--) { /* for each column... */
if (floor[j][i] == 1) /* ...print '*' if activated... */
printf("*");
else /* ...and a blank if not */
printf(" ");
}
printf("\n"); /* after the last column, print a newline */
}
}
And the output:
Code:
jason@lappy:~/c$ ./a.out
2
5,5
3
5,5
3
5,5
3
5,5
1
6
9
Segmentation fault
jason@lappy:~/c$ ./a.out
2
5,7
3
5,7
3
5,7
3
5,7
1
6
9
jason@lappy:~/c$
Thanks for any help.