@@ -20,16 +20,16 @@ protected override void ValidateGuessValues()
2020 protected override ShapeAndColorResult GetCoreResult ( )
2121 {
2222 // Check black, white and blue keyPegs
23- List < ShapeAndColorField > codesToCheck = [ .._game . Codes . ToPegs < ShapeAndColorField > ( ) ] ;
24- List < ShapeAndColorField > guessPegsToCheck = [ .. Guesses ] ;
25- List < ShapeAndColorField > remainingCodesToCheck = [ ] ;
26- List < ShapeAndColorField > remainingGuessPegsToCheck = [ ] ;
23+ List < ShapeAndColorField > codesToCheck = [ .._game . Codes . ToPegs < ShapeAndColorField > ( ) ] ; // all the codes that need to be verified with the actual check
24+ List < ShapeAndColorField > guessPegsToCheck = [ .. Guesses ] ; // all the guesses that need to be verified with the actual check
25+ List < ShapeAndColorField > remainingCodesToCheck = [ ] ; // the codes that need to be checked with the check following - filled by the actual check
26+ List < ShapeAndColorField > remainingGuessPegsToCheck = [ ] ; // the guesses that need to be checked with the check following - filled by the actual check
2727
2828 byte black = 0 ;
2929 byte white = 0 ;
3030 byte blue = 0 ;
3131
32- // check for black (correct color and shape at the correct position)
32+ // first check for black (correct color and shape at the correct position)
3333 // add the remaining codes and guess pegs to the remaining lists to check for white and blue keyPegs
3434 for ( int i = 0 ; i < guessPegsToCheck . Count ; i ++ )
3535 {
@@ -44,36 +44,40 @@ protected override ShapeAndColorResult GetCoreResult()
4444 }
4545 }
4646
47+ // next check for white (correct pair at a wrong position)
48+ // add the remaining codes and guess pegs to the remaining lists to check for blue keyPegs
4749 codesToCheck = remainingCodesToCheck ;
48- remainingCodesToCheck = new ( codesToCheck ) ;
4950 guessPegsToCheck = remainingGuessPegsToCheck ;
50- remainingGuessPegsToCheck = [ ] ;
51+ remainingCodesToCheck = new ( codesToCheck ) ;
52+ remainingGuessPegsToCheck = Enumerable . Repeat ( ShapeAndColorField . Empty , guessPegsToCheck . Count ) . ToList ( ) ;
5153
52- // check for white (correct pair at a wrong position)
53- // and add the remaining codes and guess pegs to the remaining lists to check for blue keyPegs
5454 for ( int i = 0 ; i < guessPegsToCheck . Count ; i ++ )
5555 {
5656 ShapeAndColorField ? codeField = codesToCheck . FirstOrDefault ( c => c == guessPegsToCheck [ i ] ) ;
5757 if ( codeField is not null )
5858 {
5959 white ++ ;
60- codesToCheck . Remove ( codeField ) ; // remove for the white check
61- remainingCodesToCheck . Remove ( codeField ) ; // remove for the blue check
60+
61+ var ix = codesToCheck . IndexOf ( codeField ) ;
62+ codesToCheck [ ix ] = ShapeAndColorField . Empty ; // this code is a match and thus no longer is used when checking for white
63+ remainingCodesToCheck [ ix ] = ShapeAndColorField . Empty ; // this code is also not used with the next blue check
6264 }
6365 else
6466 {
65- remainingGuessPegsToCheck . Add ( guessPegsToCheck [ i ] ) ; // add for the blue check
67+ remainingGuessPegsToCheck [ i ] = guessPegsToCheck [ i ] ; // not a match for the guess, thus it needs to be added for the blue check
6668 }
6769 }
6870
71+ // check blue (either the shape or the color is in the correct position but with a wrong paired element)
6972 codesToCheck = remainingCodesToCheck ;
7073 guessPegsToCheck = remainingGuessPegsToCheck ;
7174
72- // check blue (either the shape or the color is in the correct position but with a wrong paired element)
7375 for ( int i = 0 ; i < guessPegsToCheck . Count ; i ++ )
7476 {
75- if ( guessPegsToCheck [ i ] . Shape == codesToCheck [ i ] . Shape ||
76- guessPegsToCheck [ i ] . Color == codesToCheck [ i ] . Color )
77+ if ( ( guessPegsToCheck [ i ] != ShapeAndColorField . Empty ||
78+ codesToCheck [ i ] != ShapeAndColorField . Empty ) &&
79+ ( guessPegsToCheck [ i ] . Shape == codesToCheck [ i ] . Shape ||
80+ guessPegsToCheck [ i ] . Color == codesToCheck [ i ] . Color ) )
7781 {
7882 blue ++ ;
7983 }
0 commit comments