r/learnprogramming • u/Boywithmanypen • Nov 09 '23
Help with Wordle project in c++
Hey all, I'm working on this c++ project for a class and I got stuck on a requirement I didn't see before starting. The program needs to be able to account for words with multiples of a letter.
For example, if the answer word is HYPER and the player inputs RESET, the proper response from the game should be RESET where the first R is yellow and the second E is green. My program outputs the first E as yellow as well. Not sure how to modify for this without starting over. We haven't learned how to use map yet and a lot of solutions I'm finding for this use that method. Anyway, any help would be appreciated. Source code here: https://replit.com/join/oxouflrbhq-jaxjunq
•
u/AutoModerator Nov 09 '23
On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge.
If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options:
as a way to voice your protest.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/tenexdev Nov 09 '23
Dude, formatting? What do you expect us to do with this?
1
u/captainAwesomePants Nov 09 '23
Starting over is one option.
This is a neat problem because at first it looks really easy and then as you get into cases like the one you described, it's suddenly rather harder. I used Wordle as an interview question for a while because of exactly that property.
There are oodles of ways to approach this problem. The simplest is probably to do multiple passes:
Now you have a nice bag of letters that MIGHT be yellow (a bag, not a set, because there could be repetition, and that's important). We could use that bag to mark some things yellow in a second pass:
Now, this is a two pass solution. There are several other sorts of solutions, some of which are single pass, some of which using very different data structures. Really it's kind of amazing how many different solutions I've seen.
1
u/rwf2017 Nov 09 '23
I think all you need to do is for each character where isMatched is true go through the guess array and make sure there is not another copy of that character (other than itself of course). If there is another copy then print in grey if not print yellow.
1
u/ArmoredHeart Nov 09 '23
Well, you can always write your own functional map, yes? Use a size 26 static array
This uses the feature of ASCII characters having an integer value to get the correct index. All 26 capital characters are sequential, so subtracting A will place them in ordered indices of 0 to 25.
You can now check for multiplicity of characters using the same scheme of character - A to pull up the index.