I’m trying to create a Wordle game with C++, version 5.11. The game is simle: i randomly generate a word from a closed selection and stored in a variable. Then, i created a while loop to introduce letters and store them in variable to print to the terminal if they are correct. If they don’t, they substract you lifes from the count. The game works, because when you guess correctyl, it doesn’t substract you lifes, but it doesn’t show the word with the letters and gaps either.
/*
Wordle
Adrián Sequeiro Miramontes
04/12/2022
*/
#include<iostream>
#include<ctime>
#include<string>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
char letter;
string Words[]={"Plane","Stupid","Van","Wheel","Instrument","Deal","Countdown","Truck"};
string HiddenWord;
string discoveredWord;
int lifes;
int number;
int size;
void Action(string a, char b)
{
bool coincidence=false;
int i=0;
while (i<=a.size())
{
if (HiddenWord[i]==b)
{
discoveredWord[i]=b;
cout<<discoveredWord<<endl;
coincidence=true;
}
i++;
}
if (coincidence==false)
{
lifes--;
cout<<"Wrong answer. Now your lifes are "<<lifes<<". Try again"<<endl;
cout<<endl;
}
}
int generate()
{
srand(time(NULL));
HiddenWord=Words[rand()%8];
size=sizeof(HiddenWord)/sizeof(char);
for (int i=0; i<= size; i++)
{
discoveredWord[i]='_';
}
}
int main()
{
cout<< "Welcome to Wordle:"<<endl;
cout<<"This game is based on guessing words"<<endl;
generate();
cout<<" Options:"<<endl;
cout<<"1.Easy Mode"<<endl;
cout<<"2.Medium Mode"<<endl;
cout<<"3.Difficult Mode"<<endl;
cin>>number;
switch (number)
{
case 1:
lifes=7;
cout<<"You have "<<lifes<<" lifes"<<endl;
break;
case 2:
lifes=5;
cout<<"You have "<<lifes<<" lifes"<<endl;
break;
case 3:
lifes=3;
cout<<"You have "<<lifes<<" lifes"<<endl;
break;
}
cout<<endl;
generate();
cout<<HiddenWord<<endl;
while (discoveredWord!=HiddenWord && lifes>0)
{
cout<<"Choose a letter"<<endl;
cin>>letter;
Action(HiddenWord,letter);
}
if (lifes==0)
{
cout<<"Sorry, try again. You lost"<<endl;
}
else
{
cout<<"Well done. You guessed it"<<endl;
}
string answer;
cout<<endl;
cout<<"Do you wanna continue?"<<endl;
cin>>answer;
if (answer=="yes")
{
main();
}
else
{
return 0;
}
}
This is in the console, when executed the code. As you see, it doesn’t discriminate between upper and lowercasses.
Welcome to Wordle:
This game is based on guessing words
Options:
1.Easy Mode
2.Medium Mode
3.Difficult Mode
3
You have 3 lifes
Van
Choose a letter
a
Choose a letter
n
Choose a letter
r
Wrong answer. Now your lifes are 2. Try again
Choose a letter
v
Wrong answer. Now your lifes are 1. Try again
Choose a letter
I have no idea why it’s omitting the code inside the for loop. I would be very grateful if you shed light on it. Forgive my expression, I’m spanish 🙂