@@ -20,16 +20,16 @@ protected override void ValidateGuessValues()
20
20
protected override ShapeAndColorResult GetCoreResult ( )
21
21
{
22
22
// 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
27
27
28
28
byte black = 0 ;
29
29
byte white = 0 ;
30
30
byte blue = 0 ;
31
31
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)
33
33
// add the remaining codes and guess pegs to the remaining lists to check for white and blue keyPegs
34
34
for ( int i = 0 ; i < guessPegsToCheck . Count ; i ++ )
35
35
{
@@ -44,36 +44,40 @@ protected override ShapeAndColorResult GetCoreResult()
44
44
}
45
45
}
46
46
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
47
49
codesToCheck = remainingCodesToCheck ;
48
- remainingCodesToCheck = new ( codesToCheck ) ;
49
50
guessPegsToCheck = remainingGuessPegsToCheck ;
50
- remainingGuessPegsToCheck = [ ] ;
51
+ remainingCodesToCheck = new ( codesToCheck ) ;
52
+ remainingGuessPegsToCheck = Enumerable . Repeat ( ShapeAndColorField . Empty , guessPegsToCheck . Count ) . ToList ( ) ;
51
53
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
54
54
for ( int i = 0 ; i < guessPegsToCheck . Count ; i ++ )
55
55
{
56
56
ShapeAndColorField ? codeField = codesToCheck . FirstOrDefault ( c => c == guessPegsToCheck [ i ] ) ;
57
57
if ( codeField is not null )
58
58
{
59
59
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
62
64
}
63
65
else
64
66
{
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
66
68
}
67
69
}
68
70
71
+ // check blue (either the shape or the color is in the correct position but with a wrong paired element)
69
72
codesToCheck = remainingCodesToCheck ;
70
73
guessPegsToCheck = remainingGuessPegsToCheck ;
71
74
72
- // check blue (either the shape or the color is in the correct position but with a wrong paired element)
73
75
for ( int i = 0 ; i < guessPegsToCheck . Count ; i ++ )
74
76
{
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 ) )
77
81
{
78
82
blue ++ ;
79
83
}
0 commit comments