-
Notifications
You must be signed in to change notification settings - Fork 13
/
GameRules.cs
85 lines (69 loc) · 3.22 KB
/
GameRules.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
74
75
76
77
78
79
80
81
82
83
84
85
using System;
using System.Diagnostics;
namespace NumberGuessConsole
{
public static class GameRules
{
public static PlayerNumber[] GetCandidates(PlayerNumber number) =>
number.Number == 1 ?
new[] { number.ToOpponent() + 1 } :
new[] { number.ToOpponent() - 1, number.ToOpponent() + 1 };
}
[DebuggerStepThrough]
public class PlayerIdentity
{
public static PlayerIdentity A { get; } = new PlayerIdentity("A");
public static PlayerIdentity B { get; } = new PlayerIdentity("B");
public static PlayerIdentity[] Both { get; } = new[] { A, B };
static PlayerIdentity()
{
A.Opponent = B;
B.Opponent = A;
}
public string Name { get; }
public PlayerIdentity Opponent { get; private set; }
PlayerIdentity(string name)
{
Name = name;
}
public override string ToString() => Name;
}
[DebuggerStepThrough]
public struct PlayerNumber
{
public PlayerIdentity PlayerId { get; }
public int Number { get; }
public PlayerNumber(PlayerIdentity playerId, int number)
{
PlayerId = playerId;
Number = number;
}
public PlayerNumber ToOpponent() => new PlayerNumber(PlayerId.Opponent, Number);
public static PlayerNumber operator +(PlayerNumber v, int delta) => new PlayerNumber(v.PlayerId, v.Number + delta);
public static PlayerNumber operator -(PlayerNumber v, int delta) => new PlayerNumber(v.PlayerId, v.Number - delta);
public static bool operator ==(PlayerNumber v1, PlayerNumber v2) => v1.PlayerId == v2.PlayerId && v1.Number == v2.Number;
public static bool operator !=(PlayerNumber v1, PlayerNumber v2) => !(v1 == v2);
public override bool Equals(object obj) => obj is PlayerNumber && this == (PlayerNumber)obj;
public override int GetHashCode() => PlayerId.GetHashCode() ^ Number.GetHashCode();
public override string ToString() => $"{PlayerId} = {Number}";
}
[DebuggerStepThrough]
public struct PlayerTurns
{
public PlayerIdentity PlayerId { get; }
public int Turns { get; }
public PlayerTurns(PlayerIdentity playerId, int turns = 0)
{
PlayerId = playerId;
Turns = turns;
}
public PlayerTurns ToOpponent() => new PlayerTurns(PlayerId.Opponent, Turns);
public static PlayerTurns operator +(PlayerTurns v, int delta) => new PlayerTurns(v.PlayerId, v.Turns + delta);
public static PlayerTurns operator -(PlayerTurns v, int delta) => new PlayerTurns(v.PlayerId, v.Turns - delta);
public static bool operator ==(PlayerTurns v1, PlayerTurns v2) => v1.PlayerId == v2.PlayerId && v1.Turns == v2.Turns;
public static bool operator !=(PlayerTurns v1, PlayerTurns v2) => !(v1 == v2);
public override bool Equals(object obj) => obj is PlayerTurns && this == (PlayerTurns)obj;
public override int GetHashCode() => PlayerId.GetHashCode() ^ Turns.GetHashCode();
public override string ToString() => $"{PlayerId} @ {Turns}";
}
}