-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathturtle.c
More file actions
358 lines (320 loc) · 12.2 KB
/
Copy pathturtle.c
File metadata and controls
358 lines (320 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
WeChat: cstutorcs
QQ: 749389476
Email: tutorcs@163.com
/**
* CSC A48 - Intro to Computer Science II, Summer 2020
*
* 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 2020
*/
#include "imgUtils.c"
// This lets the driver code override the image size if it needs to. Make sure
// you don't hard-code these values anywhere!
#ifndef SIZEX
#define SIZEX 512
#define SIZEY 512
#endif
/*---------------------------------------------------------------------------*/
/**
* This struct contains one node of the linked list, which represents a single
* command to the Turtle. It's field should include:
*
* - cmd : A char array of size 10 holding the command name
*
* - val : 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 {
} CmdNode;
/*---------------------------------------------------------------------------*/
CmdNode *newCmdNode(char cmd[10], int val) {
/**
* This function allocates a new CmdNode 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
*/
return NULL;
}
/*---------------------------------------------------------------------------*/
void printCommandList(CmdNode *head) {
/**
* 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
*/
return;
}
/*---------------------------------------------------------------------------*/
void queryByCommand(CmdNode *head, char cmd[10]) {
/**
* 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
*/
return;
}
/*---------------------------------------------------------------------------*/
int countCommands(CmdNode *head) {
/**
* This function counts and returns the length of the linked list. It
* requires list traversal.
*
* TODO: Implement this function
*/
return 0;
}
/*---------------------------------------------------------------------------*/
CmdNode *insertCommand(CmdNode *head, CmdNode *new_CmdNode) {
/**
* This function inserts the node new_CmdNode *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
*/
return NULL;
}
/*---------------------------------------------------------------------------*/
CmdNode *insertCommandBefore(CmdNode *head, CmdNode *new_CmdNode, int cmdNum) {
/**
* 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 new_CmdNode
* *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
*/
return NULL;
}
/*---------------------------------------------------------------------------*/
void updateCommand(CmdNode *head, int cmdNum, char cmd[10], int val) {
/**
* 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
*/
return;
}
/*---------------------------------------------------------------------------*/
CmdNode *deleteCommand(CmdNode *head, int cmdNum) {
/**
* 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
*/
return NULL;
}
/*---------------------------------------------------------------------------*/
CmdNode *deleteCommandList(CmdNode *head) {
/**
* 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
*/
return NULL;
}
/*---------------------------------------------------------------------------*/
void run(Image *im, CmdNode *head, int *endX, int *endY) {
/**
* 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: In the image we work with, the top-left pixel is (0,0),
* - x increases as you go right, up till SIZEX-1
* - y increases as you go down, up till SIZEY-1
*
* (0,0) (SIZEX-1, 0)
* x------------------------x
* | |
* | |
* | |
* | |
* | IMAGE |
* | |
* | |
* | |
* | |
* | |
* x------------------------x
* (0, SIZEY-1) (SIZEX-1, SIZEY-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 <val> : Draw lines in colour <val> from now on
* - forward <val> : Move the turtle forward <val> steps (pixels)
* in the direction it is facing.
* - backward <val> : Same as above, except move backwards
* - 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 SIZEX-1 / SIZEY-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 (SIZEX-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 < SIZEX, 0 <= y < SIZEY ]
*
*
* TODO: Implement this function
*/
return;
}
/*---------------------------------------------------------------------------*/
// All done!