-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.cpp
More file actions
341 lines (320 loc) · 12 KB
/
Copy pathplot.cpp
File metadata and controls
341 lines (320 loc) · 12 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
//
// plot.cpp
// plot
// Created by Sam Alsup on 10/21/19.
// Copyright © 2019 Sam Alsup. All rights reserved.
// Name : Samuel Alsup
// UID : 805371633
//
#include "grid.h"
#include <iostream>
#include <cctype>
#include <string>
using namespace std;
const int HORIZ = 0;
const int VERT = 1;
const int FG = 0;
const int BG = 1;
const int LEFT = 0;
const int RIGHT = 1;
const int DOWN = 2;
const int UP = 3;
const int HMAX = 30; // horizontal maximum of the grid
const int HMIN = 0; // horizontal minimum of the grid
const int VMAX = 20; // vertical maximum of the grid
const int VMIN = 0; // vertical minimum of the grid
bool plotLine(int r, int c, int distance, int dir, char plotChar, int fgbg);
bool plotHorizontalLine(int r, int c, int distance, char ch, int fgbg);
bool plotVerticalLine(int r, int c, int distance, char ch, int fgbg);
int performCommands(string commandString, char& plotChar, int& mode, int& badPos);
int main()
{
setSize(20, 30);
char currentChar = '*';
int currentMode = FG;
for (;;)
{
currentChar = '*';
currentMode = FG;
cout << "Enter a command string: ";
string cmd;
getline(cin, cmd);
if (cmd == "")
break;
int position;
int status = performCommands(cmd, currentChar, currentMode, position);
switch (status)
{
case 0:
draw();
break;
case 1:
cout << "Syntax error at position " << position+1 << endl;
break;
case 2:
cout << "Cannot perform command at position " << position+1 << endl;
break;
default:
// It should be impossible to get here.
cerr << "performCommands returned " << status << "!" << endl;
}
}
}
bool plotHorizontalLine(int r, int c, int distance, char ch, int fgbg) // tries to plot a horizontal line; returns true if it can, false if it can't
{
if ((c + distance > HMAX) || (c + distance < HMIN)) // checks to see if it can plot the line
return false;
if (fgbg == FG) // for foreground plotting
{
if (distance >= 0) // for plotting positive distances
for (int k = 0; k <= distance; k++)
setChar(r, c + k, ch);
else // for plotting negative distances
for (int k = 0; k >= distance; k--)
setChar(r, c + k, ch);
}
else if (fgbg == BG) // for background plotting
{
if (distance >= 0) // for plotting positive distances
{
for(int i = 0; i <= distance; i++)
{
if (getChar(r, c + i) == ' ')
setChar(r, c + i, ch);
}
}
else // for plotting negative distances
{
for(int i = 0; i >= distance; i--)
{
if (getChar(r, c + i) == ' ')
setChar(r, c + i, ch);
}
}
}
else
return false;
return true;
}
bool plotVerticalLine(int r, int c, int distance, char ch, int fgbg) // tries to plot a vertical line; returns true if it can; false if it can't
{
if ((r + distance > VMAX) || (r + distance < VMIN)) // checks to see if it can plot the line
{
return false;
}
if (fgbg == FG) // for foreground plotting
{
if (distance >= 0) // for plotting positive distances
{
for (int k = 0; k <= distance; k++)
setChar(r + k, c, ch);
}
else // for plotting negative distances
for (int k = 0; k >= distance; k--)
setChar(r + k, c, ch);
}
else if (fgbg == BG) // for background plotting
{
if (distance >= 0) // for plotting positive distances
{
for (int i = 0; i <= distance; i++)
{
if (getChar(r + i, c) == ' ')
setChar(r + i, c, ch);
}
}
else // for plotting negative distances
{
for (int i = 0; i >= distance; i--)
{
if (getChar(r + i, c) == ' ')
setChar(r + i, c, ch);
}
}
}
else
return false;
return true;
}
bool plotLine(int r, int c, int distance, int dir, char plotChar, int fgbg) // plots the line using the functions plotHorizontalLine and plotVerticalLine
{
if ((dir != HORIZ) && (dir != VERT)) // error checking
return false;
if ((fgbg != FG) && (fgbg != BG))
return false;
if ( ! (isprint(plotChar)))
return false;
if (((dir == HORIZ) && ( ! (plotHorizontalLine(r, c, distance, plotChar, fgbg)))) || ((dir == VERT) && ( ! (plotVerticalLine(r, c, distance, plotChar, fgbg))))) // tries to plot line
{
return false;
}
return true;
}
int performCommands(string commandString, char& plotChar, int& mode, int& badPos)
{
string distanceSpecified = ""; // used to give us the integer version of distanceSpecifiedInt
int distanceSpecifiedInt = 0;
int directionSpecified = HORIZ; // checks vertical line vs horizontal line desired
int specificDirectionSpecified = LEFT; // used to make distance negative later on if the line should go left
int currentXPosition = 1; // used to keep track of cursor's x position
int currentYPosition = 1; // used to keep track of cursor's y position
bool needToPlotLine = false;
int futureIncrement = 0; // used for case when a 0 is used as a placeholder
for(int i = 0; i != commandString.size(); i++)
{
futureIncrement = 0;
needToPlotLine = false;
distanceSpecified = "";
distanceSpecifiedInt = 0;
if (commandString[i] == 'h' || commandString[i] == 'H') // creates a distanceSpecifiedInt for h function call that gives us the parameter to pass to the plotLine function
{
directionSpecified = HORIZ;
needToPlotLine = true;
if ((commandString.size() >= i + 1) && (isdigit(commandString[i+1]))) // checks for a number at the next two characters, adding it to the distance that the line will go
{
if (commandString[i+1] == '0')
futureIncrement += 1;
distanceSpecified += commandString[i+1];
specificDirectionSpecified = RIGHT;
if ((isdigit(commandString[i+2])) && commandString.size() >= i + 2)
{
distanceSpecified += commandString[i+2];
}
}
else if ((commandString.size() >= i + 1) && (commandString[i+1] == '-')) // checks for up to two negative numbers, adding it to the distance that the line will go
{
specificDirectionSpecified = LEFT; // tells us to make it a negative distance later
if ((commandString.size() >= i + 2) && (isdigit(commandString[i+2])))
{
if (commandString[i+2] == '0')
futureIncrement += 1;
distanceSpecified += commandString[i+2];
if ((commandString.size() >= i + 3) && (isdigit(commandString[i+3])))
{
distanceSpecified += commandString[i+3];
}
}
else
{
badPos = i + 2;
return 1;
}
}
else
{
badPos = i + 1;
return 1;
}
distanceSpecifiedInt = stoi(distanceSpecified); // turns distance from a string to an int
if (specificDirectionSpecified == LEFT) // makes distance negative if the line is to be plotted left
distanceSpecifiedInt *= -1;
}
else if (commandString[i] == 'v' || commandString[i] == 'V') // manages vertical line commands
{
directionSpecified = VERT;
needToPlotLine = true;
if ((commandString.size() >= i + 1) && (isdigit(commandString[i+1]))) // looks for up to two numbers to add to the distance the plotted line will go
{
if (commandString[i+1] == '0')
futureIncrement += 1;
distanceSpecified += commandString[i+1];
specificDirectionSpecified = UP;
if ((isdigit(commandString[i+2])) && commandString.size() >= i + 2)
{
distanceSpecified += commandString[i+2];
}
}
else if ((commandString.size() >= i + 1) && (commandString[i+1] == '-')) // looks for up to two negative numbers to add the distance the plotted line will go
{
specificDirectionSpecified = DOWN;
if ((commandString.size() >= i + 2) && (isdigit(commandString[i+2])))
{
if (commandString[i+2] == '0')
futureIncrement += 1;
distanceSpecified += commandString[i+2];
if ((commandString.size() >= i + 3) && (isdigit(commandString[i+3])))
{
distanceSpecified += commandString[i+3];
}
}
else
{
badPos = i + 1;
return 1;
}
}
else
{
badPos = i + 1;
return 1;
}
distanceSpecifiedInt = stoi(distanceSpecified);
if (specificDirectionSpecified == DOWN) // makes distance negative if the line goes down
distanceSpecifiedInt *= -1;
}
else if (commandString[i] == 'f' || commandString[i] == 'F') // manages changing to foreground mode with a character
{
if ((commandString.size() >= i + 1) && (isprint(commandString[i+1])))
{
mode = FG;
plotChar = commandString[i + 1];
i++;
}
else
{
badPos = i + 1;
return 1;
}
}
else if (commandString[i] == 'b' || commandString[i] == 'B') // manages changing to background mode with a character
{
if ((commandString.size() >= i + 1) && (isprint(commandString[i+1])))
{
mode = BG;
plotChar = commandString[i + 1];
i++;
}
else
{
badPos = i + 1;
return 1;
}
}
else if (commandString[i] == 'c' || commandString[i] == 'C') // manages the clear function call
{
clearGrid();
mode = FG;
currentXPosition = 1;
currentYPosition = 1;
plotChar = '*';
}
else
{
badPos = i;
return 1;
}
if (needToPlotLine) // checks and plots the line if it needs to plot a line
{
if ( ! (plotLine(currentXPosition, currentYPosition, distanceSpecifiedInt, directionSpecified, plotChar, mode)))
{
badPos = i;
return 2;
}
if (directionSpecified == HORIZ) // changes x coordinate for next iteration
currentYPosition += distanceSpecifiedInt;
else if (directionSpecified == VERT)// changes y coordinate for next iteration
currentXPosition += distanceSpecifiedInt;
if (distanceSpecifiedInt >= 10) // increases i value to the index of the beginning of the next command
i += 2;
else if ((distanceSpecifiedInt < 10) && (distanceSpecifiedInt > 0))
i++;
else if ((distanceSpecifiedInt < 0) && (distanceSpecifiedInt > -10))
i += 2;
else if (distanceSpecifiedInt <= -10)
i += 3;
}
if (futureIncrement == 1) // increments i if a 0 was used as a placeholder, ex: 03 instead of 3
i++;
}
return 0;
}