-
Notifications
You must be signed in to change notification settings - Fork 0
/
Direction.cs
47 lines (39 loc) · 1.2 KB
/
Direction.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
using System;
using System.Collections.Generic;
namespace KSnakeGame;
public class Direction
{
public readonly static Direction Left = new Direction(0, -1);
public readonly static Direction Right = new Direction(0, 1);
public readonly static Direction Up = new Direction(-1, 0);
public readonly static Direction Down = new Direction(1, 0);
public int RowOffset { get; }
public int ColOffset { get; }
private Direction(int rowOffset, int colOffset)
{
RowOffset = rowOffset;
ColOffset = colOffset;
}
public Direction Opposite()
{
return new Direction(-RowOffset, -ColOffset);
}
public override bool Equals(object obj)
{
return obj is Direction direction &&
RowOffset == direction.RowOffset &&
ColOffset == direction.ColOffset;
}
public override int GetHashCode()
{
return HashCode.Combine(RowOffset, ColOffset);
}
public static bool operator ==(Direction left, Direction right)
{
return EqualityComparer<Direction>.Default.Equals(left, right);
}
public static bool operator !=(Direction left, Direction right)
{
return !(left == right);
}
}