ziofu
21-03-2007, 21:23
Ciao a tutti! Sono nuovo di qui.
Ho iniziato da poco a programmare in C++. Vengo da Java, quindi il passaggio non è rose e fiori. Non sono ancora arrivato alle classi, qunidi in sostanza (da quanto ho capito) è come se fino adesso avessi programmato in C.
Seguendo un libro, ho fatto un programma che dovrebbe essere un primordiale programma di disegno (turtle graphics) e volevo sapere:
- se il codice è abbastanza pulito: non sono molto contento dei 2 switch (sono convinto ci sia un metodo più elegante).
- come prendere in input 2 numeri consecutivi, ma lasciando il secondo "facoltativo". Cioè con:
cin >> a >> b;
salvare in a il primo ed eventualmente in b se viene passato.
Questo è il mio codice (siate pure cattivi che devo imparare ^^):
#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;
}
Ho iniziato da poco a programmare in C++. Vengo da Java, quindi il passaggio non è rose e fiori. Non sono ancora arrivato alle classi, qunidi in sostanza (da quanto ho capito) è come se fino adesso avessi programmato in C.
Seguendo un libro, ho fatto un programma che dovrebbe essere un primordiale programma di disegno (turtle graphics) e volevo sapere:
- se il codice è abbastanza pulito: non sono molto contento dei 2 switch (sono convinto ci sia un metodo più elegante).
- come prendere in input 2 numeri consecutivi, ma lasciando il secondo "facoltativo". Cioè con:
cin >> a >> b;
salvare in a il primo ed eventualmente in b se viene passato.
Questo è il mio codice (siate pure cattivi che devo imparare ^^):
#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;
}