-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
313 lines (259 loc) · 8.92 KB
/
main.c
File metadata and controls
313 lines (259 loc) · 8.92 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define MAX_LENGTH 256 // You can adjust the maximum length of the name
int colictscore;
struct Entry {
char YourFullName[MAX_LENGTH];
int points;
};
// Function prototypes
void Save_name();
void titel_game();
void titel_Score();
int compareEntries(const void *a, const void *b);
int main() {
// Start menu //
int farstwant;
printf(" 1: Start\n 2: top Score\n 3: Exit\n");
printf("Select the number 1 or 2 or 3 : ");
scanf("%d", &farstwant);
printf("\n");
// if you select Start or top Score or Exit//
if (farstwant == 2) {
titel_Score();
printf("\n\n");
int menu ;
printf("do you want Return to the start menu ?\n1: Return\n2: Exit\n-Select 1 or 2: ");
scanf("%d", &menu );
printf("\n");
if (menu == 1) { // Play again
getchar();
system("cls");
main();
} else {
return 0;
}
return 0;
} else if (farstwant == 3) {
return 0;
}
// the number 1 or 2 or 3 : //
int NumDigits;
printf("Select the number\n 1: 3\n 2: 5\n 3: 7\n");
printf("Select the number 1 or 2 or 3 : ");
scanf("%d", &NumDigits);
printf("\n");
// Repeat if the number is not required //
while (NumDigits < 1 || NumDigits > 3) {
printf("Select the number 1 or 2 or 3 : ");
scanf("%d", &NumDigits);
printf("\n");
}
int maxAttempts; // Set the maximum number of attempts //
printf("how much attempts you want : ");
scanf("%d", &maxAttempts);
printf("\n");
// same location or Not in the same location.
int testo;
printf("- Select what do you want -\n 1: same location.\n 2: Not in the same location.\n\n");
printf("- Select the number 1 or 2 : ");
scanf("%d", &testo);
printf("\n");
while (testo < 1 || testo > 2) {
printf("- Select the number 1 or 2 : ");
scanf("%d", &testo);
printf("\n");
}
// Delete all of the above //
getchar();
system("cls");
titel_game();
printf("\n\n");
// Convert the number to "*" etc. //
if (NumDigits == 1) {
NumDigits = 3;
} else if (NumDigits == 2) {
NumDigits = 5;
} else {
NumDigits = 7;
}
// random number //
srand(time(NULL));
int randomNum[7]; // maximum number of random numbers is 7 //
printf("Random numbers : ");
for (int i = 0; i < NumDigits; ++i) {
int newRandom;
int isDuplicate;
do {
isDuplicate = 0;
newRandom = rand() % 20 + 1;
// Check if the new number is already in the array //
for (int j = 0; j < i; ++j) {
if (newRandom == randomNum[j]) {
isDuplicate = 1;
break;
}
}
} while (isDuplicate);
randomNum[i] = newRandom;
printf("* ");
}
printf("\n");
// Check whether the entry is valid or no //
int corrGuess = 0;
int guessedNum[7] = {0}; // guessed numbers array //
if (testo == 1) {
printf("-you Select :same location.\n");
} else if (testo == 2) {
printf("-you Select :Not in the same location.\n");
}
// Attempts Score
int AttemptsScore;
if(maxAttempts <= 4){
colictscore = colictscore+5000;
}else if(maxAttempts <= 8){
colictscore = colictscore+3000;
}else if(maxAttempts <= 16){
colictscore = colictscore+1000;
}else if(maxAttempts <= 20){
colictscore = colictscore+500;
}
int sameAttempts;
if (testo == 1) {
colictscore = colictscore *2;
}
printf("Write %d numbers in each attempt - \n", NumDigits);
for (int attempt = 1; attempt <= maxAttempts && corrGuess < NumDigits; ++attempt) {
// Enter a guess // Array //
printf("Attempt %d - Guess the numbers: ", attempt);
// Array userGuess //
int userGuess[7];
for (int i = 0; i < NumDigits; ++i) {
scanf("%d", &userGuess[i]);
}
// Check if the guessed number matches any of the random numbers //
if (testo == 1) { // Check for same location
for (int i = 0; i < NumDigits; ++i) {
if (userGuess[i] == randomNum[i] && guessedNum[i] == 0) {
guessedNum[i] = userGuess[i];
corrGuess++;
}
}
} else if (testo == 2) { // Check for not in the same location
for (int i = 0; i < NumDigits; ++i) {
for (int j = 0; j < NumDigits; ++j) {
if (userGuess[i] == randomNum[j] && guessedNum[j] == 0) {
guessedNum[j] = userGuess[i];
corrGuess++;
break;
}
}
}
}
// Print the current state of guessed numbers
for (int k = 0; k < NumDigits; ++k) {
if (guessedNum[k] != 0) {
printf("%d ", guessedNum[k]);
} else {
printf("* ");
}
}
if (attempt <= AttemptsScore) {
colictscore = colictscore * 0.889;
} else {
colictscore = colictscore * 0.88;
}
printf("\n");
}
// You won or not //
if (corrGuess < NumDigits) {
printf("\n");
printf("Sorry, you couldn't guess all the numbers in %d attempts. You lost.\n", maxAttempts);
} else {
printf("\n");
printf("Congrats! You guessed all the numbers.\n -You won 5$ million \n\n\n");
printf("Your score is : %d",colictscore);
printf("\n\n");
int saveTest;
printf("Do you want to save your score?\n1: Yes\n2: No\nSelect 1 or 2: ");
scanf("%d", &saveTest);
printf("\n");
if (saveTest == 1) { // save
Save_name();
printf("Data saved to file successfully.\n");
} else {
return 0;
}
}
int reaptegame;
printf("Do you want Play again?\n1: Yes\n2: No\nSelect 1 or 2: ");
scanf("%d", &reaptegame);
printf("\n");
if (reaptegame == 1) { // Play again
getchar();
system("cls");
main();
} else {
return 0;
}
return 0;
}
void titel_game() {
printf("===========================================================================\n");
printf("================= Start guessing random numbers ====================\n");
printf("===========================================================================\n");
}
void titel_Score() {
printf("======================================================\n");
printf("================= Top Score ====================\n");
printf("======================================================\n");
FILE *file = fopen("output.txt", "r");
if (file == NULL) {
fprintf(stderr, "Error opening file\n");
return;
}
int maxEntries = 100000; // Adjust as needed
struct Entry *entries = malloc(maxEntries * sizeof(struct Entry));
// Read entries from the file
int i = 0;
char line[MAX_LENGTH];
while (fgets(line, MAX_LENGTH, file) != NULL && i < maxEntries) {
// Use sscanf to extract name and points from the line
if (sscanf(line, "Full Name: %s Points: %d", entries[i].YourFullName, &entries[i].points) == 2) {
i++;
}
}
fclose(file);
// Sort entries by points in descending order
qsort(entries, i, sizeof(struct Entry), compareEntries);
// Display the top 10 names and points
printf("\n");
int displayCount = i < 10 ? i : 10;
for (int j = 0; j < displayCount; j++) {
printf("Top %d: %s - Points: %d\n", j + 1, entries[j].YourFullName, entries[j].points);
}
printf("\n");
printf("======================================================\n");
printf("======================================================\n");
printf("======================================================\n");
}
void Save_name() {
char YourFullName[MAX_LENGTH];
// Prompt user for name
printf("Enter your full name: ");
scanf("%s", YourFullName);
FILE *file = fopen("output.txt", "a");
if (file == NULL) {
fprintf(stderr, "Error opening file\n");
return;
}
// Write the name and points to the file
fprintf(file, "Full Name: %s Points: %d\n", YourFullName, colictscore);
fclose(file);
}
// Comparator function for qsort
int compareEntries(const void *a, const void *b) {
return ((struct Entry *)b)->points - ((struct Entry *)a)->points;
}