跳到正文

个人记录

自制Wordle

冷却心约 14 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月24日
篇幅
约 14 分钟
获赞
1

版本

  1. v1当前

自制 Wordle

code:

1. 主程序(Wordle.cpp):

C++
#include<bits/stdc++.h>
#include<Windows.h>
#include "DictionaryMent.h"
#define ll long long
using namespace std;
ll cnt,flag=0;
set<char> green,yellow,red;
string keystr,input[10005];
DictionaryMent dm;
void cls(){
	system("cls");
	return ;
}
void setColor(string color){
	if(color=="Red")SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_RED);
	if(color=="Green")SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_GREEN);
	if(color=="Cyan")SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);
	if(color=="Yellow")SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
	if(color=="Blue")SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_BLUE);
	if(color=="Default")SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY);
	return ;
}
ll getRand(){
	srand(time(0));
	ll sum=0;
	for(int i=1;i<=100000;i++){
		sum+=rand();
	}
	return sum;
}
void showWord(string word){
	for(int i=0;i<word.size();i++){
		if(word[i]==keystr[i]){
			setColor("Green");
			green.insert(word[i]);
		}
		else if(keystr.find(word[i])<keystr.size()){
			setColor("Yellow");
			yellow.insert(word[i]);
		}
		else{
			setColor("Red");
			red.insert(word[i]);
		}
		cout<<word[i];
		setColor("Default");
		cout<<" ";
	}
	setColor("Default");
	cout<<"\n";
	return ;
}
void display(){
	cls();
	setColor("Cyan");
	printf("Key Length:%d\n",keystr.size());
	for(int i=1;i<=cnt;i++){
		showWord(input[i]);
	}
	for(char c='a';c<='z';c++){
		if(green.count(c))setColor("Green");
		else if(yellow.count(c))setColor("Yellow");
		else if(red.count(c))setColor("Red");
		else setColor("Default");
		cout<<c;
		setColor("Default");
		cout<<" ";
	}
	return;
}
void doWithString(string in){
	if(in.size()!=keystr.size()){
		cls();
		printf("Length Error!!!");
		Sleep(2000);
		cls();
		return ;
	}
	if(dm.dictionary.count(in)==0){
		cls();
		printf("There Isn't This Word In The Game's Dictionary!!!");
		Sleep(2000);
		cls();
		return ;
	}
	input[++cnt]=in;
	if(keystr==in){
		flag=1;
		return ;
	}
	return ;
}
string get(){
	string str;
	cin>>str;
	return str;
}
int main(){
	printf("Getting Dictionary...");
	dm.initDictionary();
	freopen("CON","r",stdin);
	cin.clear();
	Sleep(1000);
	cls();
	cls();
	printf("initing Wordle...");
	cnt=0;
	Sleep(1000);
	cls();
	printf("Choosing Key...");
//	printf("key index:%d\n",keyIndex);
	Sleep(5000);
	do{
		ll keyIndex=getRand()%dm.len+1;
		keystr=dm.getKeyString(keyIndex);
	}
	while(keystr.size()<4);
	Sleep(1000);
	cls();
	setColor("Blue");
	printf("			WORDLE				\n\n\n");
	setColor("Yellow");
	printf("		   by @GIAOchen			");
	Sleep(7000);
	cls();
	ll maxx=keystr.size()+1;
	if(keystr.size()<=3)maxx=4;
	while(cnt<maxx){
		display();
		if(flag==1){
			setColor("Cyan");
			printf("\n\nYou Win!\nKey:%s\n",keystr.c_str());
			Sleep(10000);
			setColor("Default");
			return 0;
		}
		setColor("Default");
		printf("\n");
		printf("Input A Guess String:");
		Sleep(2000);
		string str=get();
		if(str=="EXIT")break;
		doWithString(str);
	}
	display();
	if(flag==1){
		setColor("Cyan");
		printf("\n\nYou Win!\nKey:%s\n",keystr.c_str());
		Sleep(10000);
		setColor("Default");
		return 0;
	}
	else{
		setColor("Red");
		printf("\n\nYou Lose!\nKey:%s\n",keystr.c_str());
		Sleep(10000);
		setColor("Default");
		return 0;
	}
	system("pause");
	return 0;
}

