-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
330 lines (323 loc) · 9.48 KB
/
main.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/***
*
* --- RPG IP 2016 ---
* @descricao main.c é o arquivo principal do jogo, onde toda a mágica acontece!
* @desde 06/06/2016
* @autores
* [
* Bruno Melo
* Filipe Cumaru
* Rafael Santana
* Victor Aurélio
* ]
*
*
***/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <time.h>
#define CLS cls;
#if UNIX //Se estiver em um sistema linux
#define CLS clear
#endif
typedef struct{
char representacao;// A letra que vai representar o personagem no mapa
char nome[20]; //O Nome do personagem
int ataque, defesa, vida;// Variáveis que representam quanto de ataque, defesa ou vida o personagem possui
int x, y;// Coordenadas X e Y do personagem no mapa
}Personagem;
typedef struct{
int tamanho; // Tamanho de Linhas x Colunas (Nossa matriz será sempre quadrada)
char **mapa; // A matriz que representa o mapa
}Jogo;
/***
*
* Declarando cabeçalhos das funções
*
***/
char pegarOpcaoMenu();
void exibirMenu(char *arquivoDeMenu, char *ultimo, char *opcao);
void exibirArquivo(char *nomeDoArquivo);
void iniciarJogo(Personagem *heroi, Personagem *monstros, Jogo *atual);
void carregarJogo(Personagem *heroi, Personagem *monstros, Jogo *atual);
void criarJogo(Personagem *heroi, Personagem *monstros, Jogo *atual);
void alterarCaracterPrePalavra(char *texto, char *busca, char c);
void carregarMapaSalvoEmArquivo(Jogo *jogo, char *nomeArquivoMapa);
void carregarPersonagensSalvosEmArquivo(Personagem *heroi, Personagem *monstros);
void salvarMapaEmMapasSalvos(char *nomeDoMapa);
int main(int argc, char const *argv[]){
Personagem *heroi = (Personagem*) malloc(sizeof(Personagem));
Personagem *monstros = (Personagem*) malloc(sizeof(Personagem) * 4);
Jogo *jogoAtual = (Jogo*) malloc(sizeof(Jogo));
while(1){
switch(pegarOpcaoMenu("templates/tela_inicial")){// De acordo com a opção que o menu retornar
case 'W': //Caso seja selecionada a primeira opção do menu
iniciarJogo(heroi, monstros, jogoAtual);
break;
case 'S': //Caso seja selecionada a opção de baixo do menu (GAME OPTIONS)
switch(pegarOpcaoMenu("templates/game_options")){
case 'W': // CONFIGURAÇÕES DOS MAPAS
switch(pegarOpcaoMenu("templates/map_options")){
case 'W': //SELECIONAR UM MAPA
carregarMapasSalvos();
break;
case 'S': //CRIAR UM MAPA
system("cls");
char nome[200];
printf("digite o nome do mapa: ");
gets(nome);
salvarMapaEmMapasSalvos(nome);
break;
};
break;
case 'S': // CONFIGURAÇÕES DE PERSONAGENS
switch(pegarOpcaoMenu("templates/characters_options")){
case 'W': //SELECIONAR UM MAPA
break;
case 'S': //CRIAR UM MAPA
break;
};
}
break;
case 'H':
system("CLS");
system("color 0e");
exibirArquivo("templates/help");
system("pause");
};
}
return 0;
}
/***
*
* Esta retorna qual a opção escolhida pelo usuário no menu.
*
***/
char pegarOpcaoMenu(char *nomeArquivoDeMenu){
char ultimo='S', opcao='W';
do{
if(ultimo != opcao && (opcao == 'W' || opcao == 'S')){// Só vai redezenhar se ele realmente alternar entre as posições...
system("CLS");
exibirMenu(nomeArquivoDeMenu, &ultimo, &opcao);
}
}while((opcao = toupper(getch())) && opcao != 13 && opcao != 3 && opcao != 'H');
// OPCAO == 3 CTRL +c
// OPCAO == 13 ENTER
//Casos especiais, após o fim do menu, para sair do jogo ou para voltar a tela principal.
if(opcao == 3 && (strcmp(nomeArquivoDeMenu,"templates/tela_inicial") == 0 || strcmp(nomeArquivoDeMenu, "templates/game_over") == 0)){ //Ctrl+C pressionado
printf("Jogo finalizado!\n");
exit(1);
}else if(opcao == 3){
return 0;//vai retornar um case inválido (diferente de w, e de s).. fará com que retorne para o menu principal.
}else if(opcao == 'H'){
ultimo = 'H';
}
return ultimo;
}
/***
*
* Esta função exibe o menu e de acordo com a atual posição do cursor
* ela coloca '>' em cima ou em baixo do texto.
*
***/
void exibirMenu(char *nomeArquivoDeMenu, char *ultimo, char *opcao){
int i;
char texto[300], palavras[2][50];
FILE *file = fopen(nomeArquivoDeMenu, "r");
if(file == NULL){
#if _WIN32 //Se estiver em um sistema windows
printf("\a\a");/** Dois beep's para não encontrado, só funciona no windows. **/
#endif
printf("\n\tArquivo %s nao encontrado!\n", nomeArquivoDeMenu);
exit(1);
}else{
for(i=0; (!feof(file)); i++){
if(i==0){
//ANTES DE TUDO, vai escanear apenas as duas primeiras palavras do Arquivo!!
// PALAVRA 0 E PALAVRA 1 serão as palavras que representam as opções disponiveis no menu.
fscanf((FILE*) file, " %s", palavras[0]);
fscanf((FILE*) file, " %s", palavras[1]);
}else{
// As próximas linhas de leitura
//Temos as duas primeiras palavras escaneadas, sabemos diretamente do menu quem deve ser a opção de cima e a opção de baixo.
fgets(texto, 300, (FILE*) file);
if(*opcao == 'W'){
*ultimo = 'W';
alterarCaracterPrePalavra(texto, palavras[0], '>');
alterarCaracterPrePalavra(texto, palavras[1], ' ');
}else if(*opcao == 'S'){
*ultimo = 'S';
alterarCaracterPrePalavra(texto, palavras[0], ' ');
alterarCaracterPrePalavra(texto, palavras[1], '>');
}
printf("%s", texto);
}
}
}
fclose(file);
}
/***
*
* Esta função recebe um texto como parametro,
* busca uma determinada palavra e coloca um caracter duas posições antes da palavra.
*
***/
void alterarCaracterPrePalavra(char *texto, char *busca, char c){
char *ptr, *aux;
aux = strstr(texto,busca); // Procura por uma palavra no texto e retorna o endereço da posição do caracter 0 desta palavra.
if(aux != NULL){
ptr = aux-2; // Volta 2 caracteres antes do caracter de inicio da palavra.
*ptr = c;// Substitui o conteúdo pelo caracter C que foi passado como parametro
}
}
/***
*
* Esta função irá iniciar um jogo novo.
*
***/
void iniciarJogo(Personagem *heroi, Personagem *monstros, Jogo *atual){
}
/***
*
* Esta função irá criar um novo jogo, ou seja, novos mapas e personagens
*
***/
void criarJogo(Personagem *heroi, Personagem *monstros, Jogo *atual){
}
/***
*
* Essa função carrega os mapas salvos. Apartir do arquivo mapas.txt
*
***/
int carregarMapasSalvos(){
int i,mapaSelecionado;
char texto[300];
FILE *file = fopen("mapas.txt", "r");
if(file == NULL){
#if _WIN32 //Se estiver em um sistema windows
printf("\a\a");/** Dois beep's para não encontrado, só funciona no windows. **/
#endif
printf("\n\tArquivo de mapas nao encontrado!\n");
exit(1);
}else{
system("CLS");
printf("\n\nMapas encontrados na base de dados:\nCod:\tNome:\n");
for(i=0; (!feof(file)); i++){
fgets(texto, 300, (FILE*) file);
printf("%d\t %s", i+1,texto);
}
printf("\nPressione o codigo do mapa que deseja usar: ");
scanf("%d", &mapaSelecionado);
return mapaSelecionado-1;
}
}
/***
*
* Essa função carrega os mapas salvos. Apartir do arquivo mapas.txt
*
***/
void salvarMapaEmMapasSalvos(char *nomeDoMapa){
FILE *file = fopen("mapas.txt", "a");
if(file == NULL){
#if _WIN32 //Se estiver em um sistema windows
printf("\a\a");/** Dois beep's para não encontrado, só funciona no windows. **/
#endif
printf("\n\tArquivo de mapas nao encontrado!\n");
exit(1);
}else{
system("CLS");
//fputs("\n", (FILE*) file);
fputs(nomeDoMapa, (FILE*) file);
}
fclose(file);
}
/***
*
* Essa função carrega o mapa do arquivo de texto.
*
***/
void carregarMapaSalvoEmArquivo(Jogo *jogo, char *nomeArquivoMapa){
int i,nLinhas=1;
char texto[300];
FILE *file = fopen(nomeArquivoMapa, "r");
if(file == NULL){
#if _WIN32 //Se estiver em um sistema windows
printf("\a\a");/** Dois beep's para não encontrado, só funciona no windows. **/
#endif
printf("\n\tArquivo mapa nao encontrado!\n");
exit(1);
}else{
fscanf((FILE*) file, "%d", &nLinhas);
//printf("Numero de linhas: %d\n", nLinhas);
jogo->mapa = (char**) malloc(sizeof(char*) * nLinhas);
//para ignoranr o \n que vem após o número..
jogo->mapa[0] = (char*) malloc(sizeof(char) * 300);
fgets(jogo->mapa[0], 300, (FILE*) file);
for(i=0; i<nLinhas; i++){
jogo->mapa[i] = (char*) malloc(sizeof(char) * 300);
fgets(jogo->mapa[i], 300, (FILE*) file);
}
/*for(i=0; i<nLinhas; i++){
printf("| %s",jogo->mapa[i]);
}*/
}
}
/***
*
* Essa função carrega os personagens do arquivo binário.
*
***/
void carregarPersonagensSalvosEmArquivo(Personagem *heroi, Personagem *monstros){
FILE *file = fopen("binario.bin", "rb");
fread(heroi, sizeof(Personagem), 1, file);
printf("%s\n", heroi->nome);
}
/***
*
* Essa função carrega um arquivo de texto e exibe-o na tela.
*
***/
void exibirArquivo(char *nomeDoArquivo){
FILE *file;
file = fopen(nomeDoArquivo, "r");
if(file == NULL){
#if _WIN32 //Se estiver em um sistema windows
printf("\a\a");/** Dois beep's para não encontrado, só funciona no windows. **/
#endif
printf("\n\tArquivo %s nao encontrado!\n", nomeDoArquivo);
exit(1);
}else{
char texto[300];
while(!feof(file)){
fgets(texto, 300, (FILE*) file);
printf("%s", texto);
}
}
fclose(file);
}
/***
*
*
*
***/
int numeroDeLinhasArquivoTexto(char *nomeArquivo){
FILE *file = fopen(nomeArquivo, "r");
if(file == NULL){
#if _WIN32 //Se estiver em um sistema windows
printf("\a\a");/** Dois beep's para não encontrado, só funciona no windows. **/
#endif
printf("\n\tArquivo de mapas nao encontrado!\n");
exit(1);
}else{
system("CLS");
int i=0;
char texto[300];
for(i=0; (!feof(file)); i++){
fgets(texto, 300, (FILE*) file);
}
return i;
}
return -1;
}