Hi! I'm new here.

I'm reading a book about C++ so I'm quite new to the language.
An exercise asks to write a simple "Turtle Graphics" program.
This program should be able to simulates very simple graphics thanks to the following commands:

Command Meaning
1 - Pen up
2 - Pen down
3 - Turn right
4 - Turn left
5 , n - Move forward n spaces
6 - Print 20-by-20 array
9 - End of data (sentinel)

Now, before I post my code, my questions:

- is the code clear? I'm not very happy about the two switch I use to decide the command and the direction (in the move function).

- I wasn't able to implement the fifth command in one line, I mean you have to write 5, ENTER and than the N. The exercise asks to do it in one line. How can I achieve that?

Thank you for any suggestion and please don't be gentle: I want to learn :)

My code:

#include <iostream>

using namespace std;

void printMap(void);
void printCommands(void);
void move(int);

const int END = 9;
const int commands[7] = { 0, 1, 2, 3, 4, 5, 6 };
int map[20][20] = { 0 };
bool penDown = false;
int x = 0;
int y = 0;
int dir = 0; // 0 = N, 1 = E, 2 = S, 3 = O

int main() {

    cout << "Welcome to the Turtle Graphics! Here's the list of commmands:" << endl;
    printCommands();
    
    int command;
    int args;
    
    cout << "Enter a command: ";
    cin >> command;
    if (command == 5) cin >> args;
    
    while (command != END) {
        switch (command) {
            // print commands
            case 0:
            printCommands();
            break;
            
            // set the pen down
            case 1:
            if (penDown) penDown = false;
            else cout << "The pen is already up." << endl;
            break;
            
            // pen up
            case 2:
            if (!penDown) penDown = true;
            else cout << "The pen is already down." << endl;
            break;
            
            // turn right
            case 4:
            dir = (dir + 1) % 4;
            cout << "The new direction is " << dir << endl;
            break;
            
            // turn left
            case 3:
            dir -= 1;
            if (dir < 0) dir = 3;
            cout << "The new direction is " << dir << endl;
            break;
            
            // move forward
            case 5:
            cout << "Moving forward by " << args << endl;
            move(args);
            break;
            
            // show the map
            case 6:
            printMap();
            break;
        
        }
        
        cout << "Enter a command (0 to show commands): ";
        cin >> command;
        if (command == 5) cin >> args;
        
    }
    
    cout << "Bye bye!" << endl;
    return 0;
}

void move(int steps) {
    while (steps-- > 0) {
        switch (dir) {
            // N
            case 0:
            if (y > 0) y--;
            break;
            
            // E
            case 1:
            if (x < 20) x++;
            break;
        
            // S
            case 2:
            if (y < 19) y++;
            break;
            
            // O
            case 3:
            if (x > 0) x--;
            break;
        }
        if (penDown) map[y][x] = 1;
    }
}

void printMap() {
    for (int i = 0; i < 20; i++) {
        for (int j = 0; j < 20; j++) {
            if (x == j && y == i) cout << "X";
            else if (map[i][j] == 0) cout << " ";
            else cout << "*";
        }
        cout << endl;
    }
}

void printCommands() {
    cout << "0    - Show the commands available\n"
         << "1    - Turn up the pen\n"
         << "2    - Turn down the pen\n"
         << "3    - Turn left\n"
         << "4    - Turn right\n"
         << "5, n - Move therd of n tiles\n"
         << "6    - Show the drawing\n"
         << "9    - Exit program" << endl; 
}
Salem commented: Excellent first question - salem +6

Recommended Answers

All 6 Replies

Hi, and thanks for using code tags. The code itself looks pretty good.

I think you should have put more work into validating the user's input. One of the most simple things you could have done is placed a default condition in your input switch block that would inform the user that what he/she typed was invalid.

However, even that won't solve user input problems. For example, have you tried entering letters at the prompt at seen what happens? This is a lot harder to fix, because you must first place the input in a string and then validate it, finally convert it back to a number, and so it's probably not worth it for a small homework assignment. If you are interested, you can read my C++ input tutorial (doesn't cover the validation part, I'm planning to add that):
http://www.daniweb.com/tutorials/tutorial71858.html

Read everything as a string, then convert it to an input string stream where you can work your way through it at your own pace.

Example

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main ( ) {
    cout << "Enter command " << flush;
    string s1;
    getline(cin,s1);
    istringstream s2(s1);
    int type;
    s2 >> type;
    if ( type == 5 ) {
        int param;
        s2 >> param;
        cout << "Type " << type << " with param " << param << endl;
    } else {
        cout << "Just a regular " << type << endl;
    }
    return 0;
}

> I'm not very happy about the two switch I use to decide the command and the direction
Since dir is an integer from 0 to 3, use it to index an array of delta-x and delta-y values which correspond to the directions you want to move in.

> int map[20][20]
Too many uses of 20 in this code.
Declare a constant, then use that in all the places where you currently hard-code the value.

Thanks for answering.

Well you're right. There's no validation because I've still not studied string manipulation (it's in the next chapter) so I assumed there was no need.

You're tutorial is interesting, thanks for linking.

Read everything as a string, then convert it to an input string stream where you can work your way through it at your own pace.

Example

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main ( ) {
    cout << "Enter command " << flush;
    string s1;
    getline(cin,s1);
    istringstream s2(s1);
    int type;
    s2 >> type;
    if ( type == 5 ) {
        int param;
        s2 >> param;
        cout << "Type " << type << " with param " << param << endl;
    } else {
        cout << "Just a regular " << type << endl;
    }
    return 0;
}

> I'm not very happy about the two switch I use to decide the command and the direction
Since dir is an integer from 0 to 3, use it to index an array of delta-x and delta-y values which correspond to the directions you want to move in.

> int map[20][20]
Too many uses of 20 in this code.
Declare a constant, then use that in all the places where you currently hard-code the value.

Can you be more specific about the first suggestion? (i.e have I to split the dir in dirX and dirY?)

>Can you be more specific about the first suggestion? (i.e have I to split the dir in dirX and dirY?)
I would think you would do something like this:

const dirX[4] = {0, 1, 0, -1};
const dirY[4] = {1, 0, -1, 0};

Then all you would have to do is multiply 'steps' by dirX[dir] to get the X movement, and dirY[dir] for Y movement.

>Can you be more specific about the first suggestion? (i.e have I to split the dir in dirX and dirY?)
I would think you would do something like this:

const dirX[4] = {0, 1, 0, -1};
const dirY[4] = {1, 0, -1, 0};

Then all you would have to do is multiply 'steps' by dirX[dir] to get the X movement, and dirY[dir] for Y movement.

Now It's cleaner to me!
Thank you again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.