Jesús Romero Rivera
Juan Castro
Profesor:open_mouth::ok_hand::clap:
Krish Campos
do while
Bienvenidos, en este tercer reto tienen que lograr que su personaje se mueva en las direcciones izquierda, derecha, arriba y abajo. En la clase anterior ya vimos como leer la entrada de datos usando CIN e interpretarla con condicionales para mover a mi personaje en dos direcciones (izquierda y derecha), tienen que hacer lo mismo para moverlo arriba y abajo. Tip: Al igual que sumamos y restamos una variable que representa el movimiento lateral, tenemos que crear otra que represente el movimiento vertical, manipularla y pasársela como parámetro al dibujado del mapa.
Jesús Romero Rivera
Juan Castro
Profesor:open_mouth::ok_hand::clap:
Krish Campos
do while
<code> #include <iostream> using namespace std; void drawMap(int heroPositionRow,int heroPositionColumn, char gameMap[5][5]) { for(int row = 0; row < 5; row++)//loop for Row { for(int column = 0; column < 5; column ++)//loop for Column { if( row != heroPositionRow) { cout<< gameMap[row][column]; }else if( column != heroPositionColumn) { cout<< gameMap[row][column]; }else { cout<<'H'; } } cout<<endl; } } int main() { int heroPositionRow = 0, heroPositionColumn = 0; char input = ' '; bool isGameOver = false; char gameMap [5][5] = { {'1','1','1','1','1'}, {'1','1','1','1','1'}, {'1','1','1','1','1'}, {'1','1','1','1','1'}, {'1','1','1','1','1'} }; drawMap(heroPositionRow,heroPositionColumn ,gameMap); while(isGameOver == false) { cout<<"Direccion: ";cin>>input; switch (input) { /* Cada condional se usa para que al mover de posicion este no se salga del mapa. */ case 'd'://Movimiento a la derecha. if(heroPositionColumn < 4 ) { heroPositionColumn += 1; } break; case 'i'://Movimineto a la izquierda. if(heroPositionColumn == 0) { heroPositionColumn -= 0; }else { heroPositionColumn -= 1; } break; case 'a'://Movimineto a la arriba. if(heroPositionRow == 0) { heroPositionRow -= 0; }else { heroPositionRow -= 1; } break; case 'b'://Movimineto a la abajo. if(heroPositionRow < 4 ) { heroPositionRow += 1; } break; default://Cualquier respueta diferente, finaliza el juego. isGameOver = true; break; } drawMap(heroPositionRow, heroPositionColumn,gameMap); } return 0; }
Diego Andres Cabrera Rojas
Andres Santana
Sergio Javier Lopez Olivera
Saludos Andrés, sé que han pasado más de 3 años, pero aquí está mi respuesta...
Depende...
Como programador te enfrentarás a un sin fin de situaciones donde parece que los apuntadores resuelven todo el problema. Sin embargo lo que definirías como algo excesivo es viéndolo desde la perspectiva de otro prorgamador. . ¿Si tu fueras otro programador, podrías entender tu código con el uso actual que tienes de apuntadores? . Si la respuesta es afirmativa, entonces úsalos inteligentemente y como puedas, si la respuesta es no, entonces considera opciones menos dinámicas pero donde puedes tener mayor control sobre las variables y estructuras que estás manejando. . A fin de cuentas, una funcionalidad, característica o elemento del lenguaje es buena si es útil resolviendo el problema que tienes entre manos, ya todo lo demás serían aditivos o cosas que no tienen importancia para realizar la tarea que tienes frente a ti.
Jesus Ivan Villalobos de la Cruz
Wlly Anderson Samata Ccoya
me ayudò bastante
Manuel Durán
Manuel Alejandro Aguilar Téllez Girón
Rafael Sandoval Chavarría
Christian Alexis Serrato Guerrero
Johan Giuseppe Mateus Cifuentes
Edward Fernandez
marco antonio cespedes mamani
Manuel Durán
esta bueno, lo probe y esta muy bien que no se salga del area, ademas de que esta excelente el toque de crear las dimenciones del area, felicitaciones.
Jesús Everardo Bermúdez Rodríguez
Carlos Nassif Trejo Garcia
Alejandro Urrea Giraldo
Oscar Arbey Junca
Sebastian Andree Lopera Quevedo
Fernando Jesús Paredes Rios
David Lara
Carlos Pavajeau
Acá dejo mi solución al reto. Si se sale del mapa, saldrá del otro lado
#include <iostream> #include <conio.h> using namespace std; void drawMap(int heroPosX, int heroPosY, char gameMap[5][5]) { for (int i=0; i<5;i++) { for(int p=0; p<5; p++) { if(i != heroPosY ) { cout << gameMap[p][i]; }else { if (p != heroPosX) { cout << gameMap[p][i]; }else { cout << 'X'; } } } cout << endl; } } int main() { setlocale(LC_ALL, ""); char gameMap[5][5] = { {'o','o','o','o','o'}, {'o','o','o','o','o'}, {'o','o','o','o','o'}, {'o','o','o','o','o'}, {'o','o','o','o','o'} }; int heroPosX = 1; int heroPosY = 1; bool isGameOver = false; char input; drawMap(heroPosX, heroPosY, gameMap); while (!isGameOver) { cin >> input; if(input == 'd') { heroPosX += 1; } if (input == 'a') { heroPosX -= 1; } if (input == 'w') { heroPosY -=1; } if (input == 's') { heroPosY +=1; } if (input == 'p') { isGameOver = true; } if(heroPosX == 5) { heroPosX = 0; } if(heroPosY == 5) { heroPosY = 0; } if(heroPosX == -1) { heroPosX = 4; } if(heroPosY == -1) { heroPosY = 4; } drawMap(heroPosX, heroPosY, gameMap); } _getch(); return 0; }
Reto Cumplido
#include <iostream> using namespace std; void DrawMap(int HeroPosX ,int HeroPosY, char GameMap[5][5]){ for(int i = 0 ; i < 5 ; i++){ for(int j = 0; j < 5 ; j++){ if( i != HeroPosY || j!= HeroPosX ){ cout << GameMap[i][j]; }else{ cout << '6'; } } cout << "\n"; } } int main() { bool GameOver = false; int HeroPosX = 0; int HeroPosY = 0; char GameMap [5][5] = { {'#','#','#','#','#'}, {'#','#','#','#','#'}, {'#','#','#','#','#'}, {'#','#','#','#','#'}, {'#','#','#','#','#'}, }; DrawMap(HeroPosX,HeroPosY,GameMap); char Input = ' '; do{ cin >> Input; switch(Input){ case 'd' :{ HeroPosX += 1; break; } case 'a' :{ HeroPosX -= 1; break; } case 'w' :{ HeroPosY -= 1; } case 's' :{ HeroPosY += 1; } } if(HeroPosX == 5 ){ HeroPosX = 0; } else if(HeroPosX == -1){ HeroPosX = 4; } else if(HeroPosY == 5){ HeroPosY = 0; } else if(HeroPosY == -1){ HeroPosY = 4; } DrawMap(HeroPosX,HeroPosY,GameMap); }while(!GameOver); return 0; }
Buen día. Mi pregunta es: ¿Hasta qué punto es ideal el uso de los apuntadores? hice mi código en base a eso y me gustó, me ayudo a comprender un poquito más este tema, sin embargo, me queda la duda en que momento sería un: "no es necesario un apuntador" o el uso excesivo de estos empiece afectar mi aplicación.
#include <iostream> void DrawWorld(char gameMap[5][5], int *heroPosX, int *heroPosY) { for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { if (*heroPosY == i) if (*heroPosX == j) std::cout << 'H'; else std::cout << gameMap[i][j]; else std::cout << gameMap[i][j]; } std::cout << "\n"; } } void CalculateHeroPos(char *input, int *currentHeroPosX, int *currentHeroPosY) { switch (*input) { case 'a': --*currentHeroPosX; break; case 'd': ++*currentHeroPosX; break; case 'w': --*currentHeroPosY; break; case 's': ++*currentHeroPosY; break; case 'q': break; default: std::cout << "invalid command! :( \n"; break; } } int main() { char gameMap[5][5] = { {'1', '1', '1', '1', '1'}, {'1', '1', '1', '1', '1'}, {'1', '1', '1', '1', '1'}, {'1', '1', '1', '1', '1'}, {'1', '1', '1', '1', '1'}}; int heroPosX = 3; int heroPosY = 1; char input = ' '; bool isGameOver = false; while (!isGameOver) { if (input == 'q') { isGameOver = true; std::cout << "Good bye! :) \n"; continue; } DrawWorld(gameMap, &heroPosX, &heroPosY); std::cin >> input; CalculateHeroPos(&input, &heroPosX, &heroPosY); } return 0; }
Bueno esta sencillo y no se me ocurrio como usar los arrays para hacerlo en menos lineas, ademas de que me encontre de que la libreria iostream solo trae 4 funciones super basicas. Intente instalar otras librerias como ncurses pero se me hizo un chungo y no queria quedarme pegado con el reto, asi que alli va mi esperpento... salu2
#include <iostream> #include <stdlib.h> using namespace std; int fillMap(int xmap, int ymap, int xchar, int ychar) { for(int i=0; i<ymap; i++) { cout << endl; for(int j=0; j<xmap; j++) { if(xchar == i && ychar == j) { cout << "O"; } else { cout << " "; } } } } int main() { char gameInput; int xmap = 9; int ymap = 9; int xchar = 2; int ychar = 2; //char map [xmap][ymap]; cout << "bienvenido, presiona a,s,d,w + enter para mover tu personaje" << endl << "presiona q + enter para salir" << endl; while (gameInput != 'q') { fillMap(xmap, ymap, xchar, ychar); cin >> gameInput; if (gameInput == 'a') { /* mueve personaje a la izquierda */ ychar = ychar - 1; } else if (gameInput == 'd') { /* mueve personaje a la derecha */ ychar = ychar + 1; } else if (gameInput == 's') { /* mueve personaje hacia abajo */ xchar = xchar + 1; } else if (gameInput == 'w') { /* mueve personaje hacia abajo */ xchar = xchar - 1; } else { if (gameInput != 'q') { cout << "tonto, te dije que ingreses a,s,d,w + enter para mover tu personaje" << endl << "y presiona q + enter para salir" << endl; } } } return 0; }
#include <iostream> #include <stdlib.h> using namespace std; void print_menu(){ cout << "Welcome to my liberynth" << endl; cout << "Here's te controls" << endl; cout << "[A] to move left" << endl; cout << "[D] to move right" << endl; cout << "[W] to move up" << endl; cout << "[S] to move down" << endl; cout << "[P] to end game" << endl; cout << "Every move press enter" << endl; cout << "Tap any button to start" << endl; } void draw_map(int HeroPosition[2][1], string map[11][15]) { for(int i = 0; i < 11 ; i++){ for(int j = 0; j < 15; j++){ if (i != HeroPosition[0][0] /* && j != HeroPosition[0][1] */){ cout << map[i][j]; } else { if (j != HeroPosition[0][1]){ cout << map[i][j]; }else{ cout << " H "; } } } cout << ' ' << endl; } } int change_position(char move, int HeroPosition[2][1], bool* endGame){ //bool localEndGame = *endGame; switch (move) { case 'a': HeroPosition[0][1]--; break; case 'd': HeroPosition[0][1]++; break; case 's': HeroPosition[0][0]++; break; case 'w': HeroPosition[0][0]--; break; case 'p': *endGame = true; break; } return HeroPosition[2][1]; } int main(){ char move; int HeroPosition[2][1] = { {1}, {13} }; string map[11][15] = { {"||", "=====", "=====", "=====", "=====", "=====", "=====", "=====", "=====", "=====", "=====", "=====", "=====", "=====", "||"}, {"||", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "||"}, {"||", " ", "=====", " ", "=====", "=====", "=====", " ", "=====", "=====", "=====", "=====", "=====", "=====", "||"}, {"||", " ", "=====", " ", "=====", "=====", "=====", " ", "=====", "=====", "=====", "=====", "=====", " ", "||"}, {"||", " ", "=====", " ", "=====", "=====", "=====", " ", " ", " ", " * ", "=====", " ", " * ", "||"}, {"||", " ", "=====", " * ", "=====", "=====", "=====", " * ", "=====", "=====", "=====", "=====", "=====", "=====", "||"}, {"||", " ", "=====", "=====", "=====", "=====", "=====", "=====", "=====", "=====", "=====", "=====", "=====", "=====", "||"}, {"||", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " S ", "||"}, {"||", "=====", " ", "=====", "=====", "=====", "=====", " ", "=====", "=====", "=====", "=====", "=====", "=====", "||"}, {"||", " * ", " ", " * ", "=====", "=====", "=====", " ", " ", " ", " ", " ", " ", " * ", "||"}, {"||", "=====", "=====", "=====", "=====", "=====", "=====", "=====", "=====", "=====", "=====", "=====", "=====", "=====", "||"} }; bool endGame = false; print_menu(); do { cout << ' ' << endl; cin >> move; system("clear"); HeroPosition[2][1] = change_position(move, HeroPosition, &endGame); if (HeroPosition[0][0] == 0) HeroPosition[0][0]++; else if (HeroPosition[0][0] == 10) HeroPosition[0][0]--; else if (HeroPosition[1][0] == 0) HeroPosition[1][0]++; else if (HeroPosition[1][0] == 14) HeroPosition[1][0]--; draw_map(HeroPosition, map); } while(!endGame); }
Se borra en cada input.
Mi versión:
\#include\<iostream> using *namespace* <u>std</u>; *void* DrawMap(*int* *HeroPosX*, *int* *HeroPosY*, *char* *GameMap*\[5]\[5]) { *int* i, j;   for(j = 0; j \< 5; j++) { for(i = 0; i \< 5; i++) {   if(i != *HeroPosX*) { cout \<\< *GameMap*\[j]\[i]; }  else  { if(j != *HeroPosY*) { cout \<\< *GameMap*\[j]\[i]; } else { cout \<\< 'H'; } } } cout \<\< endl; } } *int* main() { *int* posicionHeroeX = 1; *int* posicionHeroeY = 1; *bool* GameOver = false; *char* input = ' '; *char* map\[5]\[5] =  { {'1', '1', '1', '1', '1'}, {'1', '1', '1', '1', '1'}, {'1', '1', '1', '1', '1'}, {'1', '1', '1', '1', '1'}, {'1', '1', '1', '1', '1'} };   DrawMap(posicionHeroeX, posicionHeroeY, map); while(GameOver == false) { cin >> input; if(input == 'd') { posicionHeroeX++; }  else if(input == 'a') { posicionHeroeX--; } else if(input == 'w') { posicionHeroeY--; } else if(input == 's') { posicionHeroeY++; } else if(input == 'p') { GameOver = true; } DrawMap(posicionHeroeX, posicionHeroeY, map);   } return 0; }
Game:
#include <iostream> using namespace std; void drawMaps(int HeroPositionX, int HeroPositionY, char map[5][5]) { for (int i = 0; i < 5; i = i + 1) { for(int j = 0; j < 5; j = j + 1){ if(i != HeroPositionY){ cout << map[j][i]; } else { if ( j != HeroPositionX){ cout << map[j][i]; } else { cout << 'H'; } } } cout << endl; } cout << endl; } int main() { int HeroPositionX = 0; int HeroPositionY = 0; char map[5][5] = { {'1','1','1','1','1'}, {'1','1','1','1','1'}, {'1','1','1','1','1'}, {'1','1','1','1','1'}, {'1','1','1','1','1'}, }; char Input = ' '; bool isGameOver = false; drawMaps(HeroPositionX, HeroPositionY, map); while(isGameOver == false){ cin >> Input; if (Input == 'd'){ HeroPositionX = HeroPositionX + 1; } else if (Input == 'a') { HeroPositionX = HeroPositionX - 1; } else if (Input == 'w') { HeroPositionY = HeroPositionY - 1; } else if (Input == 's'){ HeroPositionY = HeroPositionY + 1; } else if (Input == 'p') { isGameOver = true; } drawMaps(HeroPositionX, HeroPositionY, map); } return 0; }
#include <iostream> using namespace std; void DrawMap(int HeroPosX, int HeroPosY, char GameMap[5][5]) { system("CLS"); for (int i = 0; i < 5; i = i + 1) { for (int j = 0; j < 5; j = j + 1) { if (i != HeroPosY) { cout << GameMap[j][i]; } else { if (j != HeroPosX) { cout << GameMap[j][i]; } else { cout << "H"; } } } cout << endl; } } int main() { int HeroPosX = 1; int HeroPosY = 1; bool isGameOver = false; char Input = ' '; char GameMap[5][5] = { {'1', '1', '1', '1', '1'}, {'1', '1', '1', '1', '1'}, {'1', '1', '1', '1', '1'}, {'1', '1', '1', '1', '1'}, {'1', '1', '1', '1', '1'}, }; DrawMap(HeroPosX, HeroPosY, GameMap); while (isGameOver == false) { cin >> Input; if (Input == 'd') { HeroPosX += 1; } else if (Input == 'a') { HeroPosX -= 1; } else if (Input == 'w') { HeroPosY -= 1; } else if (Input == 's') { HeroPosY += 1; } else if (Input == 'f') { isGameOver = true; } DrawMap(HeroPosX, HeroPosY, GameMap); } return 0; }```
¡Hola a todos!
Les dejo mi propuesta para el reto, Le añadí las colisiones al jugador.
#include <iostream> using namespace std; /* @description: Draw the map with the player's current position in the screen. @param playerPosX: Current player's position on x axis. @param playerPosY: Current player's position on y axis. */ void DrawMap(int playerPosX, int playerPosY) { cout << endl; for (int j=0; j<=6; j++) { for (int i=0; i<=6; i++){ // Add the map's edges and the left/right border. if (i==0 || i==6) { if (j==0 || j==6) cout << "*"; else cout << "|"; } // Add the top/bottom border. else if (j==0 || j==6) cout << "-"; else { // Draw the rest of the map. if (i == playerPosX and j == playerPosY) cout << "P"; else cout << "."; } } cout << endl; } cout << endl << endl; } int main(){ int playerPosX = 1; int playerPosY = 1; char input = ' '; bool gameOver = false; cout << "Bienvenido jugador" << endl; while (!gameOver) { cout << "¿Qué quieres hacer?"; cout << "Moverte a la izquierda (a) o moverte a la derecha (d): "; cin >> input; if (input == 'd' and playerPosX < 5) playerPosX += 1; else if (input == 'a' and playerPosX > 1) playerPosX -= 1; else if (input == 'w' and playerPosY > 1) playerPosY -= 1; else if (input == 's' and playerPosY < 5) playerPosY += 1; else if (input == 'p') gameOver = true; DrawMap(playerPosX, playerPosY); cout << endl; } }
agregue el detalle de introducir limites...eje x eje y
#include <iostream> using namespace std; void DrawGame(int PosHeroX, int PosHeroY, int X, int Y) { for(int i=0; i<Y; i++) { for(int j=0; j<X; j++) { if(i != PosHeroY) { cout << "-"; } else { if(j != PosHeroX) { cout << "-"; } else { cout << 'H'; } } } cout << endl; } } int main() { int HeroPosX = 1; int HeroPosY = 1; bool IsGameOver = false; char Input = ' '; int limitY = 0; int limitX = 0; int limitC = 0; cout << "Dibuja un mapa ingresando sus limites..." << endl; cout << "limite eje x..."; cin >> limitX; cout << "limite eje y..."; cin >> limitY; DrawGame(HeroPosX, HeroPosY, limitX, limitY); while(IsGameOver == false) { cin >> Input; switch(Input) { case 'd': if(HeroPosX != limitX-1) { HeroPosX++; } else { HeroPosX; } break; case 'a': if(HeroPosX != limitC) { HeroPosX--; } else { HeroPosX; } break; case 's': if(HeroPosY != limitY-1) { HeroPosY++; } else { HeroPosY; } break; case 'w': if(HeroPosY != limitC) { HeroPosY--; } else { HeroPosY; } break; default: IsGameOver = true; break; } DrawGame(HeroPosX, HeroPosY, limitX, limitY); } return 0; }
#include <iostream> #include <windows.h> using namespace std; void drawMap(char gameZone[5][5],int x,int y){ system("cls"); for(int i = 0; i < 5; i++){ for(int j = 0; j < 5; j++){ if(y == i && x == j){ gameZone[i][j] = 'h'; cout << gameZone[i][j]; } else{ gameZone[i][j] = '.'; cout << gameZone[i][j]; } } cout << endl; } } int main() { char gameZone[5][5]; int x = 0,y = 0; bool isGameOver = false; char movement = ' '; drawMap(gameZone,x,y); while(!isGameOver) { cin >> movement; switch(movement){ case 'a': x--; if(x < 0) x++; break; case 'd': x++; if(x > 4) x--; break; case 'w': y--; if(y < 0) y++; break; case 's': y++; if(y > 4) y--; break; case 'p': isGameOver = true; break; default: cout << "Opcion Invalida" << endl; break; } drawMap(gameZone,x,y); } return 0; }```
#include <iostream> #include <array> using namespace std; // Recorriendo un arreglo y dibujandolo void drawMap(char gameMap[][5], int mapSize, int x, int y) { for ( int i = 0; i < mapSize; i++ ) { cout << endl; for ( int j = 0; j < mapSize; j++ ) { // Dibujar mapa o jugador if ( ( i == y ) && ( x == j ) ){ cout << 'H'; } else { cout << gameMap[i][j]; } } } } // Rellenando un arreglo void fillMap( char gameMap[][5], int mapSize ) { const char EMPTY = '1'; // constante de lugar vacio // Recorriendo el arreglo y rellenando for ( int i = 0; i < mapSize; i++ ) { for ( int j = 0; j < mapSize; j++ ) { gameMap[i][j] = EMPTY; } } } int main() { // Constant const int mapSize = 5; // Mapa y coordenadas de jugador char gameMap[mapSize][mapSize]; int heroPosX = 1; int heroPosY = 1; // track de seguir jugando bool keepPlaying = true; // Entrada de jugador y rellenando mapa char input = ' '; fillMap( gameMap, mapSize ); // TMover al jugador por el mapa acorde a la entrada del usuario while ( keepPlaying ) { // Dibujando mapa drawMap( gameMap, mapSize, heroPosX, heroPosY ); // Tomando input del usuario cin >> input; // Cambiando la posicion del jugador dependiendo del input // Posicion en X if ( input == 'd' ) { heroPosX++; } else if ( input == 'a' ) { heroPosX--; } // Posicion en Y else if ( input == 'w' ) { heroPosY--; } else if ( input == 's' ) { heroPosY++; } // GameOver else if ( input == 'p' ) { keepPlaying = false; } } return 0; }
Ejercicio resuelto!
#include <iostream> using namespace std; void DrawMap(int hPos[2], char gMap[4][5]){ for(int i=0;i<4;i++){ for(int j=0;j<5;j++){ if(j!=hPos[0]-1){ cout<<gMap[i][j]<<" "; }else{ if(i!=hPos[1]-1){ cout<<gMap[i][j]<<" "; }else{ cout<<"X "; } } } cout<<endl; } } int main() { bool IsGameOver=false; char pMove=' '; int heroPos[2]={1,1}; char Gamemap [4][5]{ {'1','1','1','1','1'}, {'1','1','1','1','1'}, {'1','1','1','1','1'}, {'1','1','1','1','1'} }; while(IsGameOver==false){ DrawMap(heroPos,Gamemap); cout<<" "; cin>>pMove; switch(pMove){ case 'a': if(heroPos[0]!=1){ heroPos[0]--; } break; case 'd': if(heroPos[0]!=5){ heroPos[0]++; } break; case 'w': if(heroPos[1]!=1){ heroPos[1]--; } break; case 's': if(heroPos[1]!=4){ heroPos[1]++; } break; case 'x': IsGameOver=true; break; } } return 0; }
Buenas noches aqui dejo mi solucion al reto
#include <iostream> using namespace std; void DrawMap ( int HeroPosX , int HeroPosY , char GameMap[5][5]) { for (int Columnas = 0; Columnas < 5; Columnas++) { for ( int Filas = 0; Filas < 5; Filas ++) { if (Columnas != HeroPosY) { cout <<GameMap[Filas][Columnas]; } else { if (Filas != HeroPosX) { cout <<GameMap[Filas][Columnas]; } else { cout << 'X'; } } } cout<<endl; } } int main() { int HeroPosX = 1; int HeroPosY = 1; bool IsGameOver = false; char Input = ' '; char GameMap[5][5] = { {'0','0','0','0','0'}, {'0','0','0','0','0'}, {'0','0','0','0','0'}, {'0','0','0','0','0'}, {'0','0','0','0','0'}, }; DrawMap(HeroPosX, HeroPosY ,GameMap); while (IsGameOver == false) { cin >> Input ; if (Input == 'd' | Input =='D') { HeroPosX = HeroPosX + 1; } else if (Input == 'a' | Input =='A') { HeroPosX = HeroPosX -1; } else if (Input == 'w' | Input =='W') { //HeroPosX =HeroPosX; HeroPosY = HeroPosY - 1; } else if (Input == 's' | Input =='S') { //HeroPosX =HeroPosX; HeroPosY = HeroPosY + 1; } else if (Input == 'p' | Input== 'P') { IsGameOver = true; } DrawMap(HeroPosX, HeroPosY ,GameMap); } return 0; } ```Muchas gracias
#include <iostream> using namespace std; void drawMap(int heroPosV, int heroPosH, char gameMap[4][5]) { for(int j = 0; j < 4; j++) { for(int i = 0; i < 5; i++) { if(j != heroPosV || i != heroPosH) { cout << gameMap[j][i]; } else { cout << 'H'; } } cout << endl; } } int main() { int posicionH = 2; int posicionV = 0; char mapa[4][5] = { {'0', '0', '0', '0', '0'}, {'0', '0', '0', '0', '0'}, {'0', '0', '0', '0', '0'}, {'0', '0', '0', '0', '0'} }; bool gameOver = false; drawMap(posicionV, posicionH, mapa); char input; while(!gameOver) { cin >> input; switch(input) { case 'd': posicionH ++; break; case 'a': posicionH --; break; case 'w': posicionV --; break; case 's': posicionV ++; break; case 'e': gameOver = true; break; default: cout << "Ja?" << endl; break; } drawMap(posicionV, posicionH, mapa); } return 0; }
void DrawMap(int HeroPosX, int HeroPosY, char GameMap[5][5]) { for (int i = 0; i < 5; i = i + 1) { for (int j = 0; j < 5; j++) { if (i != HeroPosY){ cout << GameMap[j][i]; }else{ if (j != HeroPosX) { cout << GameMap[j][i]; } else { cout << 'H'; } } } cout << endl; } } int main() { int HeroPosX = 1; int HeroPosY = 1; bool IsGameOver = false; char Input = ' '; char GameMap[5][5] = { {'1', '1', '1', '1', '1'}, {'1', '1', '1', '1', '1'}, {'1', '1', '1', '1', '1'}, {'1', '1', '1', '1', '1'}, {'1', '1', '1', '1', '1'} }; DrawMap(HeroPosX, HeroPosY, GameMap); while (IsGameOver == false) { cin >> Input; if (Input == 'd') { HeroPosX = HeroPosX + 1; }else if (Input == 'a') { HeroPosX = HeroPosX - 1; }else if (Input == 'p') { IsGameOver = true; }else if (Input == 'w') { HeroPosY = HeroPosY - 1; }else if (Input == 's') { HeroPosY = HeroPosY + 1; } DrawMap(HeroPosX, HeroPosY, GameMap); } return 0; }```
Les dejo mi versión:
#include <iostream> using namespace std; void drawMap(int heroPosX, int heroPosY, char gameMap[5][5]) { for(int i = 0; i < 5; i++) //for columnas { for(int p = 0; p < 5; p++) //for filas { if(i != heroPosY) { cout << gameMap[p][i]; }else { if(p != heroPosX) { cout << gameMap[p][i]; } else { cout << 'H'; } } } cout << endl; } } int main() { int heroPosX = 1; int heroPosY = 1; bool isGameOver = false; char input = ' '; char gameMap[5][5] = { {'1', '1','1','1','1'}, {'1', '1','1','1','1'}, {'1', '1','1','1','1'}, {'1', '1','1','1','1'}, {'1', '1','1','1','1'} }; drawMap(heroPosX, heroPosY, gameMap); while(isGameOver == false) { cin >> input; if(input == 'd') { heroPosX++; }else if(input == 'a') { heroPosX--; } else if(input == 'w') { heroPosY--; }else if(input == 's') { heroPosY++; } else if (input == 'p') { isGameOver = true; } drawMap(heroPosX, heroPosY, gameMap); } return 0; }```
#include <iostream> void draw_map(char game_map[5][5], int heroPositionX, int heroPositionY) { for (int i = 0; i < 5; ++i) { for (int j = 0; j < 5; ++j) { if (i == heroPositionY && j == heroPositionX) { std::cout << 'H'; } else { std::cout << game_map[i][j]; } } std::cout << std::endl; } } int main() { int heroPositionX = 0; int heroPositionY = 1; char key = '\000'; bool gameOver = false; char game_map[5][5] = {{'1', '1', '1', '1', '1'}, {'1', '1', '1', '1', '1'}, {'1', '1', '1', '1', '1'}, {'1', '1', '1', '1', '1'}, {'1', '1', '1', '1', '1'}}; draw_map(game_map, heroPositionX, heroPositionY); while (!gameOver) { std::cin >> key; if (key == 'd') { ++heroPositionX; } else if (key == 'a') { --heroPositionX; } else if (key == 'w') { --heroPositionY; } else if (key == 's') { ++heroPositionY; } else if (key == 'p') { gameOver = true; } draw_map(game_map, heroPositionX, heroPositionY); } return 0; }