Skip to content

Commit bb08df1

Browse files
committed
implementing IEquatable, calm nullable warnings
1 parent 392f922 commit bb08df1

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

src/Evaluation.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
using Lang;
2+
using System.Diagnostics.CodeAnalysis;
23

34
namespace MastermindVariante
45
{
5-
public struct Points : IComparable<Points>
6+
public readonly struct Points : IComparable<Points>, IEquatable<Points>
67
{
78
public short blackpins { get; init; }
89
public short whitepins { get; init; }
@@ -13,13 +14,36 @@ public Points(short blacks, short whites)
1314
this.whitepins = whites;
1415
}
1516

16-
public int CompareTo(Points other)
17+
public readonly int CompareTo(Points other)
1718
{
1819
if (this.blackpins < other.blackpins) return -1;
1920
if (this.blackpins == other.blackpins)
2021
return this.whitepins.CompareTo(other.whitepins);
2122
else return 1;
2223
}
24+
25+
public readonly bool Equals(Points other)
26+
{
27+
return other is Points p && p.blackpins == blackpins && p.whitepins == whitepins;
28+
}
29+
30+
public readonly override bool Equals(object? obj)
31+
=> obj is Points objS && Equals(objS);
32+
33+
public static bool operator== (Points a, Points b)
34+
{
35+
return a.Equals(b);
36+
}
37+
38+
public static bool operator!= (Points a, Points b)
39+
{
40+
return !(a == b);
41+
}
42+
43+
public override readonly int GetHashCode()
44+
{
45+
return HashCode.Combine(blackpins, whitepins);
46+
}
2347
}
2448
public class Evaluation
2549
{

src/GuessedRow.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
public partial class GuessedRow : Parameter, ILetters, IMMDispose
44
{
55
private static short rowIndex = -1;
6+
67
internal static Form1? Caller { get; private set; }
78
private readonly List<Piece> pieces;
89
private readonly List<ResultPin> pins;
@@ -32,8 +33,8 @@ public GuessedRow(Form1 _caller)
3233

3334
for (short i = 0; i < level; i++)
3435
{
35-
pieces.Add(new Piece(Caller, rowIndex, i));
36-
pins.Add(new ResultPin(Caller, rowIndex, i));
36+
pieces.Add(new Piece(Caller!, rowIndex, i));
37+
pins.Add(new ResultPin(Caller!, rowIndex, i));
3738
}
3839

3940
FormatObjects();
@@ -114,7 +115,7 @@ internal bool Calculate()
114115
{
115116
foreach (var x in pieces)
116117
{
117-
if (!(ExcludedChars!.Contains(x.GetLetter()[0])))
118+
if (!ExcludedChars!.Contains(x.GetLetter()[0]))
118119
{
119120
ExcludedChars.Add(x.GetLetter()[0]);
120121
}

0 commit comments

Comments
 (0)