Skip to content

Commit 0b423c0

Browse files
Update bot algorithm to support Game8x5 and Game5x5x4
Co-authored-by: christiannagel <1908285+christiannagel@users.noreply.github.com>
1 parent 93b02c4 commit 0b423c0

5 files changed

Lines changed: 398 additions & 108 deletions

File tree

src/services/bot/CodeBreaker.Bot.Tests/CodeBreakerAlgorithmsTests.cs

Lines changed: 80 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections;
2+
using Codebreaker.GameAPIs.Client.Models;
23

34
using Xunit;
45

@@ -7,25 +8,62 @@ namespace CodeBreaker.Bot.Tests;
78
public class CodeBreakerAlgorithmsTests
89
{
910
[Fact]
10-
public void SelectPeg_Should_ThrowException()
11+
public void SelectPeg_Should_ThrowException_ForGame6x4()
1112
{
1213
Assert.Throws<InvalidOperationException>(() =>
13-
CodeBreakerAlgorithms.SelectPeg(44, 4));
14+
CodeBreakerAlgorithms.SelectPeg(44, GameType.Game6x4, 4));
15+
}
16+
17+
[Fact]
18+
public void SelectPeg_Should_ThrowException_ForGame8x5()
19+
{
20+
Assert.Throws<InvalidOperationException>(() =>
21+
CodeBreakerAlgorithms.SelectPeg(44, GameType.Game8x5, 5));
22+
}
23+
24+
[Fact]
25+
public void SelectPeg_Should_ThrowException_ForGame5x5x4()
26+
{
27+
Assert.Throws<InvalidOperationException>(() =>
28+
CodeBreakerAlgorithms.SelectPeg(44, GameType.Game5x5x4, 4));
1429
}
1530

1631
[Theory]
1732
[InlineData(0b_000100_000100_000100_000100, 0, 0b_000100)]
1833
[InlineData(0b_000100_000100_000100_000100, 1, 0b_000100)]
1934
[InlineData(0b_000100_000100_000100_000100, 2, 0b_000100)]
2035
[InlineData(0b_000100_000100_000100_000100, 3, 0b_000100)]
21-
public void SelectPegTest(int code, int number, int expected)
36+
public void SelectPegTest_Game6x4(int code, int number, int expected)
37+
{
38+
int actual = CodeBreakerAlgorithms.SelectPeg(code, GameType.Game6x4, number);
39+
Assert.Equal(expected, actual);
40+
}
41+
42+
[Theory]
43+
[InlineData(0b_0100_0100_0100_0100_0100, 0, 0b_0100)]
44+
[InlineData(0b_0100_0100_0100_0100_0100, 1, 0b_0100)]
45+
[InlineData(0b_0100_0100_0100_0100_0100, 2, 0b_0100)]
46+
[InlineData(0b_0100_0100_0100_0100_0100, 3, 0b_0100)]
47+
[InlineData(0b_0100_0100_0100_0100_0100, 4, 0b_0100)]
48+
public void SelectPegTest_Game8x5(int code, int number, int expected)
2249
{
23-
int actual = CodeBreakerAlgorithms.SelectPeg(code, number);
50+
int actual = CodeBreakerAlgorithms.SelectPeg(code, GameType.Game8x5, number);
51+
Assert.Equal(expected, actual);
52+
}
53+
54+
[Theory]
55+
[InlineData(0b_00100_00100_00100_00100, 0, 0b_00100)]
56+
[InlineData(0b_00100_00100_00100_00100, 1, 0b_00100)]
57+
[InlineData(0b_00100_00100_00100_00100, 2, 0b_00100)]
58+
[InlineData(0b_00100_00100_00100_00100, 3, 0b_00100)]
59+
public void SelectPegTest_Game5x5x4(int code, int number, int expected)
60+
{
61+
int actual = CodeBreakerAlgorithms.SelectPeg(code, GameType.Game5x5x4, number);
2462
Assert.Equal(expected, actual);
2563
}
2664

2765
[Fact]
28-
public void HandleBlackMatches_Should_Find1BlackMatch()
66+
public void HandleBlackMatches_Should_Find1BlackMatch_Game6x4()
2967
{
3068
List<int> toMatch =
3169
[
@@ -35,12 +73,27 @@ public void HandleBlackMatches_Should_Find1BlackMatch()
3573
];
3674
int selection = 0b_000001_010000_000001_000001;
3775

38-
List<int> actual = CodeBreakerAlgorithms.HandleBlackMatches(toMatch, 1, selection);
76+
List<int> actual = CodeBreakerAlgorithms.HandleBlackMatches(toMatch, GameType.Game6x4, 1, selection);
3977
Assert.Equal(2, actual.Count);
4078
}
4179

