I am trying to make my own WORDLE game with C++. I have gotten to the point where I can check if a character is in the right spot but I do not know how to check if a letter is in the word but in a different place. This is what I have so far:
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <cstdlib>
using namespace std;
string pickword();
string checkword(string ans, string word);
int main() {
string guess;
cout << "Hi welcome to WORDLE\n\n*Hit enter to start*";
cin.ignore();
string word = pickword();
cout << endl << word;
cout << "\nEnter a 5-letter word: ";
cin >> guess;
checkword(guess, word);
}
string pickword() {
srand((unsigned) time(NULL));
int random = 1 + (rand() % 1999);
string string;
ifstream ReadFile;
ReadFile.open("words.txt");
for (int i = 1; i <= random; i++) {
getline(ReadFile, string);
}
ReadFile.close();
return string;
}
string checkword(string ans, string word) {
char anslst[5];
char wrdlst[5];
for (int i = 0; i <= 4; i++) {
anslst[i] = ans[i];
wrdlst[i] = word[i];
}
//Green = \033[32m
//Red = \033[31m
//Yellow = \033[33m
for (int i = 0; i <= 4; i++) {
if (anslst[i] == wrdlst[i]) {
cout << "\033[32m" << anslst[i];
}
else if (anslst[i] != wrdlst[i]) {
cout << "\033[31m" << anslst[i];
}
}
return word;
}
the part relevent to the question is the bottom of checkword()
. how can I see if the letters in the player's guess are in the the list containing the letters of the answer?