Wordle Pygame – typing 5 of the same letters in a word turn it into yellow

Image of the problem:

Image of the problem

Code involved:

def checkguess(self,guess): 

        self.guess="".join(self.guess)
        print(self.guess)
        for i,letter in enumerate(self.guess):
        
                if self.answer[i] == self.guess[i]:
                        self.highlightbox(i,GREEN)
                        
                elif letter in self.answer:          
                        self.highlightbox(i,YELLOW)

                        
                else:
                        self.highlightbox(i,GRAY)
        

I have tried to use breaks, I think it involves making a second loop, and putting the check inside the loop, and then breaking the loop once it goes to a correct letter.

  • 1

    Create a copy of answer or better create a collections.Counter from it. In a first loop check for exact matches (green). For each exact match set the letter in the answer copy to e.g. - or decrement it in the Counter. In a second loop check remaining places of guess for remaining characters in the answer. Again, if a match is found, remove the character or decrement the Counter.

    – 

  • You could 1st make a pass for green letters, replacing each one with some symbol in self.answer (or rather a copy of it), then make a 2nd pass for yellow letter with the same logic (else you’ll get the same problem).

    – 

Leave a Comment