Turtle graphics

I am stuck at this point and really need help getting back on track. Below is the assignment details followed by the code I have written so far

The turtle holds a pen in one of two positions, up or down.

When the pen is down the turtle draws out the shape as it moves.

When the pen is up, the turtle moves about freely without writing anything.

For this assignment you will write a program to simulate the operation of the turtle thus replicating a computerized drafting board, plotter, and a computerized cutting machine.

Use a n by n array where you determine the size of the array and call the array grid. The array should be initialized to all zeros. The commands to manipulate the “turtle” are input from an external file called turtle.txt.

A listing of valid commands are shown below:


Command Meaning
U Pen up
D Pen down
R Turn right
L Turn left
F n move forward n spaces
P Print the 30 by 30 array
Q End of data sentinel

Any movement of the turtle when the pen is down will change the elements of the array to 1’s.

When the print command is executed you should display the configuration of the array by displaying an asterisk (*) for each occurrence of 1. A blank is displayed for each occurrence of a zero.

You will have to create a file of turtle commands to print out the word Computer on the screen.

Example of Turtle commands

 U ---- pen is up
 F 10 ---- move forward 10 squares from the current position i.e. if position 0, will end up at position 10
 D ---- Put the pen down
 L ---- Turn left
 F 5 ---- draw a line starting from the current location to location + 4, stop at current location + 4

Write the computer program using the commands in turtle.txt to spell the word Computer on the screen


#include <iostream>
#include <fstream>
using namespace std;

const int NUMROWS = 20;
const int NUMCOLS = 40;

// prototypes:
void fileValidation(ifstream &infile);
void printArray(int grid[][NUMCOLS]);
void processCommands(int grid[][NUMCOLS], ifstream &infile, int &direction);
void movement(int grid[][NUMCOLS], bool &pen, int &direction);

int main() {

ifstream infile;
int grid[NUMROWS][NUMCOLS] = { 0 };
int rows, cols;
bool pen = false; // false = pen up
int direction = 1; // 1 = east, 2 = south, 3 = west, 4 = north

printArray(grid);
fileValidation(infile);
processCommands(grid, infile, direction);
movement(grid, pen, direction);

infile.close(); // close the file

return 0;
} // end main

/*****************************************************************************
Function name: fileValidation
Description: opening the file and validating it was opened correctly.
*****************************************************************************/
void fileValidation(ifstream &infile) {

infile.open("turtle.txt");
if (!infile) {
cout << "Error opening file..." << endl;
exit(1106);
} // end if

infile.close();
} // end file validation

/*****************************************************************************
Function name: processCommand use switch
Description: get command and process commmand until EOF when you get the q
quits and p prints
*****************************************************************************/
void processCommands(int grid[][NUMCOLS], ifstream &infile, int &direction){

char command;
int spacesMoved;

infile.open("turtle.txt");
if (!infile) {
cout << "Error opening file..." << endl;
exit(1106);
} // end if

for (int i = 0; i < NUMROWS; i++)
for (int j = 0; j < NUMCOLS; j++) {
grid[i][j] = { 0 };
} // end for

while (infile >> command && (toupper(command)) != 'Q') {
if (direction == 1) {
if (command = 'R')
direction == 2;
else if (command == 'L')
direction = 4;
} // case 1
else if (direction == 2) {
if (command == 'R')
direction == 3;
else if (command == 'L')
direction == 1;
} // case 2
else if (direction == 3) {
if (command == 'R')
direction == 4;
else if (command == 'L')
direction == 2;
} // case 3
else if (direction == 4) {
if (command == 'R')
direction = 1;
else if (command == 'L')
direction = 3;
} // case 4
} // end while

infile.close();
} // end void processCommands

/*****************************************************************************
Function name: movement
Description: contains all code for movement of the turtle.
*****************************************************************************/
void movement(int grid[][NUMCOLS], bool &pen){


} // end void movement

/*****************************************************************************
Function name: printArray
Description: displaying the array
*****************************************************************************/
void printArray(int grid[][NUMCOLS], int &direction) {

if (direction == 2) {
int i = y;
for (int i = 0; i < NUMROWS; i++)
for (int j = 0; j < NUMCOLS; j++) {
cout << grid[i][j] << (j == 40 ? "\n" : "");
}
}
} // end void printArray

[/code]
Topic archived. No new replies allowed.