/
Player.cs
175 lines (155 loc) · 6.66 KB
/
Player.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hanabi
{
public class Move
{
public HeldCard Play { get; private set; }
public HeldCard Discard { get; private set; }
public Hint Hint { get; private set; }
// Factory methods.
private Move() {}
public static Move DoPlay(HeldCard c) { return new Move{ Play = c }; }
public static Move DoDiscard(HeldCard c) { return new Move { Discard = c }; }
public static Move DoHint(Hint h) { return new Move { Hint = h }; }
}
public class Player
{
public Player(Game g, int idx)
{
this.Strategy = new Steller();
this.Game = g;
this.Index = idx;
this.Cards = new List<HeldCard>();
}
private IStrategy Strategy;
public Game Game { get; private set; }
public int Index { get; private set; }
public List<HeldCard> Cards { get; private set; }
// Public-facing hand of cards.
public List<Card> Hand() { return this.Cards.Select(c => c.Actual).ToList(); }
// Next player in the order.
public Player Next() { return this.Game.Players[(this.Index + 1) % this.Game.Players.Count]; }
public void Go()
{
Move m = this.Strategy.Play(this);
if (m.Play != null)
{
this.Cards.Remove(m.Play);
this.AddCard(this.Game.Play(m.Play.Actual));
}
else if (m.Discard != null)
{
this.Cards.Remove(m.Discard);
this.AddCard(this.Game.Discard(m.Discard.Actual));
}
else
{
this.Game.Hint(m.Hint);
}
}
public void GetHint(Hint h)
{
// TODO: store a history of received hints ... do we need a full move history?
for (int i = 0; i < this.Cards.Count; i++)
{
HeldCard card = this.Cards[i];
bool match = h.Positions.Contains(i);
int value = h.Number.HasValue ? h.Number.Value : h.Suit.Value;
Func<Card, int> get = c => h.Number.HasValue ? c.Number : c.Suit;
// If the hint matches this card, remove all other values.
// If the hint does not match this card, remove the hinted value.
card.Possible.RemoveAll(c => match ? get(c) != value : get(c) == value);
}
}
public void AddCard(Card c)
{
if (c == null) return;
this.Cards.Add(new HeldCard(c));
}
}
interface IStrategy
{
// Do something when it's your turn.
Move Play(Player me);
}
public class AlwaysPlay : IStrategy
{
public Move Play(Player me) { return Move.DoPlay(me.Cards[0]); }
}
public class AlwaysDiscard : IStrategy
{
public Move Play(Player me) { return Move.DoDiscard(me.Cards[0]); }
}
public class Steller : IStrategy
{
public Move Play(Player me)
{
// Narrow down card possibilities by process of elimination.
me.RestrictCards();
// Re-evaluate all cards based on new information in hand, board, and opponents' hands.
foreach (HeldCard c in me.Cards)
{
c.Label = c.State(me);
}
// TODO: override states based on last hint (requires history)
// TODO: immediately PLAY on recent hints according to position?
// Keep any queued cards that have a known number or suit (likely received a hint).
foreach (HeldCard c in me.Cards.Where(c =>
c.Label == Intent.QUEUE && (c.Number().HasValue || c.Suit().HasValue)))
{
c.Label = Intent.KEEP;
}
Player next = me.Next();
// Send critical hints, if possible.
if (me.Game.Hints > 0)
{
// For playable cards, or cards wrongly marked playable, hint number, then suit (top of queue first).
foreach (HeldCard c in next.Cards)
{
bool canPlay = me.Game.Deck.AllowPlay(c.Actual);
if ((canPlay && c.Label != Intent.PLAY) ||
(!canPlay && c.Label == Intent.PLAY))
{
if (!c.Number().HasValue) return Move.DoHint(new Hint(next, null, c.Actual.Number));
if (!c.Suit().HasValue) return Move.DoHint(new Hint(next, c.Actual.Suit, null));
}
}
// For pole cards that shouldn't be discarded, hint suit. This should move them into state KEEP.
// Only hint when there are no PLAY or DISCARD cards, since these are higher priority.
// TODO: enable this once we have better hinting. At the moment it does not help.
/*
if (next.Cards.FirstOrDefault(c => c.Label == Intent.PLAY || c.Label == Intent.DISCARD) == null)
{
var pole = next.Cards.FirstOrDefault(c => c.Label == Intent.QUEUE);
if (pole != null && pole.Actual.LastChance(next, me))
{
//if (!pole.Number().HasValue) return Move.DoHint(new Hint(next, null, pole.Actual.Number));
if (!pole.Suit().HasValue) return Move.DoHint(new Hint(next, pole.Actual.Suit, null));
}
}
*/
}
// Play cards that are ready (lowest number first).
HeldCard play = me.Cards.Where(c => c.Label == Intent.PLAY)
.OrderBy(c => c.Number().GetValueOrDefault(int.MaxValue))
.FirstOrDefault();
if (play != null) return Move.DoPlay(play);
// Discard when we have cards ready to go (any order).
HeldCard disc = me.Cards.FirstOrDefault(c => c.Label == Intent.DISCARD);
if (disc != null) return Move.DoDiscard(disc);
// TODO: send non-critical hints, like suit, especially when many hints are available
// TODO: special hints for 5s?
// Otherwise discard the first card from queue.
HeldCard discq = me.Cards.FirstOrDefault(c => c.Label == Intent.QUEUE);
if (discq != null) return Move.DoDiscard(discq);
// TODO: Non-critical hint?
// If we really have to, discard any card from the keep stack.
// TODO: discard the _safest_ card (highest, or least played).
return Move.DoPlay(me.Cards[0]);
}
}
}