Hi all, new to the forum. I'm pretty desperate at the moment trying to get my &%$@^@$ C program to work. In my C class we are supposed to develop a "game" absed on Turtle Graphics. The program must have two arrays. One single dim array that stores the users commands and a second double dim array that will be used to track the turtles movements. So far I have programmed the user input along with error checking the input. I have also written the printing procedure that will run through the double dimension array and print out "*" that indicate where the turtle moved. I initailized both arrays to zero and what I am trying to do is for "pen down" commands and movements I would like to store a "1" in the double dim array. For "pen up" movements I will keep the array element contents to zero. That way when the printing procedure executes it will only print "*" for the element contents that contain a "1". THE MILLION DOLLAR QUESTION IS: how do I link the commands in the single dim array to the movements stored in the double dim array? I have attempted/started to right some code for this but am completely lost. Could someone take a look at my code and suggest a simple way to accomplish this? Thanks a bunch!
Code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 100 //size of command array
#define ROW 20 //size of row in floor array
#define COL 20 //size of col in floor array
/* FUNCTION PROTOTYPES */
void initFArray(int[ROW][COL]); //function to initialize Floor array
void initCArray(int[SIZE]); //function to initialize Command array
void inputCommands(int[SIZE]); //function that records input commands into array
void readCArray(int[SIZE]); //function reads command array and changes floor array accordingly
void printArray(int [ROW][COL]); //function to print out turtle movement
void printHeader(); //function that prints out what the commands are
int turnRight(int);
int turnLeft(int);
void turtleMove(int, int, int[ROW][COL]);
void readCArray(int arrayCommands[SIZE])
{//commands 1,2,3,4,5,6,9
int x;
int command = 0;
bool penUp = true;
bool penDown = false;
while(command !=9)
{
int x=0;
command = arrayCommands[x];
if(command == 1)
{
penUp=true;
}
if(command == 2)
{
penDown=true;
}
if(command == 3)
{
}
if(command == 4)
{
}
if(command == 5)
{
}
if(command == 6)
{
}
x++;
}
int main()
{
int arrayFloor[ROW][COL]; //init 20x20 floor array
int arrayCommands[SIZE]; //init Command array to 100 elements
initFArray(arrayFloor); //call to initFloor to zero out arrayFloor
initCArray(arrayCommands); //call to initCArray to zero out arrayCommands
printHeader(); //call to printHeader to print out command header
inputCommands(arrayCommands); //call to inputCommands to record commands into arrayCommands
readCArray(arrayCommands); //call to readCArray to read values from arrayCommands to Floor Array
//printArray(arrayFloor);
system("PAUSE");
return 0;
}
void printArray(int arrayFloor [ROW][COL]) //prints out blanks or * based on arrayFloor value
{
int x;
int y;
int counter=1;
for(x=0;x<ROW;x++)
{
for(y=0;y<COL;y++)
{
if(arrayFloor[x][y]==1)
{
printf("*");
}
else
{
printf(" ");
}
if(counter==COL)
{
printf("\n");
counter=0;
}
counter++;
}
}
}
void printHeader() //prints out command header
{
printf("Command\t\tMeaning\n\n");
printf("1\t\tPen Up\n");
printf("2\t\tPen Down\n");
printf("3\t\tTurn Right\n");
printf("4\t\tTurn Left\n");
printf("5,X\t\tMove Forward X Spaces\n");
printf("6\t\tPrint the 20x20 array\n");
printf("9\t\tEnd of data(sentinel)\n\n\n");
}
void initCArray(int arrayCommands[SIZE]) //initializes 100 element arrayCommands array to zero
{
int x;
for(x=0;x<SIZE;x++)
{
arrayCommands[x]=0;
}
}
void initFArray(int arrayFloor[ROW][COL]) //initializes 20x20 arrayFloor array to zero
{
int x;
int y;
for(x=0;x<ROW;x++)
{
for(y=0;y<COL;y++)
{
arrayFloor[x][y]=0;
}
}
}
void inputCommands(int arrayCommands [SIZE]) //inputs commands from user into arrayCommands array
{ //and validates input commands
int index=0;
int command=0;
int counter=1;
int distance=-1;
do
{
printf("Command %i: ",counter);
scanf("%i,%i",&command, &distance);
//commands 1,2,3,4,5,6,9
if((command > 0 && command < 5) || (command == 6) || (command == 9))
{
arrayCommands[index]=command;
counter++;
index ++;
}
else
{
if ((command == 5) && (distance >= 0))
{
arrayCommands[index]=command;
index=index+1;
arrayCommands[index]=distance;
distance = -1;
counter++;
index++;
}
else
{
if ((command == 5) && (distance < 0))
{
printf("\tYou have not entered a distance (eg 5,10) please try again\n");
}
else
{
printf("\tCommand %i is invalid, please re-enter\n",counter);
}
}
}
}
while((command !=9) && (index < SIZE-1));
}