4280
[Fact]
43-
public void HandleBlackMatches_Should_Find2BlackMatches()
81+
public void HandleBlackMatches_Should_Find1BlackMatch_Game8x5()
82+
{
83+
List<int> toMatch =
84+
[
85+
0b_1000_0100_1000_1000_1000, // hit
86+
0b_1000_0100_0100_1000_1000, // hit
87+
0b_0010_0010_0010_0010_0010 // miss
88+
];
89+
int selection = 0b_0001_0100_0001_0001_0001;
90+
91+
List<int> actual = CodeBreakerAlgorithms.HandleBlackMatches(toMatch, GameType.Game8x5, 1, selection);
92+
Assert.Equal(2, actual.Count);
93+
}
94+
95+
[Fact]
96+
public void HandleBlackMatches_Should_Find2BlackMatches_Game6x4()
4497
{
4598
List<int> toMatch =
4699
[
@@ -50,12 +103,12 @@ public void HandleBlackMatches_Should_Find2BlackMatches()
50103
];
51104
int selection = 0b_000001_010000_010000_000001;
52105

53-
List<int> actual = CodeBreakerAlgorithms.HandleBlackMatches(toMatch, 2, selection);
106+
List<int> actual = CodeBreakerAlgorithms.HandleBlackMatches(toMatch, GameType.Game6x4, 2, selection);
54107
Assert.Equal(2, actual.Count);
55108
}
56109

57110
[Fact]
58-
public void HandleBlackMatches_Should_Find3BlackMatches()
111+
public void HandleBlackMatches_Should_Find3BlackMatches_Game6x4()
59112
{
60113
List<int> toMatch =
61114
[
@@ -65,24 +118,24 @@ public void HandleBlackMatches_Should_Find3BlackMatches()
65118
];
66119
int selection = 0b_000001_100000_010000_000001;
67120

68-
List<int> actual = CodeBreakerAlgorithms.HandleBlackMatches(toMatch, 3, selection);
121+
List<int> actual = CodeBreakerAlgorithms.HandleBlackMatches(toMatch, GameType.Game6x4, 3, selection);
69122
Assert.Single(actual);
70123
}
71124

72125
[Fact]
73-
public void HandleBlackMatches_Should_BeEmpty()
126+
public void HandleBlackMatches_Should_BeEmpty_Game6x4()
74127
{
75128
List<int> toMatch =
76129
[
77130
0b_000100_010000_001000_000010
78131
];
79132
int selection = 0b_000001_010000_001000_001000;
80-
List<int> actual = CodeBreakerAlgorithms.HandleBlackMatches(toMatch, 1, selection);
133+
List<int> actual = CodeBreakerAlgorithms.HandleBlackMatches(toMatch, GameType.Game6x4, 1, selection);
81134
Assert.Empty(actual);
82135
}
83136

84137
[Fact]
85-
public void HandleWhiteMatches_Should_Find1WhiteMatches()
138+
public void HandleWhiteMatches_Should_Find1WhiteMatches_Game6x4()
86139
{
87140
List<int> toMatch =
88141
[
@@ -92,21 +145,30 @@ public void HandleWhiteMatches_Should_Find1WhiteMatches()
92145
];
93146
int selection = 0b_000001_010000_000001_000001;
94147

95-
List<int> actual = CodeBreakerAlgorithms.HandleWhiteMatches(toMatch, 1, selection);
148+
List<int> actual = CodeBreakerAlgorithms.HandleWhiteMatches(toMatch, GameType.Game6x4, 1, selection);
96149
Assert.Equal(2, actual.Count);
97150
}
98151

99152
[Fact]
100-
public void IntToColors_Should_ConvertToCorrectColor()
153+
public void IntToColors_Should_ConvertToCorrectColor_Game6x4()
101154
{
102155
int value = 0b_000100_010000_000001_100000;
156+
Dictionary<int, string> colorNames = new()
157+
{
158+
{ 0b_000001, "Black" },
159+
{ 0b_000010, "White" },
160+
{ 0b_000100, "Red" },
161+
{ 0b_001000, "Green" },
162+
{ 0b_010000, "Blue" },
163+
{ 0b_100000, "Yellow" }
164+
};
103165
string[] expected = ["Red", "Blue", "Black", "Yellow"];
104-
string[] actual = CodeBreakerAlgorithms.IntToColors(value);
166+
string[] actual = CodeBreakerAlgorithms.IntToColors(value, GameType.Game6x4, colorNames);
105167
Assert.Equal(expected, actual);
106168
}
107169

108170
[Fact]
109-
public void HandleNoMatches_Should_MatchOneResult()
171+
public void HandleNoMatches_Should_MatchOneResult_Game6x4()
110172
{
111173
List<int> toMatch =
112174
[
@@ -115,7 +177,7 @@ public void HandleNoMatches_Should_MatchOneResult()
115177
0b_001000_001000_001000_001000 // hit
116178
];
117179
int selection = 0b_000100_010000_000001_100000;
118-
List<int> actual = CodeBreakerAlgorithms.HandleNoMatches(toMatch, selection);
180+
List<int> actual = CodeBreakerAlgorithms.HandleNoMatches(toMatch, GameType.Game6x4, selection);
119181
Assert.Single(actual);
120182
}
121183

0 commit comments

Comments
 (0)