Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <conio.h>
- using namespace std;
- using namespace System;
- #define Arriba 'w'
- #define Abajo 's'
- #define Izquierda 'a'
- #define Derecha 'd'
- struct Escenario {
- const short lado = 15;
- short Mapa[15][15] = {
- {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
- {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,1,1,1,1,1,0,0,0,0,1},
- {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,1,1,1,1,1,0,0,0,1,1,1,1,1,1},
- {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,1,1,1,1,1,0,0,0,0,1},
- {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
- };
- void mostrar() {
- for (short f = 0; f < lado; f++) {
- for (short c = 0; c < lado; c++) {
- Console::SetCursorPosition(c, f);
- if (Mapa[f][c] == 1)
- Console::BackgroundColor = ConsoleColor::Blue;
- else
- Console::BackgroundColor = ConsoleColor::White;
- cout << " ";
- }
- }
- Console::BackgroundColor = ConsoleColor::White;
- }
- bool esLimite(short x, short y) {
- return (Mapa[y][x] == 1 ||
- x < 0 || y < 0 ||
- x >= lado || y >= lado);
- }
- };
- struct Movil{
- short x, y, dy=1, retraso, limite=1000;
- string nombre;
- void ingresarCoordenadas(Escenario &esc) {
- do {
- Console::SetCursorPosition(0, 17);
- cout << nombre << endl;
- cout << "Ingrese la Coordenada" << endl;
- cout << "x: "; cin >> x;
- cout << "y: "; cin >> y;
- Console::SetCursorPosition(0, 17);
- cout << " " << endl;
- cout << " " << endl;
- cout << " " << endl;
- cout << " " << endl;
- } while (esc.esLimite(x, y));
- }
- void mover(Escenario &esc) {
- if (retraso == limite) {
- retraso = 0;
- Console::SetCursorPosition(x, y);
- cout << " ";
- if (esc.esLimite(x, y + dy)) dy *= -1;
- y += dy;
- Console::SetCursorPosition(x, y);
- cout << "*";
- }
- retraso++;
- }
- };
- struct Auto {
- short x, y;
- bool chocado = false;
- void ingresarCoordenadas(Escenario &esc) {
- do {
- Console::SetCursorPosition(0, 17);
- cout << "Tu auto" << endl;
- cout << "Ingrese la Coordenada" << endl;
- cout << "x: "; cin >> x;
- cout << "y: "; cin >> y;
- Console::SetCursorPosition(0, 17);
- cout << " " << endl;
- cout << " " << endl;
- cout << " " << endl;
- cout << " " << endl;
- } while (esc.esLimite(x, y));
- }
- void mover(Escenario &esc, char mov) {
- Console::SetCursorPosition(x, y);
- cout << " ";
- if (mov == Arriba && !esc.esLimite(x, y-1)) y--;
- else if (mov == Abajo && !esc.esLimite(x, y+1)) y++;
- else if (mov == Izquierda && !esc.esLimite(x-1, y)) x--;
- else if (mov == Derecha && !esc.esLimite(x+1, y)) x++;
- Console::SetCursorPosition(x, y);
- cout << char(2);
- }
- bool choque(Movil movil) {
- if (x == movil.x && y == movil.y && chocado == false)
- chocado = true;
- else if (!(x == movil.x && y == movil.y))
- chocado = false;
- return chocado;
- }
- };
- void jugar() {
- Console::CursorVisible = false;
- Console::ForegroundColor = ConsoleColor::Red;
- Escenario esc;
- Movil movil1, movil2;
- Auto autoAlumno;
- movil1.nombre = "Movil 1";
- movil2.nombre = "Movil 2";
- esc.mostrar();
- movil1.ingresarCoordenadas(esc);
- movil2.ingresarCoordenadas(esc);
- autoAlumno.ingresarCoordenadas(esc);
- short choques = 0;
- while (true) {
- if (kbhit()) {
- char tecla = getch();
- if (tecla == 'S') break;
- autoAlumno.mover(esc, tecla);
- }
- movil1.mover(esc);
- movil2.mover(esc);
- choques += autoAlumno.choque(movil1);
- choques += autoAlumno.choque(movil2);
- Console::SetCursorPosition(0, 17);
- cout << "Chocaste " << choques << " veces.";
- }
- }
- int main() {
- jugar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement