0% found this document useful (0 votes)
44 views5 pages

Turtle Graphics

The document outlines a project to create a Turtle Graphics program using a 2D array in C++. The program involves moving a turtle represented by 'O' around a 20x25 field, leaving a trail of '*' symbols, while implementing features like borders, random rock placement, and move validation. Additionally, a bonus challenge includes creating a Chaser Turtle that follows the main turtle's movements under certain constraints.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views5 pages

Turtle Graphics

The document outlines a project to create a Turtle Graphics program using a 2D array in C++. The program involves moving a turtle represented by 'O' around a 20x25 field, leaving a trail of '*' symbols, while implementing features like borders, random rock placement, and move validation. Additionally, a bonus challenge includes creating a Chaser Turtle that follows the main turtle's movements under certain constraints.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

2D Arrays (Turtle Graphics)

Turtle Graphics is a method for creating graphic images originally in the


Logo programming language. The "turtle" is an imaginary pen that is
given drawing commands, such as go up, down, left and right. You will
be writing a version of turtle graphics using a 2D array to move the turtle
around the matrix to draw an image.

Your turtle graphics program will be relatively simple. You will create a
20x25 2D char array. The capital letter ‘O’ will represent the turtle and
lines will be created using the * symbol. As the turtle moves around the
screen it will leave the * symbol behind creating a trail of where it has
been.

Before starting this lab, select TDM-GCC 9.2.0 65-bit Debug at the
top of your compiler.

When the program begins, the turtle’s field is outlined with the = symbol
marking the top and bottom border of the field, and the | symbol marking the
left and right borders. The turtle is shown in its starting position of 1, 1. The
outline symbols are NOT part of the character array. Three rocks will be
randomly placed in the field.

The number pad can be used with keys, 8, 2, 4, and 6 to move the turtle
around the field to create drawings. These numbers correspond with the
arrows on the number pad of your keyboard.

To move the turtle around the screen, you will use the getch() method rather than the cin
command. The getch() will pause the program to wait for a keyboard input and it will return the key
entered as a char. If 1 is entered it will return ‘1’, if 2 is entered it will return ‘2’. This will allow you to
enter the direction without having to press the Enter key after each entry and easily move the turtle
around the field.
Part 1: Write the basic Turtle Graphics program.

1. You have been provided with the following starter file. Copy and paste this starter code into
your C++ Compiler.

//Add the appropriate preprocessor directives here.

//Add the function prototypes here

using namespace std;

//Globals – Starting and current position of the turtle in the field.


int tr;
int tc;

int main()
{

char field[20][25]; //Create the 20x25 field array.


initialize(field); //Initialize the field array & reset the program.

display(field); //Display the field.

char dir;
do{
//Show menu options
cout << "\t 8 - Move Up" << endl;
cout << "\t 2 - Move Down" << endl;
cout << "\t 4 - Move Left" << endl;
cout << "\t 6 - Move Right" << endl;
cout << "\t 0 - Exit" << endl;

dir = getch(); //Get move option from the keyboard.

if(dir != '0')
move(field, dir); //Move the turtle.

}while(dir != '0');

return 0;

//Function Definitions – TO DO

void initialize(char f[][25])


{
}

void display(char f[][25])


{
}

void move(char f[][25], char dir)


{
}
1. Add the appropriate preprocessor directives, includes, at the top of the program.
2. Add the function prototypes below the includes.

3. Complete the initialize function. This function should do the following:


• Initialize all the array elements to a space.
• Assign starting values to the tr and tc variables. This is the starting position of the turtle.
• Assign the turtle ‘O’ to position tr and tc.

4. Complete the display function. This function should do the following:


• Clear the screen.
• Display the current location of the turtle in the following format.
➢ Current Location: 8, 14 (Indicating row 8 column 14)
• Display the outlined field array.
• Everything should be tabbed over even with the menu options.

5. Complete the move function. This function will receive the desired move as the dir variable.
This function should do the following:
• Assign ‘*’ to the current position of the turtle.
• Modify the tr or td values based on the value of the dir variable. For example, if the turtle
should move up, reduce the tr value by 1.
• Assign the ‘O’ to the new position of the turtle.
• Call the display function to display the updated field.

Test your program by running it and moving the turtle around


the field. The current location of the turtle should be shown
above the field. The array should be bordered by = symbols
above and below the field and | symbols on the left and right
side of the field. NOTE: These symbols should NOT be stored in
the array.
Part 2: Validating Moves

The program has a major flaw in it! There is nothing that keeps the turtle from moving out of the
range of the field. For this part, you should incorporate a check feature that will verify that the move
is valid before moving the turtle. When the turtle hits a wall, it should no longer be able to move in
that direction. Test your program to make sure that the turtle remains within the borders of the field.

Part 3: Add the placeRock function: void placeRock(char f[][25])

Create the appropriate prototype for this function.

This function should create a “rock” and place it into the field at a
random location. Specifications of the rock:
• This function will be called from the initialize function.
• The rock should consist of four ‘X’ characters.
XX
XX
• The rock cannot be directly next to the border of the field, the
must be placed within a smaller range within the field so that
no rocks are spawned on top of the turtle or outside the field.
• Generate a random x and y value for the top left ‘X’ of the
rock.
• Place this rock into the field… add the ‘X’ values to the field
array.

Part 4: Modify the program so that the turtle can not walk through rocks.
• There are several ways you can do this. Use what you know to figure this out.
Bonus Challenge!
Create a Chaser Turtle: This turtle will be moved by the program. Specifications for the Chaser Turtle
are:
• The Chaser Turtle should be represented by the @ symbol.
• The Chaser Turtle should start at the bottom right corner of the field.
• It should make one move toward your turtle every time your turtle makes a move. It should
use the turtle’s current tr and tc values to determine it’s next move.
• The Chaser Turtle must follow the same move rules as the original turtle, can’t walk out of
range and can’t walk through rocks.
• The Chaser Turtle also can not walk through the trail left by your turtle.
• If the Challenge Turtle catches your turtle, the program should reset by calling the initialize
function.

You might also like