Trying to create a Main for Turtle Graphics.

I will be honest, this is a homework a homework question.(kind of).

I'm taking my first year programming class, and I'm at the end our professor gave us two broken class codes and said if we fix them before our exam, it'll help us, and we can bring whatever codes we write beforehand in. (However, didn't explain what the exam will be until we start)

I've fixed both, and furthermore, I believe they're part of a Turtle Graphics Program. The closest thing we've done to that is a VERY elementary ACSCII ART assignment.

So, what I'd like to ask is, how should I go about trying to implement these classes into a main, and what should I do with the pen.h

I'd rather not have anyone actually write out full code, but just advice as to where to start and what to do.

Canvass .CPP
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
  #include "Canvas.h"

bool Canvas::growHoriztontal()
{
	if (getWidth() < MAX_HORI) {
		for (unsigned int i = 0; i < getHeight(); i++) {
			_canvas[i].push_back(false);
		}
		return true;
	}
	return false;
}

bool Canvas::growVertical()
{
	if (getHeight() != MAX_VERTI) {
		_canvas.push_back(std::vector<bool>(getWidth(), false));
		return true;
	}
	return false;
}

bool Canvas::processDrawAction(PEN_AXIS plane, int extent)
{
	if (plane == DEPTH) {
		if (_currInstruction != NULL) {
			_instructions.push_back(_currInstruction);
		}
		if (extent == 1) {
			_instructions.push_back((new Instruction(SET, _pen._x, _pen._y)));
		}
	}
	else if (isPenDown()) {
		INSTRUCTION newInstruction = plane == HORI ? DRH : DRV;
		if (_currInstruction != NULL) {
			if (_currInstruction->cmd == newInstruction) {
				_currInstruction->arg1 += extent;
			}
			else {
				_instructions.push_back(_currInstruction);
				_currInstruction = new Instruction(newInstruction, extent);
			}
		}
		else {
			_currInstruction = new Instruction(newInstruction, extent);
		}
		return true;
	}
	return false;
}

void Canvas::stop()
{
	if (_currInstruction != NULL) {
		_instructions.push_back(_currInstruction);
		_currInstruction = NULL;
	}
}

void Canvas::mark()
{
	if (isPenDown())
	{
		_canvas[_pen._y][_pen._x] = true;
	}
}

Canvas::Canvas()
{
	for (unsigned int i = 0; i < INIT_DIM; i++) {
		_canvas.push_back(std::vector<bool>(INIT_DIM, false));
	}
}

void Canvas::draw(std::ostream & sout, bool showDetails)
{
	if (showDetails) {
		sout << "Pen Position: " << _pen._x << "," << _pen._y << std::endl;
		sout << "Image Size:" << getWidth() << "," << getHeight() << std::endl;
	}

	for (unsigned int y = 0; y < getHeight(); y++) {
		for (unsigned int x = 0; x < getWidth(); x++) {
			if (_pen._show && _pen._x == x && _pen._y == y) {
				sout << (_pen._down ? PEN_STATUS_DOWN : PEN_STATUS_UP);
			}
			else {
				sout << (_canvas[y][x] ? '*' : ' ');
			}
		}
		sout << std::endl;
	}
}

unsigned int Canvas::getWidth()
{
	return _canvas[0].size();
}

unsigned int Canvas::getHeight()
{
	return _canvas.size();
}

unsigned int Canvas::getPenX()
{
	return _pen._x;
}

unsigned int Canvas::getPenY()
{
	return _pen._y;
}

bool Canvas::movePen(PEN_AXIS pa, bool positive)
{
	mark();
	if (pa == HORI) {
		if (positive) {
			if (_pen._x + 1 == getWidth())
			{
				if (!growHoriztontal()) {
					return false;
				}
			}
			_pen._x++;
			processDrawAction(pa, 1);
			return true;
		}
		else {
			if (_pen._x <= 0) {
				return false;
			}
			_pen._x--;
			processDrawAction(pa, -1);
			return true;
		}
	}
	else {
		if (positive) {
			if (_pen._y + 1 == getHeight())
			{
				if (!growVertical()) {
					return false;
				}
			}
			_pen._y++;
			processDrawAction(pa, 1);
			return true;
		}
		else {
			if (_pen._y <= 0) {
				return false;
			}
			_pen._y--;
			processDrawAction(pa, -1);
			return true;
		}
	}

	return false;
}

