-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path09-Estructuras-dinámicas.c
135 lines (116 loc) · 3.05 KB
/
09-Estructuras-dinámicas.c
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//Hacer un ejemplo de batallas por turnos entre un heroe y varios malos, los malos se incrementan
//en 1 cuando el contador del turno de la batalla sea par
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
typedef struct {
int salud;
int mana;
int fuerza;
int magia;
} heroe;
typedef struct {
int salud;
int fuerza;
} malo;
heroe yo;
malo *otros;
void introduceyo();
void introducemalo(int N);
void asignamen(int *N);
int main(int argc, char **argv)
{
int op, i, pegar, objetivo;
int cont = 0;
int N = 0;
srand(time(NULL));
introduceyo();
asignamen(&N);
do{
do{
printf("(1) Atacar \n");
printf("(2) Magia\n");
scanf("%i", &op);
}while (op < 1 || op > 2);
printf("Elige el malo al que atacar\n");
for (i = 0; i < N; i++)
{
printf("Malo %i tiene una vida de: %i \n",i, otros[i].salud);
}
scanf("%i", &objetivo);
if (otros[objetivo].salud > 0)
{
switch(op)
{
case 1:
pegar = yo.fuerza;
printf("He pegado %i\n", pegar);
otros[objetivo].salud -= pegar;
printf("Salud del malo %i es de %i\n", objetivo, otros[objetivo].salud);
break;
case 2:
if (yo.mana > 0)
{
pegar = yo.magia * (rand() % 3);
printf("He pegado %i\n", pegar);
otros[objetivo].salud -= pegar;
printf("Salud del malo %i es de %i\n", objetivo, otros[objetivo].salud);
}
else
{
printf("No tienes mana\n");
}
break;
}
}
else
{
printf("Deja a los muertos en paz\n");
}
printf("Turno de los malos\n");
for (i = 0; i < N; i++)
{
pegar = otros[i].fuerza * (rand() % 3);
printf("El malo %i me ha pegado %i\n", i, pegar);
yo.salud -= pegar;
printf("Salud del heroe: %i\n", yo.salud);
}
if ((cont % 2) == 0)
{
otros = (malo*) realloc(otros, (N + 1) * sizeof(malo));
if (otros == NULL) {
printf("No hay memoria suficiente\n");
exit(1);
}
introducemalo(N);
N++;
}
cont++;
}while (yo.salud > 0);
printf("Has muerto\n");
return 0;
}
void introduceyo()
{
yo.salud = 1000;
yo.mana = 100;
yo.fuerza = 50;
yo.magia = 100;
}
void introducemalo(int N)
{
otros[N].salud = 100;
otros[N].fuerza = 50;
}
void asignamen(int *N)
{
otros = (malo *)malloc(*N * sizeof(malo));
if (otros == NULL) {
printf("No hay memoria suficiente\n");
exit(1);
}
introducemalo(*N);
(*N)++;
}