-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
148 lines (108 loc) · 3.1 KB
/
main.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "MyUtil.h"
#include "Myfilelib.h"
#define FILENAME "random_numbers.bin"
#define NUM_COUNT 100
#define SAVEFILE "save.bin"
#define USERSFILE "users.bin"
#define IDMIN 1000
#define IDMAX 9999
void playGame(int jogId) {
int score = 0;
int currentNumber, nextNumber;
char userChoice;
char aux[5];
scanf("%s", &aux);
Log* log = (Log*)malloc(sizeof(Log));
// Generate the first random number
currentNumber = randRange(1, 20);
do {
cls(0);
printf("Numero atual: %d\n", currentNumber);
printf("Escolha (z - menor / x - maior): ");
scanf(" %c", &userChoice);
// Generate the next random number
nextNumber = randRange(1, 20);
// Compare the user's choice with the next number
if ((userChoice == 'z' && nextNumber < currentNumber) ||
(userChoice == 'x' && nextNumber > currentNumber)) {
printf("Parabens! Voce acertou.\n");
score++;
} else {
printf("Resposta incorreta. Fim do jogo!\n");
break;
}
currentNumber = nextNumber;
sleep(1);
ln(); // Break two lines
} while (userChoice != '0');
printf("Pontuacao: %d\n", score);
viewLeaderboard();
printf("Fim do jogo!\n");
}
void viewLog() {
// Code for viewing the log
}
void viewLeaderboard() {
// Code for viewing the leaderboard
}
void starGame() {
int id;
char* nome = (char*)malloc(sizeof(char)*5);
FILE* fp = fopen(USERSFILE, "rb");
printf("Nome:");
scanf("%d",&nome);
capStr(&nome);
id = nomeToId(fp, nome);
if(id == -1){ //Nome nao encontrado no banco de dados
while(!isIdUnique(fp, id)){
id = randRange(IDMIN, IDMAX);
}
fclose(fp);
fp = fopen(USERSFILE, "ab");
loadJog(fp, id, nome);
fclose(fp);
}
playGame(id);
}
int main() {
FILE* fp = fopen(SAVEFILE, "wb");
if (!fp) {
printf("Error opening the save file.\n");
return 1;
}
// Perform any necessary file operations
fclose(fp);
int choice;
do {
printf("Menu:\n");
printf("1 - Jogar\n");
printf("2 - Ver log\n");
printf("3 - Leaderboard\n");
printf("4 - Sair\n");
printf("Escolha uma opcao: ");
scanf("%d", &choice);
ln(); // Break two lines
switch (choice) {
case 1:
starGame();
break;
case 2:
viewLog();
break;
case 3:
viewLeaderboard();
break;
case 4:
printf("Saindo do jogo...\n");
break;
default:
printf("Opcao invalida! Tente novamente.\n");
break;
}
ln(); // Break two lines
} while (choice != 4);
return 0;
}