Advertisement
joaquinflorespalao

Hoja 4 - Ejercicio 2

Jun 20th, 2018
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <conio.h>
  4. using namespace std;
  5. using namespace System;
  6.  
  7. #define Arriba 'w'
  8. #define Abajo 's'
  9. #define Izquierda 'a'
  10. #define Derecha 'd'
  11.  
  12. struct Escenario {
  13. const short lado = 15;
  14. short Mapa[15][15] = {
  15. {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
  16. {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  17. {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  18. {1,0,0,0,0,1,1,1,1,1,0,0,0,0,1},
  19. {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  20. {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  21. {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  22. {1,1,1,1,1,1,0,0,0,1,1,1,1,1,1},
  23. {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  24. {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  25. {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  26. {1,0,0,0,0,1,1,1,1,1,0,0,0,0,1},
  27. {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  28. {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  29. {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
  30. };
  31. void mostrar() {
  32. for (short f = 0; f < lado; f++) {
  33. for (short c = 0; c < lado; c++) {
  34. Console::SetCursorPosition(c, f);
  35. if (Mapa[f][c] == 1)
  36. Console::BackgroundColor = ConsoleColor::Blue;
  37. else
  38. Console::BackgroundColor = ConsoleColor::White;
  39. cout << " ";
  40. }
  41. }
  42. Console::BackgroundColor = ConsoleColor::White;
  43. }
  44. bool esLimite(short x, short y) {
  45. return (Mapa[y][x] == 1 ||
  46. x < 0 || y < 0 ||
  47. x >= lado || y >= lado);
  48. }
  49. };
  50.  
  51. struct Movil{
  52. short x, y, dy=1, retraso, limite=1000;
  53. string nombre;
  54. void ingresarCoordenadas(Escenario &esc) {
  55. do {
  56. Console::SetCursorPosition(0, 17);
  57. cout << nombre << endl;
  58. cout << "Ingrese la Coordenada" << endl;
  59. cout << "x: "; cin >> x;
  60. cout << "y: "; cin >> y;
  61. Console::SetCursorPosition(0, 17);
  62. cout << " " << endl;
  63. cout << " " << endl;
  64. cout << " " << endl;
  65. cout << " " << endl;
  66. } while (esc.esLimite(x, y));
  67. }
  68. void mover(Escenario &esc) {
  69. if (retraso == limite) {
  70. retraso = 0;
  71. Console::SetCursorPosition(x, y);
  72. cout << " ";
  73. if (esc.esLimite(x, y + dy)) dy *= -1;
  74. y += dy;
  75. Console::SetCursorPosition(x, y);
  76. cout << "*";
  77. }
  78. retraso++;
  79. }
  80. };
  81.  
  82. struct Auto {
  83. short x, y;
  84. bool chocado = false;
  85. void ingresarCoordenadas(Escenario &esc) {
  86. do {
  87. Console::SetCursorPosition(0, 17);
  88. cout << "Tu auto" << endl;
  89. cout << "Ingrese la Coordenada" << endl;
  90. cout << "x: "; cin >> x;
  91. cout << "y: "; cin >> y;
  92. Console::SetCursorPosition(0, 17);
  93. cout << " " << endl;
  94. cout << " " << endl;
  95. cout << " " << endl;
  96. cout << " " << endl;
  97. } while (esc.esLimite(x, y));
  98. }
  99. void mover(Escenario &esc, char mov) {
  100. Console::SetCursorPosition(x, y);
  101. cout << " ";
  102. if (mov == Arriba && !esc.esLimite(x, y-1)) y--;
  103. else if (mov == Abajo && !esc.esLimite(x, y+1)) y++;
  104. else if (mov == Izquierda && !esc.esLimite(x-1, y)) x--;
  105. else if (mov == Derecha && !esc.esLimite(x+1, y)) x++;
  106. Console::SetCursorPosition(x, y);
  107. cout << char(2);
  108. }
  109. bool choque(Movil movil) {
  110. if (x == movil.x && y == movil.y && chocado == false)
  111. chocado = true;
  112. else if (!(x == movil.x && y == movil.y))
  113. chocado = false;
  114. return chocado;
  115. }
  116. };
  117.  
  118. void jugar() {
  119. Console::CursorVisible = false;
  120. Console::ForegroundColor = ConsoleColor::Red;
  121. Escenario esc;
  122. Movil movil1, movil2;
  123. Auto autoAlumno;
  124. movil1.nombre = "Movil 1";
  125. movil2.nombre = "Movil 2";
  126. esc.mostrar();
  127. movil1.ingresarCoordenadas(esc);
  128. movil2.ingresarCoordenadas(esc);
  129. autoAlumno.ingresarCoordenadas(esc);
  130. short choques = 0;
  131. while (true) {
  132. if (kbhit()) {
  133. char tecla = getch();
  134. if (tecla == 'S') break;
  135. autoAlumno.mover(esc, tecla);
  136. }
  137. movil1.mover(esc);
  138. movil2.mover(esc);
  139. choques += autoAlumno.choque(movil1);
  140. choques += autoAlumno.choque(movil2);
  141. Console::SetCursorPosition(0, 17);
  142. cout << "Chocaste " << choques << " veces.";
  143. }
  144. }
  145.  
  146. int main() {
  147. jugar();
  148. return 0;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement