1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
// Función moverse
#include <stdlib.h>
#include <iostream>
using namespace std;
// Declaración de función
void Moverse(int &fila, int &columna, int fil, int colum, const int norte, const int sur, const int oeste, const int este, char direccion, int token);
int main()
{
int fila = 5;
int columna = 5;
int fil = 1;
int colum = 1;
int token = 0;
const int norte = 1;
const int sur = (-1);
const int oeste = (-1);
const int este = 1;
char direccion;
Moverse(fila, columna, fil, colum, norte, sur, oeste, este, direccion, token);
cout << "jugador en " << fila << columna;
return 0;
}
// Definición de función
void Moverse(int &fila, int &columna, int fil, int colum, const int norte, const int sur, const int oeste, const int este, char direccion, int token)
{
cout << "¿En qué dirección te mueves? (n)orte, (s)ur, (e)ste, (o)este: ";
do
{
cin >> direccion;
switch(direccion)
{
case 'n': colum = (columna+norte);
if(colum<=5||colum>=1){
columna = colum;
token = 0;
}
else
{
cout << "¡Te has dado contra una pared!\n";
colum = (colum-norte);
token = 1;
}
break;
case 's': colum = (columna+sur);
if(colum<=5||colum>=1){
columna = colum;
token = 0;
}
else
{
cout << "¡Te has dado contra una pared!\n";
colum = (colum-norte);
token = 1;
}
break;
case 'e': fil = (fila+este);
if(fil<=5||fil>=1){
fila = fil;
token = 0;
}
else
{
cout << "¡Te has dado contra una pared!\n";
fil = (fil-este);
token = 1;
}
break;
case 'o': fil = (fila+oeste);
if(fil<=5||fil>=1){
fila = fil;
token = 0;
}
else
{
cout << "¡Te has dado contra una pared!\n";
fil = (fil-este);
token = 1;
}
break;
default: cout << "Error: Debe introducir una dirección válida. Vuelva a elegir.\n";
}
}
while(token==1);
}
|