-
Notifications
You must be signed in to change notification settings - Fork 1
/
Box.cs
73 lines (65 loc) · 1.98 KB
/
Box.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
namespace SudokuSpice
{
/// <summary>
/// Defines a square region within a puzzle.
/// </summary>
public readonly struct Box : IEquatable<Box>
{
/// <summary>
/// The top-left coordinate (inclusive) for this box.
/// </summary>
public readonly Coordinate TopLeft { get; }
/// <summary>
/// The size (i.e. side-length) of this box.
/// </summary>
public readonly int Size { get; }
/// <summary>
/// Constructs a box covering the given region. <paramref name="size"/> must be greater
/// than 0.
/// </summary>
public Box(Coordinate topLeft, int size)
{
if (size == 0)
{
throw new ArgumentException($"{nameof(Box)} must have a size > 0.");
}
TopLeft = topLeft;
Size = size;
}
/// <summary>
/// Returns true if the given coordinate is part of the box.
/// </summary>
public readonly bool Contains(in Coordinate coord)
{
return coord.Row >= TopLeft.Row
&& coord.Column >= TopLeft.Column
&& coord.Row < TopLeft.Row + Size
&& coord.Column < TopLeft.Column + Size;
}
public override bool Equals(object? obj)
{
if (obj is Box other)
{
return Equals(other);
}
return false;
}
public bool Equals(Box other)
{
return Size == other.Size && TopLeft == other.TopLeft;
}
public override int GetHashCode()
{
return TopLeft.GetHashCode() + Size;
}
public static bool operator ==(Box left, Box right)
{
return left.Equals(right);
}
public static bool operator !=(Box left, Box right)
{
return left.Size != right.Size || left.TopLeft != right.TopLeft;
}
}
}