2. 支持库:(DictionaryMent.h):

C++
#include<bits/stdc++.h>
#define ll long long
using namespace std;

class DictionaryMent{
 public:
 	set<string> dictionary;
 	ll len;
 	void initDictionary(){//get dictionary
 		len=0;
 		freopen("Dictionary.txt","r",stdin);
 		string str;
 		while(cin>>str){
 			if(str=="#")break;
 			dictionary.insert(str);
 			++len;
 		}
 		fclose(stdin);
 		return ;
 	}
 	string getKeyString(int index){//find string
 		int i=0;
 		for(auto str:dictionary){
 			++i;
 			if(i==index){
 				return str;
 			}
 		}
 		return "";//Can't find it
 	}
 private:

};

词库(Dictionary.txt)(会更新) : were they your their mine yours his hers ours theirs them apple pear watermelon strawberry grape banana great grand mother father other farmer nurse doctor scarf print output input over above rand sand wood stone cabbage chicken beef steak renderer reference take swim dictionary desk desktop bottom left right picnic have hamberger milk happy blog cook above beside behind between vegetable brother sister about outside inside side three four five siven eight nine eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty thirty fourty fifty sixty seventy eighty ninety hundred thousand insert back front black white green blue cyan gray purple yellow orange bean carrot rabbit guard guess plant bike clever strict farm holiday paper page head headache body tooth toothache stomach stomachache foot fever homework winter summer spring autumn fall ball bridge building house village villager balloon build kite bird dog elephant mouse rice rise late early nearly ready already biggest longest garden gardener part partner else switch break bread juice Coke breakfast lunch dinner meat fish pork park card class please close closed shut open interesting interested interest internet internation national international private public personal olympics information game race fast fastly faster bigger longer shorter listen music maths Chinese English American Canadian India Indian Russia Russian German Germany work from shcool home there that these rest ride horse sheep poor rich vase down come from form dentist server client tree flower plate glass glasses sunglasses into China America Russia Canada German French India Italy Italian Control with shift space enter delete screen pause clean clear scan plus time continue word text parent grandparent grandfather grandmother aunt uncle book case bookcase shelf knife spoon bowl boat dive take show give look feel taste smell sound smelly noise noisy answer shout rainy sunny cloudy windy snowy wind sun cloud snow rain soup woman person nation internation may should maybe make function minute second hour month year monitor teacher teach dead death sword shield hospital array arrow tick cross magic magical mysterious paint painter clock phone telephone television little news newspaper radio magazine phrase watch window classroom playground object drive only script craft push pool rename reborn born name remake make restart start awake joker clown laugh gragh photo photogragh fish vector greater than less #

注意事项:

  1. 以上三个文件请按照()里的命名,并放在同一个目录下

  2. KeyLengthKey Length : 关键词长度|LengthErrorLength Error:长度错误,输入的单词比关键词长或短|ThereIsn′tThisWordInTheGame′sDictionaryThere Isn't This Word In The Game's Dictionary:游戏词典中没有这个单词(你可以添加进去)|红色字母:此字母在关键词中没有出现|黄色字母:关键词中有这个字母,但你输入的位置不对|绿色字母:关键词中有这个字母,而且你输入的位置是对的

  3. 添加单词:在Dictionary.txt的单词部分的结尾,‘#’前面添加,单词之间用空格隔开,结束符‘#’前面也应有一个空格

  4. 如果实在猜不出,输入“EXIT”(必须大写)结束猜词

  5. 如发现有BUG,请私信我(@GIAOchen)

原文