-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovh.c
164 lines (125 loc) · 3.86 KB
/
movh.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <stdio.h>
#include <stdlib.h>
#include "client.h"
int main(){
FILE *mapa;
mapa= fopen("mapa1.txt", "r");
if(mapa==NULL){
printf("ERROR");
}
int i=0, j, vida=0, nl, nc, m, l, c, pos1, pos2;//vida é um booleano, enquanto for 0 o jogador esta vivo
int x, y; //posição do heroi no inicio do jogo
char mov; //movimentação do herói
char **linha;
fscanf(mapa,"%d %d %d", &nl, &nc, &m);
linha=(char**)malloc(nl*sizeof(char*));
if(linha==NULL){
printf("ERROR");
exit(1);
}
for(i=0; i<nl+1;i++){
linha[i]=(char*)malloc(nc*sizeof(char));
if(linha[i]==NULL){
printf("ERROR");
exit(1);
}
}
i=0;
while(fscanf(mapa," %[^\n]", linha[i])!=EOF){
printf("%s\n", linha[i]);
++i;
}
do {
srand( (unsigned)time(NULL) );
x= 2+ (rand()%(nl-5)); //pra não aparecer na borda
y= 2+ (rand()%(nc-5));
linha[x][y]='1';
} while(linha[x][y]=='m');
fclose(mapa);
for(i=0;i<nl;i++){ //posição do herói
for(j=0;j<nc;j++){
if(linha[i][j]=='1'){
l=i;
c=j;
}
}
}
for (i=0; i<nl; i++){ //localização do monstro usando matriz
for (j=0; j<nc; j++){
if (linha[i][j]=='m'){
pos1=i;
pos2=j;
}
}
}
int aux; //auxiliares
do{
mov= getch();
if(mov=='w' || mov=='W'){
if((linha[l-1][c]==' ' || linha[l-1][c]=='*') && (l<nl-1 && c<=nc-2 && c>=1) && l!=1){
linha[l][c]=' ';
linha[--l][c]='1';
}
}
else if(mov=='s'|| mov=='S'){
if((linha[l+1][c]==' ' || linha[l+1][c]=='*') && (l<nl-2 && c<=nc-2 && c>=1)){
linha[l][c]=' ';
linha[++l][c]='1';
}
}
else if(mov=='a'|| mov=='A'){
if((linha[l][c-1]==' ' || linha[l][c-1]=='*') && (l<nl-1 && c<=nc-2 && c>1)){
linha[l][c]=' ';
linha[l][--c]='1';
}
}
else if(mov=='d'|| mov=='D'){
if((linha[l][c+1]==' ' || linha[l][c+1]=='*') && (l<nl-1 && c<nc-2 && c>=1)){
linha[l][c]=' ';
linha[l][++c]='1';
}
//srand( (unsigned)time(NULL) );
aux = rand()%5;
if (aux==0){
if (linha[pos1-1][pos2]!='*' && linha[pos1-1][pos2]==' ')
{
linha[pos1][pos2]=' ';
linha[--pos1][pos2]='m';
}
}
else if (aux==1){
if (linha[pos1][pos2-1]!='*'&& linha[pos1][pos2-1]==' ')
{
linha[pos1][pos2]=' ';
linha[pos1][--pos2]='m';
}
}
else if (aux==2){
if (linha[pos1][pos2+1]!='*'&& linha[pos1][pos2+1]==' ')
{
linha[pos1][pos2]=' ';
linha[pos1][++pos2]='m';
}
}
else if (aux==3){
if (linha [pos1+1][pos2]!='*'&& linha[pos1+1][pos2]==' ')
{
linha[pos1][pos2]=' ';
linha[++pos1][pos2]='m';
}
}
//Delay de 1 minuto entre cada movimentação
usleep(2000);
/* auxiliar mod 0: anda pra cima
auxiliar mod 1: anda pra esquerda
auxiliar mod 2: anda pra direita
auxiliar mod 3: anda pra baixo
auxiliar mod 4: não faz movimento */
}
system("clear"); //comando limpar tela no windows, alterar para system("clear")
for(i=0;i<nl;i++){
printf("%s\n", linha[i]);
}
}while(vida==0);
return 0;
}