/**
* CSC A48 – Intro to Computer Science II, Summer 2021
*
* This is the program file where you will implement your solution for
* assignment 1. Please make sure you read through this file carefully
* and that you understand what is provided and what you need to complete.
*
* You will need to have read the handout carefully. Parts where you have to
* implement functionality are clearly labeled TODO.
*
* Be sure to test your work thoroughly, our testing will be extensive and will
* check that your solution is *correct*, not only that it provides
* functionality.
*
* Developed by Mustafa Quraish for CSCA48
* (c) Mustafa Quraish 2021
*/
#include “imgutils.c”
//—————————————————————————–
/**
* This struct contains one node of the linked list, which represents a single
* command to the Turtle. It’s field should include:
*
* – command : A char array of size 10 holding the command name
*
* – value : An integer that stores a parameter for the command (like forward,
* backward and colour).
*
* – next : A pointer to a struct of the same type, this is used for the
* linked list
*
* TODO: Complete this struct definition
*/
typedef struct cmdnode {
char command[10];
int value;
struct cmdnode* next;
} CommandNode;
//—————————————————————————–
/**
* This function allocates a new CommandNode struct and initializes it’s values
* based on the input paramaters given. The next pointer is always
* initialized to NULL.
*
* If the ‘cmd’ parameter is not a correct command, then print
* “Invalid command.\n” and return NULL.
*
* Note that we will always pass in a value here, even if the
* command doesn’t need one. In this case, we can just ignore
* it. It saves us from having to make separate functions to
* deal with this.
*
* TODO: Implement this function
*/
CommandNode *newCommandNode(char cmd[10], int val) {
if((strcmp(cmd, “penup”) == 0) || (strcmp(cmd, “pendown”) == 0) || (strcmp(cmd, “colour”) == 0) ||
(strcmp(cmd, “forward”) == 0) || (strcmp(cmd, “backward”) == 0) || (strcmp(cmd, “left”) == 0) ||
(strcmp(cmd, “right”) == 0))
{
CommandNode *new_cmd_node = (CommandNode *) calloc(1, sizeof(CommandNode));
if(new_cmd_node == NULL)
{
printf(“\n ERROR”)
}
else
{
strcpy(new_cmd_node -> cmd, cmd);
new_cmd_node -> val = val;
new_cmd_node -> next = head;
return new_cmd_node;
}
}
else
{
printf(“\n Invaild Command”);
return NULL;
}
}
//—————————————————————————–
/**
* This function prints out each command in the linked list one after the
* other. Each command MUST also have a line number printed before it, this
* is what you will be using to modify / delete them. To do this, initialize
* a counter and then increment it for each command.
*
* Depending on whether or not the command needs an additional value
* (like forward, backward and colour), use one of the following statements
* to print out the information for each node:
* [ The format is “linenumber: command value” ]
*
* printf(“%3d: %s %d\n”, … ); [With a value]
*
* printf(“%3d: %s\n”, … ); [Without a value]
*
* Obviously, you also need to pass in the correct parameters to the print
* function yourself, this is just so you have the correct format.
*
* TODO: Implement this function
*/
void printCommandList(CommandNode *head) {
CommandNode *newtemp = head;
int count = 0;
while(newtemp != NULL)
{
if(strcmp(newtemp -> cmd, “penup”) == 0 || strcmp(newtemp ->cmd,”pendown”) == 0 || strcmp(newtemp -> cmd,”left”) == 0) ||strcmp(newtemp -> cmd,”right”) == 0)
{
printf(%d: %s\n, count , newtemp -> cmd);
}
else
{
printf(%d: %s %d\n, count, newtemp -> cmd, newtemp -> val);
}
newtemp = newtemp -> next;
count++;
}
}
//—————————————————————————–
/**
* This function looks for commands in the linked list that match the input
* query. It prints them out in the same format as the printCommandList()
* function.
*
* Make sure that the line number of the commands that match is the same as
* the line number that would be printed by the printCommandList() function.
*
* ————————————————————————–
*
* For instance, if your printCommandList function outputs
*
* 0: penup
* 1: forward 200
* 2: right
* 3: forward 50
*
* Then, if this function is called with the same list and cmd = “forward”,
* then your output here should be
*
* 1: forward 200
* 3: forward 50
*
* TODO: Implement this function
*/
void queryCommand(CommandNode *head, char cmd[10]) {
int find = -1;
CommandNode *tmp = head;
int count = 0;
while(tmp != NULL)
{
if(strcmp(temp -> cmd, cmd) == 0)
{
}
}
return;
}
//—————————————————————————–
/**
* This function counts and returns the length of the linked list. It
* requires list traversal.
*
* TODO: Implement this function
*/
int countCommands(CommandNode *head) {
return 0;
}
//—————————————————————————–
/**
* This function inserts the node node *at the tail* of the linked
* list. (You are adding a command at the end).
*
* If head == NULL, then the linked list is still empty.
*
* It returns a pointer to the head of the linked list with the new node
* added into it.
*
* TODO: Implement this function
*/
CommandNode *insertCommand(CommandNode *head, CommandNode *node) {
return NULL;
}
/*—————————————- ———————————–*/
/**
* This function inserts a new node *before* a given Node in the linked list.
*
* ‘cmdNum’ is an integer that corresponds to the line number of a command
* from the printCommandList() function. Your task is to insert `node`
* *before* the corresponding node in the linked list.
*
* ————————————————————————–
*
* For instance, if your initial list was
*
* 0: penup
* 1: forward 200
* 2: right
* 3: forward 50
*
* And you added “pendown” before cmdNum = 2, then you will have
*
* 0: penup
* 1: forward 200
* 2: pendown
* 3: right
* 4: forward 50
*
* ————————————————————————–
*
* If there is no command with the given cmdNum (cmdNum >= list size),
* then print “Invalid Command Number.\n” to the screen and *do not*
* insert the new node.
*
* Returns a pointer to the head of the linked list with the new node added
* into it.
*
* TODO: Implement this function
*/
CommandNode *insertCommandBefore(CommandNode *head, CommandNode *node, int cmdNum) {
return NULL;
}
//—————————————————————————–
/**
* This function updates a specific node in the linked list based on the
* input parameters.
*
* ‘cmdNum’ is an integer that corresponds to the line number of a command
* from the printCommandList() function. Your task is to update the ‘cmd’ and
* ‘val’ fields of this node.
*
* If there is no command with the given cmdNum, then print
* “Invalid Command Number.\n” to the screen, and if ‘cmd’ is not a correct
* command, then print “Invalid command.\n”. In both these cases, do *not*
* modify the list.
*
* TODO: Implement this function
*/
void updateCommand(CommandNode *head, int cmdNum, char cmd[10], int val) {
return;
}
//—————————————————————————–
/**
* This function deletes the node from the linked list that corresponds to
* the line number cmdNum. If there is no command with the given cmdNum, then
* the function does nothing.
*
* Returns a pointer to the head of the linked list (which may have changed
* as a result of the deletion)
*
* TODO: Implement this function
*/
CommandNode *deleteCommand(CommandNode *head, int cmdNum) {
return NULL;
}
//—————————————————————————–
/**
* This function deletes the linked list of commands, releasing all the
* memory allocated to the nodes in the linked list.
*
* Returns a NULL pointer so that the head of the list can be set to NULL
* after deletion.
*
* TODO: Implement this function
*/
CommandNode *deleteCommandList(CommandNode *head) {
return NULL;
}
//—————————————————————————–
/**
* This function runs the list of commands to move the turtle around and draw
* the image, and returns the final position of the turtle in the variables
* endX and endY.
*
* —————————————————————————-
*
* NOTE: The image struct stores 2 fields, `sx` and `sy`, which are the X and Y
* dimensions of the image.
*
* NOTE: In the image we work with, the top-left pixel is (0,0),
* – x increases as you go right, up till im->sx-1
* – y increases as you go down, up till im->sy-1
*
* (0,0) (im->sx-1, 0)
* x————————x
* | |
* | |
* | |
* | |
* | IMAGE |
* | |
* | |
* | |
* | |
* | |
* x————————x
* (0, im->sy-1) (im->sx-1, im->sy-1)
*
* The image is in grayscale (black and white), so the colours are numbers
* from [0-255] where 0 is black, 255 is white, and the values in between
* are varying levels of gray.
*
* You need to understand how the (x,y) values are stored so you know how
* they should be updated when you move (down means y increases, etc). You do
* not need to use the ‘im’ variable for anything other than passing it into
* the drawLine() function described below.
*
* ————————————————————————–
*
* Here’s the setup – There is a turtle (with a pen) that walks along the
* image. When the pen is down (on the paper), it draws a line along the path
* that it walks on. (If you want to play around with this, consider looking
* at the `turtle` library in python!)
*
* The turtle initially starts at pixel (0, 0) [at the top left],
* facing right (in the positive x direction). The pen starts off
* as `down`, with the default colour being black (0).
*
* You need to go through the linked list and ‘run’ the commands to keep
* track of the turtles position, and draw the appropriate lines. Here is
* what each command should do:
*
* – penup : Put the pen up (stop drawing)
* – pendown : Put the pen down (start / continue drawing)
* – colour
* – forward
* in the direction it is facing.
* – backward
* – right : Turn the turtle to the right by 90 degrees
* – left : Turn the turtle to the left by 90 degrees
*
* NOTE: Make sure that you do *not* go outside the image. For this, set the
* maximum and minimum values of x and y to be 0 and im->sx-1 / im->sy-1
* respectively.
*
* For instance, if the turtle is at (0,0) facing right, and your command is
* `forward 100000`, it will only go forward till (im->sx-1, 0), and end
* up at the rightmost pixel in that row.
*
* IMPORTANT: Once you are done with all the commands, make sure you save the
* final (x,y) location of the turtle into the variables endX and endY.
*
* ————————————————————————–
*
* We have already implemented a drawLine() function (in imgutils.c) which
* you should use to actually draw the lines. It takes in the following:
*
* – a pointer to an image struct (use ‘im’)
* – (x,y) location of start point
* – (x,y) location of end point
* – a colour to draw the line in [0-255]
*
* Note that this function only draws horizontal and vertical lines, so
* either the x values or the y values of both points must be the same.
* Both these points *must* also be within the image.
* [ 0 <= x < im->sx, 0 <= y < im->sy ]
*
*
* TODO: Implement this function
*/
void run(Image *im, CommandNode *head, int *endX, int *endY) {
return;
}
//—————————————————————————–
// All done!