Skip to content

Commit 06045fc

Browse files
Merge pull request #239 from CodebreakerApp/235-analyzer5x5x4
fix analyzer for 5x5x4 games
2 parents 1ef368d + a4f8715 commit 06045fc

File tree

4 files changed

+51
-16
lines changed

4 files changed

+51
-16
lines changed

src/services/common/Codebreaker.GameAPIs.Analyzers.Tests/Analyzers/ShapeGame5x5x4AnalyzerTests.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,34 @@ namespace Codebreaker.GameAPIs.Analyzer.Tests;
77

88
public class ShapeGame5x5x4AnalyzerTests
99
{
10+
[Fact]
11+
public void SetMove_ShouldNotReturnBlueWithOneWhite()
12+
{
13+
ShapeAndColorResult expectedKeyPegs = new(0, 1, 0);
14+
ShapeAndColorResult? resultKeyPegs = AnalyzeGame(
15+
["Rectangle;Green", "Triangle;Blue", "Square;Blue", "Triangle;Green"],
16+
["Circle;Red", "Square;Green", "Triangle;Blue", "Star;Yellow"]
17+
);
18+
// Position 3: Triangle.Blue is correct, but should be in the 2nd position
19+
// all the other pegs are incorrect
20+
21+
Assert.Equal(expectedKeyPegs, resultKeyPegs);
22+
}
23+
24+
[Fact]
25+
public void SetMove_ShouldReturnOneWhiteAndOneBlue()
26+
{
27+
ShapeAndColorResult expectedKeyPegs = new(0, 1, 1);
28+
ShapeAndColorResult? resultKeyPegs = AnalyzeGame(
29+
["Rectangle;Purple", "Square;Purple", "Star;Red", "Circle;Yellow"],
30+
["Triangle;Purple", "Star;Green", "Circle;Blue", "Square;Purple"]
31+
);
32+
// position 4: Square;Purple is correct but in an incorrect position (should be 2) - white
33+
// position 1: Purple correct - blue
34+
35+
Assert.Equal(expectedKeyPegs, resultKeyPegs);
36+
}
37+
1038
[Fact]
1139
public void SetMove_ShouldReturnTwoBlack()
1240
{
@@ -111,7 +139,7 @@ public void SetMove_ShouldReturnTwoBlueForMatchingShapes()
111139
[Fact]
112140
public void SetMove_ShouldReturnOneBlackAndOneWhite()
113141
{
114-
// the first and second guess have a correct shape, but both in the wrong positon
142+
// the first and second guess have a correct shape, but both in the wrong position
115143
// all the colors are incorrect
116144
ShapeAndColorResult expectedKeyPegs = new(1, 1, 0);
117145
ShapeAndColorResult? resultKeyPegs = AnalyzeGame(

src/services/common/Codebreaker.GameAPIs.Analyzers/Analyzers/ShapeGameGuessAnalyzer.cs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/services/common/Codebreaker.GameAPIs.Analyzers/Codebreaker.Analyzers.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
<LangVersion>latest</LangVersion>
9+
<VersionPrefix>3.7.1</VersionPrefix>
910
<PackageTags>
1011
Codebreaker;CNinnovation;GameAnalyzers
1112
</PackageTags>

src/services/common/Codebreaker.GameAPIs.Analyzers/Fields/ShapeAndColorField.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
public partial record class ShapeAndColorField(string Shape, string Color)
44
{
5+
internal static ShapeAndColorField Empty => new ShapeAndColorField(string.Empty, string.Empty);
6+
57
private const char Separator = ';';
68
public override string ToString() => $"{Shape}{Separator}{Color}";
79

0 commit comments

Comments
 (0)