Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: HRSLASH en 3 Marzo 2012, 21:41 pm



Título: Graficos de Tortuga (C++)
Publicado por: HRSLASH en 3 Marzo 2012, 21:41 pm
Hola foreros! dejo aqui un programa q hice basado en los graficos de tortuga. El programa recibe comandos q luego interpreta para dibujar sobre una matriz. Espero comentarios y sugerencias.
Saludos!

Código
  1.  
  2. #include <iostream>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. const int floor_size = 20;
  7.  
  8. void mainMenu(void);
  9. void turtleGraphics(int [][floor_size], int []);
  10.  
  11. int main(void)
  12. {
  13.    const int max_command = 256;
  14.  
  15.    int floor[floor_size][floor_size] = {{false},{false}};
  16.    int command[max_command] = {false};
  17.  
  18. mainMenu();
  19.  
  20.    cout << "\nEnter commands(max " << max_command << "): ";
  21.    int i = 0;
  22.    while (command[i - 1] != 9){
  23.        cin >> command[i];
  24.        i++;
  25.    }
  26. cout << "\n" << endl;
  27. turtleGraphics(floor, command);
  28.  
  29.    return 0;
  30. }
  31.  
  32. void mainMenu(void)
  33. {
  34. cout << "*****" << setw(20) << "Turtle Graphics" << setw(10) << "*****" << endl;
  35. cout << "\nCommand" << setw(20) << "Meaning" << endl;
  36. cout << setw(7) << "1" << setw(20) << "Pen up" << endl;
  37. cout << setw(7) << "2" << setw(20) << "Pen down" << endl;
  38. cout << setw(7) << "3" << setw(20) << "Turn right" << endl;
  39. cout << setw(7) << "4" << setw(20) << "Turn left" << endl;
  40. cout << setw(7) << "5,10" << setw(20) << "Move forward 10"
  41. << "\n" << setw(27) << "spaces (or a number" << "\n"
  42. << setw(27) << "other than 10)" << endl;
  43. cout << setw(7) << "6" << setw(20) << "Print the" << "\n"
  44. << setw(15) << floor_size << "-by-" << floor_size
  45. << " array" << endl;
  46. cout << setw(7) << "9" << setw(20) << "End of data" << "\n"
  47. << setw(27) << "(sentinel)" << endl;
  48. }
  49.  
  50. void turtleGraphics(int flr[][floor_size], int cmd[])
  51. {
  52. int coord_x = 0, coord_y = 0, move = 0,
  53. row, column, i = 0;
  54. bool pen = false;
  55.  
  56. while (cmd[i] != 9){
  57. //Uncoment next line to show coord_x and coord_y
  58. //cout << "X: " << coord_x << "\nY: " << coord_y << endl;
  59. switch(cmd[i]){
  60. case 1: //Don't write
  61. pen = false;
  62. break;
  63. case 2: //Write
  64. pen = true;
  65. break;
  66. case 3: //Turn rigth
  67. if (move == 3)
  68. move = 0;
  69. else
  70. move++;
  71. break;
  72. case 4: //Turn left
  73. if (move == 0)
  74. move = 3;
  75. else
  76. move--;
  77. break;
  78. case 5: // Move turtle
  79. i++;
  80. switch(move){ //Nested switch (control direction)
  81. case 0: //Move down
  82. if ((cmd[i] + coord_y) > floor_size) //Check if it is in floor limits
  83. cmd[i] = floor_size - coord_y - 1; //If not, set move to the limit
  84.  
  85. for (row = coord_y; row < (cmd[i] + coord_y); row++)
  86. for (column = coord_x; column < coord_x + 1; column++)
  87. flr[row][column] = pen;
  88. coord_y = row;
  89. break;
  90. case 1: //Move rigth
  91. if ((cmd[i] + coord_x) > floor_size) //Check if it is in floor limits
  92. cmd[i] = floor_size - coord_x - 1; //If not, set move to the limit
  93.  
  94. for (row = coord_y; row < coord_y + 1; row++)
  95. for (column = coord_x; column < (cmd[i] + coord_x); column++)
  96. flr[row][column] = pen;
  97. coord_x = column;
  98. break;
  99. case 2: //Move up
  100. if (cmd[i] > coord_y) //Check if it is in floor limits
  101. cmd[i] = floor_size - 1; //If not, set move to the limit
  102.  
  103. for (row = coord_y; row > (coord_y - cmd[i]); row--)
  104. for (column = coord_x; column < coord_x + 1; column++)
  105. flr[row][column] = pen;
  106. coord_y = row;
  107. break;
  108. case 3: //Move left
  109. if (cmd[i] > coord_x) //Check if it is in floor limits
  110. cmd[i] = floor_size - 1; //If not, set move to the limit
  111.  
  112. for (row = coord_y; row < coord_y + 1; row++)
  113. for (column = coord_x; column > (coord_x - cmd[i]); column--)
  114. flr[row][column] = pen;
  115. coord_x = column;
  116. break;
  117. default:
  118. ;
  119. } //End nested switch
  120. break;
  121. case 6: // Print floor
  122. for (int i = 0; i < 20; i++){
  123. for (int j = 0; j < 20; j++){
  124. if (flr[i][j] == true)
  125. cout << setw(2) << "*";
  126. else
  127. cout << setw(2) << " ";
  128. }
  129. cout << endl;
  130. }
  131. break;
  132. default:
  133. ;
  134. }
  135. i++;
  136. }
  137. }
  138.  
  139.  


Título: Re: Graficos de Tortuga (C++)
Publicado por: Xandrete en 4 Marzo 2012, 11:13 am
Tienes un error. El compilador sólo se queja si compilas con la opción -Wall

Fíjate:

Código
  1.    int i = 0;
  2.    while (command[i - 1] != 9){
  3.        cin >> command[i];
  4.        i++;
  5.    }

Eso significa que comenzará la iteración  en la posición -1 del array. Malo.

En su lugar, deberías poner algo así como:

Código
  1.    do {
  2.        cin >> command[i];
  3.        i++;
  4.    } while (command[i - 1] != 9);

O...

Código
  1.    int i = 0;
  2.    cin >> command[i];
  3.    while (command[i] != 9){
  4.        i++;
  5.        cin >> command[i];
  6.    }

O...

Código
  1.    int i = 0;
  2.    while (cin >> command[i] and command[i] != 9){
  3.        i++;
  4.    }

Saludos


Título: Re: Graficos de Tortuga (C++)
Publicado por: HRSLASH en 5 Marzo 2012, 15:31 pm
Tenes razon! no habia notado ese detalle..  :-\  :D