void Canvas::togglePen()
{
	processDrawAction(DEPTH, _pen._down ? -1 : 1);
	_pen._down = !_pen._down;
}

void Canvas::toggleShowPen()
{
	_pen._show = !_pen._show;
}

bool Canvas::isPenDown()
{
	return _pen._down;
}

bool Canvas::doCommand(ACTION a)
{
	switch (a) {
	case LEFT:
		movePen(HORI, false);
		break;
	case RIGHT:
		movePen(HORI, true);
		break;
	case DOWN:
		movePen(VERTI, true);
		break;
	case UP:
		movePen(VERTI, false);
		break;
	case PEN_TOGGLE:
		togglePen();
		break;
	case PEN_POSITION:
		toggleShowPen();
		break;
	case STOP:
		stop();
		break;
	}

	return false;
}

std::vector<Instruction*> Canvas::getInstructions()
{
	return _instructions;
}


Canvass.h

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
#pragma once
#include <vector>
#include <iostream>
#include "Pen.h"
#include "Instruction.h"

enum ACTION { UP, DOWN, LEFT, RIGHT, PEN_TOGGLE, STOP, PEN_POSITION, NONE };
const unsigned int INIT_DIM = 4, MAX_HORI = 50, MAX_VERTI = 18;

class Canvas
{
private:
	Pen _pen;
	std::vector<std::vector<bool>> _canvas;
	std::vector<Instruction*> _instructions;
	Instruction* _currInstruction = NULL;
	bool growHoriztontal();
	bool growVertical();
	bool processDrawAction(PEN_AXIS, int);
	void stop();
	void mark();

public:
	Canvas();
	void draw(std::ostream&, bool = true);
	unsigned int getWidth();
	unsigned int getHeight();
	unsigned int getPenX();
	unsigned int getPenY();
	bool movePen(PEN_AXIS, bool);
	void togglePen();
	void toggleShowPen();
	bool isPenDown();
	bool doCommand(ACTION);
	std::vector<Instruction*> getInstructions();
};


instructions.cpp
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
#include "Instruction.h"

Instruction::Instruction(INSTRUCTION cmd, int arg1)
{
	this->cmd = cmd;
	this->arg1 = arg1;
	this->numArg = 1;
}

Instruction::Instruction(INSTRUCTION cmd, unsigned int arg1, unsigned int arg2)
{
	this->cmd = cmd;
	this->arg1 = static_cast<int>(arg1);
	this->arg2 = static_cast<int>(arg2);
	this->numArg = 2;
}

std::string Instruction::ToString()
{
	std::string r;
	switch (cmd)
	{
	case SET:
		r = "SET ";
		break;
	case DRV:
		r = "DRV ";
		break;
	case DRH:
		r = "DRH ";
		break;
	}

	r += std::to_string(arg1);
	if (numArg == 2) {
		r += " " + std::to_string(arg2);
	}
	return r;
}


instructions.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#pragma once

#include <string>

enum INSTRUCTION { SET, DRV, DRH };
struct Instruction
{
public:
	INSTRUCTION cmd;
	int arg1, arg2;
	unsigned int numArg;
	Instruction(INSTRUCTION cmd, int arg1);
	Instruction(INSTRUCTION cmd, unsigned int arg1, unsigned int arg2);
	std::string ToString();
};


Pen.h
1
2
3
4
5
6
7
8
9
10
#pragma once
enum PEN_AXIS { HORI, VERTI, DEPTH };
const char PEN_STATUS_UP = 'O', PEN_STATUS_DOWN = 'X';
struct Pen
{
public:

	unsigned int _x = 0, _y = 0;
	bool _down = false, _show = true;
};


Thanks guys, I have tried extensively on my own, but simply can't figure it ouyt.

*EDIT* If you guys see any glaring errors please do point them out :D
Last edited on
You can use the canvas class like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
#include "canvas.h"

int main()
{
  Canvas canvas;

  canvas.doCommand(UP);
  canvas.movePen(HORI, true);
  canvas.movePen(VERTI, true);
  canvas.doCommand(DOWN);
  canvas.draw(std::cout);
}

OUTPUT
Pen Position: 1,2
Image Size:4,4


 O

Pen.h is used inside the Canvas class so you don't have to bother about it.
Topic archived. No new replies allowed.