mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 15:16:33 -08:00
Add Memory and Matrix
This commit is contained in:
@@ -2,21 +2,12 @@ namespace Digits;
|
||||
|
||||
internal class Guesser
|
||||
{
|
||||
private readonly IReadOnlyList<int> _weights = new List<int> { 0, 1, 3 }.AsReadOnly();
|
||||
private readonly int[][,] _matrices = new[] { new int[3, 3], new int[9, 3], new int[27, 3] };
|
||||
private readonly int[] _indices = new[] { 2, 8, 26 };
|
||||
private readonly Memory _matrices = new();
|
||||
private readonly IRandom _random;
|
||||
|
||||
public Guesser(IRandom random)
|
||||
{
|
||||
_random = random;
|
||||
|
||||
for (int j = 0; j < 3; j++)
|
||||
{
|
||||
for (int i = 0; i < 3; i++) { _matrices[0][i, j] = 9; }
|
||||
for (int i = 0; i < 9; i++) { _matrices[1][i, j] = i == 4 * j ? 2 : 3; }
|
||||
for (int i = 0; i < 27; i++) { _matrices[2][i, j] = 1; }
|
||||
}
|
||||
}
|
||||
|
||||
public int GuessNextDigit()
|
||||
@@ -24,29 +15,18 @@ internal class Guesser
|
||||
var currentSum = 0;
|
||||
var guess = 0;
|
||||
|
||||
for (int j = 0; j < 3; j++)
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
var sum = Enumerable.Range(0, 3).Aggregate((s, i) => s + GetWeightedValue(i, j));
|
||||
var sum = _matrices.GetWeightedSum(i);
|
||||
if (sum > currentSum || _random.NextFloat() >= 0.5)
|
||||
{
|
||||
currentSum = sum;
|
||||
guess = j;
|
||||
guess = i;
|
||||
}
|
||||
}
|
||||
|
||||
return guess;
|
||||
}
|
||||
|
||||
public void ObserveActualDigit(int digit)
|
||||
{
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
_matrices[i][_indices[i], digit]++;
|
||||
}
|
||||
_indices[2] = _indices[2] % 9 * 3 + digit;
|
||||
_indices[1] = _indices[2] % 9;
|
||||
_indices[0] = digit;
|
||||
}
|
||||
|
||||
private int GetWeightedValue(int matrix, int row) => _weights[matrix] * _matrices[matrix][_indices[matrix], row];
|
||||
public void ObserveActualDigit(int digit) => _matrices.ObserveDigit(digit);
|
||||
}
|
||||
27
34_Digits/csharp/Matrix.cs
Normal file
27
34_Digits/csharp/Matrix.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
namespace Digits;
|
||||
|
||||
internal class Matrix
|
||||
{
|
||||
private readonly int _weight;
|
||||
private readonly int[,] _values;
|
||||
|
||||
public Matrix(int width, int weight, Func<int, int, int> seedFactory)
|
||||
{
|
||||
_weight = weight;
|
||||
_values = new int[width, 3];
|
||||
|
||||
for (int i = 0; i < width; i++)
|
||||
for (int j = 0; j < 3; j++)
|
||||
{
|
||||
_values[i, j] = seedFactory.Invoke(i, j);
|
||||
}
|
||||
|
||||
Index = width - 1;
|
||||
}
|
||||
|
||||
public int Index { get; set; }
|
||||
|
||||
public int GetWeightedValue(int row) => _weight * _values[Index, row];
|
||||
|
||||
public int IncrementValue(int row) => _values[Index, row]++;
|
||||
}
|
||||
30
34_Digits/csharp/Memory.cs
Normal file
30
34_Digits/csharp/Memory.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
namespace Digits;
|
||||
|
||||
public class Memory
|
||||
{
|
||||
private readonly Matrix[] _matrices;
|
||||
|
||||
public Memory()
|
||||
{
|
||||
_matrices = new[]
|
||||
{
|
||||
new Matrix(27, 3, (_, _) => 1),
|
||||
new Matrix(9, 1, (i, j) => i == 4 * j ? 2 : 3),
|
||||
new Matrix(3, 0, (_, _) => 9)
|
||||
};
|
||||
}
|
||||
|
||||
public int GetWeightedSum(int row) => _matrices.Select(m => m.GetWeightedValue(row)).Sum();
|
||||
|
||||
public void ObserveDigit(int digit)
|
||||
{
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
_matrices[i].IncrementValue(digit);
|
||||
}
|
||||
|
||||
_matrices[0].Index = _matrices[0].Index % 9 * 3 + digit;
|
||||
_matrices[1].Index = _matrices[0].Index % 9;
|
||||
_matrices[2].Index = digit;
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
|
||||
I guessed more than 1/3 of your numbers.
|
||||
I win.
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
|
||||
I guessed exactly 1/3 of your numbers.
|
||||
It's a tie game.
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
|
||||
I guessed less than 1/3 of your numbers.
|
||||
You beat me. Congratulations *****
|
||||
|
||||
|
||||
Reference in New Issue
Block a user