-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordle.c
147 lines (129 loc) · 3.75 KB
/
wordle.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
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <csse2310a1.h>
/*
* Main function which handles running the program.
*/
int main(int argc, char* argv[]) {
// Check for command line arguments
int i;
int len_present = 0;
int max_present = 0;
int len_min = 3, max_min = 3;
int len_max = 9, max_max = 9;
int max_args = 6;
char* dic_path = malloc(1);
FILE* dic_file;
if (argc > max_args) {
fprintf(stderr, "Too many arguments present");
return 1;
}
for (i = 1; i < argc; i++) {
// If argument starts with a '-'
if (argv[i][0] == '-') {
// Check for -len
if (!strcmp(argv[i], "-len")) {
// Check if argument already given
if (len_present == 1) {
fprintf(stderr, "Argument error: %s already given.\n",
argv[i]);
return 1;
}
len_present = 1;
printf("Argument: %s\n", argv[i]); // Debug
// Check for subsequent length
int len;
// if atoi fails, it will return 0 which will result in error 1
len = atoi(argv[i + 1]);
if (len >= len_min && len <= len_max) {
printf("Length value: %d\n", len); // Debug
// Skip next loop as we are checking here
i++;
} else {
printf("Invalid %s argument value: %d. Value must be "
"between %d and %d", argv[i], len, len_min,
len_max);
/*fprintf(stderr, "invalid %s argument value: %d. Value " +
"must be between %d and %d.", argv[i], len,
len_min, len_max);*/
return 1;
}
} else if (!strcmp(argv[i], "-max")) { // Check for -max
if (max_present == 1) {
fprintf(stderr, "Argument error: %s already given.\n",
argv[i]);
return 1;
}
max_present = 1;
printf("Argument: %s\n", argv[i]); // Debug
// Check for subsequent max value
int max;
// if atoi fails, it will return 0 which will result in error 1
max = atoi(argv[i + 1]);
if (max >= max_min && max <= max_max) {
printf("Max value: %d\n", max); // Debug
// Skip next loop as we are checking here
i++;
} else {
/*fprintf(stderr, "invalid %s argument value: %d. Value " +
"must be between %d and %d.", argv[i], max, max_min,
max_max);*/
return 1;
}
} else {
// Else if an argument starts with '-' but is not -len or -max
// then program errors.
fprintf(stderr, "I1nvalid argument given: %s\n", argv[i]);
return 1;
}
} else if (!strcmp(argv[i], "")) {
// Any argument is the empty string.
fprintf(stderr, "I2nvalid argument given: %s\n", argv[i]);
return 1;
} else {
// Check for exisiting dictionary path
if (!strcmp(dic_path, "")) {
// Size dic_path for size of path passed as argument
dic_path = realloc(dic_path, sizeof(argv[i]));
strcpy(dic_path, argv[i]);
printf("Dictionary path: %s\nargv[i]: %s\n", dic_path, argv[i]);
// Check given dictionary path is valid
// Open file for read only
dic_file = fopen(dic_path, "r");
if (dic_file) {
// File can be opened
do {
int c = fgetc(dic_file);
if (c != EOF) {
// If we haven't reached the EOF
}
} while (!feof(dic_file));
fclose(dic_file); // Free up file
} else /* NULL */ {
// If the dictionary file could not be opened
fprintf(stderr, "wordle: dictionary file \"%s\" cannot be "
"opened\n", dic_path);
}
} else {
fprintf(stderr, "Too many arguments given: %s not expected\n",
argv[i]);
return 1;
}
}
}
// At this point, all arguments have been dealt with
// Welcome message
printf("----------Welcome to Wordle.c----------");
// Prompt for input
char* input;
do {
printf("Enter your guess: ");
fgets(input, 10, stdin);
} while ();
// Clean exit of program
return 0;
}
int checkDictionary(char* dict[]) {
}