/
simple_npc.go
43 lines (37 loc) · 1.28 KB
/
simple_npc.go
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
package interaction
import (
"github.com/joshprzybyszewski/cribbage/logic/strategy"
"github.com/joshprzybyszewski/cribbage/model"
"github.com/joshprzybyszewski/cribbage/utils/rand"
)
var _ npc = (*simpleNPC)(nil)
type simpleNPC struct{}
func (npc *simpleNPC) getBuildCribAction(hand []model.Card, isDealer bool) (model.BuildCribAction, error) {
return cribActionHelper(hand, Simple, isDealer)
}
func (npc *simpleNPC) getPegAction(unpegged []model.Card, prevPegs []model.PeggedCard, curPeg int) model.PegAction {
// try random strategies until we either have to say go or have a valid peg card
card, sayGo := randomPegStrategy(unpegged, prevPegs, curPeg)
for card.PegValue()+curPeg > 31 && !sayGo {
card, sayGo = randomPegStrategy(unpegged, prevPegs, curPeg)
}
return model.PegAction{
Card: card,
SayGo: sayGo,
}
}
func randomPegStrategy(hand []model.Card, prevPegs []model.PeggedCard, curPeg int) (model.Card, bool) {
var card model.Card
var sayGo bool
switch rand.Int() % 4 {
case 0:
card, sayGo = strategy.PegToFifteen(hand, prevPegs, curPeg)
case 1:
card, sayGo = strategy.PegToThirtyOne(hand, prevPegs, curPeg)
case 2:
card, sayGo = strategy.PegToPair(hand, prevPegs, curPeg)
default:
card, sayGo = strategy.PegToRun(hand, prevPegs, curPeg)
}
return card, sayGo
}