'Can't compare specific positions in a list
I was working on making a wordle bot to challenge my ability to program, but I am at a standstill.
List<string> wordsA = new List<string>();
List<string> letters = new List<string>();
letters.Add("");
wordsA.Add("");
if (letters[8] == "1")
{
for (int i = 0; i < words.Length; i++)
{
wordsA.Add(words[i]);
if (letters[7] == wordsA[4])
{
possiblewords1.Add(words[i]);
wordsA.Remove(words[i]);
}
}
}
Every guess, it breaks up the guess into 10 parts. For example, ERASE would turn into E2R1A3S2E1, with the numbers representing if the letter was correct. And I was trying to compare "wordsA" to "letters". These are both lists. Almost every time it tries to check, I get the error
Unhandled exception. System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index') at System.Collections.Generic.List`1.get_Item(Int32 index)
This only occurs at the lines where it's comparing "wordsA" to "letters"
Solution 1:[1]
I have no idea about wordle, but the error is quite obvious... your letters list has one element - empty string. So, when you write if (letters[8] == "1") - your index is out of bounds
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Felix |
