-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordle.cpp
More file actions
171 lines (171 loc) · 3.87 KB
/
wordle.cpp
File metadata and controls
171 lines (171 loc) · 3.87 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
#include<bits/stdc++.h>
#include<conio.h>
#include<windows.h>
using namespace std;
string wrds[15000],ans[2500],chn[2500],line;
string user,sys,trans,gsd,adchn,adwrd;
int i,j,k,th=0,life=6,cnt1=0,cnt2=0,falg=0;
int c;bool reset=true;
ifstream ansifile("wordle_ans.txt");
ifstream gssifile("wordle_guess.txt");
ofstream ofile("wordle_guess.txt",ios::app);
HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
char tc(int inte){return inte;}
void play();
void cons();
void openn(){
ifstream ansifile("wordle_ans.txt");
ifstream gssifile("wordle_guess.txt");
ofile.open("wordle_guess.txt",ios::app);
}
void closee(){
ansifile.close();gssifile.close();ofile.close();
}
void opr(){
//closee();openn();cons();
ansifile.clear();
ansifile.seekg(0,ios::beg);
}
int search(string arr[],int sise,string targ){
for(k=0;k<=sise;k++){
if(arr[k]==targ){return k;}
}
return -1;
}
void color(string clr){
if(clr=="white"){
SetConsoleTextAttribute(hout,FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_BLUE);
}
else if(clr=="yellow"){
SetConsoleTextAttribute(hout,FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_RED);
}
else if(clr=="cyan"){
SetConsoleTextAttribute(hout,FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_BLUE);
}
else if(clr=="green"){
SetConsoleTextAttribute(hout,FOREGROUND_INTENSITY|FOREGROUND_GREEN);
}
else if(clr=="red"){
SetConsoleTextAttribute(hout,FOREGROUND_INTENSITY|FOREGROUND_RED);
}
}
void cons(){
int sise;
cnt1=0,cnt2=0,th=0;
while(getline(gssifile,line)){
wrds[th]=line;
th++;cnt1++;
}
th=0;
while(getline(ansifile,line)){
sise=line.size();
for(i=0;i<sise;i++){
if(line[i]!=' '){
ans[th]+=line[i];
}
else{break;}
}
chn[th]=line.substr(i+1,sise-i-1);
th++;cnt2++;
}
}
void eNd(string ver){
if(ver=="win"){
color("yellow");
cout<<"You guessed out the word!\n";
color("cyan");
cout<<sys<<" "<<trans<<"\n";
}
else if(ver=="lose"){
color("red");
cout<<"You used up the times.\n";
color("white");
cout<<"the word is ";color("cyan");
cout<<sys;color("white");cout<<".\n";color("cyan");
cout<<sys<<" "<<trans<<"\n";
}
else{
color("red");cout<<"You choose to restart.\n";
color("white");
cout<<"the word is ";color("cyan");
cout<<sys;color("white");cout<<".\n";color("cyan");
cout<<sys<<" "<<trans<<"\n";reset=true;play();
}
color("white");
cout<<"Wanna play again?[Y/N]\n";
char choice;
cin>>choice;
if(choice=='Y'){
reset=true;play();
}
else{closee();exit(0);}
}
void play(){
if(reset){
th=rand()%cnt2;
sys=ans[th];trans=chn[th];reset=false;user="";
life=6;gsd="";//opr();
}
//cout<<th<<wrds[th]<<"\n";
L:color("white");cout<<"_ _ _ _ _";
for(i=0;i<9;i++){cout<<'\b';}
user="";
for(i=0;i<5;i++){
c=getch();
if(c==224){ansifile.close();gssifile.close();ofile.close();exit(0);}
if(c!=8 && isalpha(tc(c)) || c=='-'){
cout<<tc(c)<<" ";
user+=tc(c);
}
else if(i!=0 && c==8){
cout<<"\b\b";
cout<<"_ _";
cout<<"\b\b\b";
user.erase(user.end()-1);
i-=2;
}
else{i--;}
}
for(i=0;i<10;i++){cout<<'\b';}
if(user=="rstrt"){eNd("restart");}
if(search(wrds,cnt1,user)==-1){
color("red");Sleep(500);
for(j=0;j<15;j++){cout<<" ";}
for(j=0;j<15;j++){cout<<"\b";}
cout<<"Illegal.";
Sleep(750);
for(j=0;j<10;j++){cout<<"\b \b";}
user="";goto L;
}
if(gsd.find(user)!=gsd.npos && gsd.find(user)%5==0){
color("cyan");Sleep(500);
cout<<"You've guessed this word.";Sleep(1500);
for(j=0;j<50;j++){cout<<"\b \b";}
user="";goto L;
}
gsd+=user;
for(i=0;i<5;i++){
if(user[i]==sys[i]){
color("green");
cout<<user[i]<<" ";
continue;
}
if(sys.find(user[i])!=sys.npos){
color("yellow");
cout<<user[i]<<" ";
}
else{color("white");cout<<user[i]<<" ";}
}
cout<<"\n";
if(sys==user){eNd("win");}
if(life==1){eNd("lose");}
life--;user="";
play();
}
int main(){
srand((unsigned)time(NULL));
cons();
SetConsoleTitle("Wordle");
reset=true;play();
return 0;
}