-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordle.c
140 lines (131 loc) · 3.64 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
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define WORD_COUNT 2315
#define WORD_LENGTH 5
#define CHANCES 5
#define RED "\x1b[31m"
#define YELLOW "\x1b[33m"
#define GREEN "\x1b[32m"
#define GRAY "\x1b[90m"
#define RESET "\x1b[0m"
#define CORRECT 2
#define PRESENT 1
#define ABSENT -1
int checkSubmission(char finalWord[], char *submittedWord){
int result[5] = {0,0,0,0,0};
for(int i = 0; i < 5; i++){
if(finalWord[i] == submittedWord[i]){
result[i] = CORRECT;
}
else{
for(int j = 0; j < 5; j++){
if(submittedWord[i] == finalWord[j]){
result[i] = PRESENT;
break;
}
}
if(result[i] == 0){
result[i] = ABSENT;
}
}
}
int isCorrect = 0;
for(int i = 0; i<5; i++){
// printf("result => %d\n", result[i]);
if(result[i] == 2){
printf(GREEN "%c" RESET, submittedWord[i]);
isCorrect++;
}
else if (result[i] == 1) {
printf(YELLOW "%c" RESET, submittedWord[i]);
isCorrect = -1;
}
else if (result[i] == -1) {
printf(GRAY "%c" RESET, submittedWord[i]);
isCorrect = -1;
}
}
printf("\n");
if(isCorrect == 5) return 0;
return 1;
}
int validSubmission(char* submission){
for(int i = 0; i < 5; i++){
if(
(int)submission[i] >= 65 && (int)submission[i] <= 122 &&
(int)submission[i] != 91 && (int)submission[i] != 92 &&
(int)submission[i] != 93 && (int)submission[i] != 94 &&
(int)submission[i] != 95
){
submission[i] = toupper(submission[i]);
continue;
}
else{
return 1;
}
}
FILE *fp;
fp = fopen("wordle_words.txt", "r");
char line[256];
int count = 0;
if(fp != NULL){
while(fgets(line, sizeof(line), fp)){
char compareWord[6] = {line[0], line[1], line[2], line[3], line[4], '\0'};
if(strcmp(submission, compareWord) == 0){
return 0;
}
}
}
return 1;
}
char* wordFetcher(char *wordToBeFetched){
FILE *fp;
fp = fopen("wordle_words.txt", "r");
char line[256];
srand(getpid());
int randomLineNumber = rand() % (WORD_COUNT + 1 - 0) + 1;
int count = 0;
if(fp != NULL){
while(fgets(line, sizeof(line), fp)){
count++;
if(count == randomLineNumber){
strcpy(wordToBeFetched, line);
}
}
}
return wordToBeFetched;
}
int main(){
char wordToBeFetched[WORD_LENGTH + 1];
char finalWord[6];
char* fetchedWord = wordFetcher(wordToBeFetched);
for(int i = 0; i<5; i++){
finalWord[i] = wordToBeFetched[i];
}
printf("Welcome\n Go play shoo\n");
finalWord[5] = '\0';
for(int i = 1; i <= CHANCES; i++){
char buffer[256];
char submission[6];
printf("> ");
scanf("%s", buffer);
for(int i = 0; i < 5; i++){
submission[i] = buffer[i];
}
if(validSubmission(submission) == 1){
printf("Not a valid word!\n");
i--;
continue;
}
if(checkSubmission(finalWord, submission) == 0){
printf(GREEN "---===You won!===---" RESET);
return 0;
};
}
printf(RED "---===You lost, new york times can't sell you premium sub now!" RESET);
printf("The correct answer was" GREEN " %s" RESET, finalWord);
return 0;
}