0% found this document useful (0 votes)
33 views4 pages

Laberinto

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views4 pages

Laberinto

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include "pch.

h"
#include "conio.h"
#include "iostream"

#define FILAS 30
#define COLUMNAS 60
#define Arriba 72
#define Abajo 80
#define Derecha 77
#define Izquierda 75

using namespace System;


using namespace std;

void cursor(int x, int y) {


Console::SetCursorPosition(x, y);
}

void color(int c) {
Console::ForegroundColor = (ConsoleColor)c;
}

void genera_laberinto(int laberinto[FILAS][COLUMNAS]) {


for (int f = 0; f < FILAS; f++) {
for (int c = 0; c < COLUMNAS; c++) {
cursor(c, f);
color(laberinto[f][c]);
cout << (char)219;
}
}
}

void dibuja_personaje(int x, int y) {


color(14);
cursor(x, y); cout << "(O)";
cursor(x, y + 1); cout << "_|_";
cursor(x, y + 2); cout << " | ";
}

void borra_personaje(int x, int y) {


color(1);
cursor(x, y); cout << " ";
cursor(x, y + 1); cout << " ";
cursor(x, y + 2); cout << " ";
}

bool esValido(int x, int y, const int laberinto[FILAS][COLUMNAS]) {


return x >= 0 && x < COLUMNAS && y >= 0 && y < FILAS && laberinto[y][x] == 0;
}

bool encontrarCamino(int xInicio, int yInicio, int xDestino, int yDestino, const
int laberinto[FILAS][COLUMNAS]) {
int x = xInicio, y = yInicio;

while (x != xDestino || y != yDestino) {


dibuja_personaje(x, y);
_sleep(100);

if (xDestino > x && esValido(x + 1, y, laberinto)) {


borra_personaje(x, y);
x++;
}
else if (xDestino < x && esValido(x - 1, y, laberinto)) {
borra_personaje(x, y);
x--;
}
else if (yDestino > y && esValido(x, y + 1, laberinto)) {
borra_personaje(x, y);
y++;
}
else if (yDestino < y && esValido(x, y - 1, laberinto)) {
borra_personaje(x, y);
y--;
}
}

return true;
}

int main() {
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