-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoveValidator.cs
More file actions
64 lines (58 loc) · 1.69 KB
/
Copy pathMoveValidator.cs
File metadata and controls
64 lines (58 loc) · 1.69 KB
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
using System.Net.Sockets;
namespace Elevens.Core;
public sealed class MoveValidator
{
public bool isValidSelection(IReadOnlyList<Card> selected)
{
if(selected == null)
return false;
if(selected.Count == 2)
return IsValidPair(selected[0], selected[1]);
if(selected.Count == 3)
return IsValidTriple(selected);
return false;
}
public bool HasLegalMoves(IReadOnlyList<Card> tableCards)
{
if (tableCards == null || tableCards.Count == 0) return false;
//check pair that sums to 11
for(int i = 0; i < tableCards.Count; i++)
{
for (int j = i+1; j < tableCards.Count; j++)
{
if(IsValidPair(tableCards[i], tableCards[j]))
{
return true;
}
}
}
//check J,Q,K triple
bool hasJ = tableCards.Any(c => c.IsJack);
bool hasQ = tableCards.Any(c => c.IsQueen);
bool hasK = tableCards.Any(c => c.IsKing);
if (hasJ && hasQ && hasK)
{
return true;
}
else
{
return false;
}
}
private bool IsValidPair(Card a, Card b)
{
if (a == null || b == null) return false;
return a.ValueForEleven + b.ValueForEleven == 11;
}
private bool IsValidTriple(IReadOnlyList<Card> three)
{
bool hasJ = false, hasQ = false, hasK = false;
foreach (var card in three)
{
if (card.IsJack) hasJ = true;
if (card.IsQueen) hasQ = true;
if (card.IsKing) hasK = true;
}
return hasJ && hasQ && hasK;
}
}