-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.go
70 lines (63 loc) · 1.62 KB
/
player.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
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
package poner
import (
"math"
"math/rand"
"sort"
"time"
)
// Player holds the data for a player in the game
type Player struct {
Name string
Score int
LastScore int
GamesWon int
DealtHand Hand
PlayingHand Hand
Discard Discard
Gone bool
IsComputer bool
SkillLevel int
}
// AddScore adds scores to the player's total
func (player *Player) AddScore(scores []Score) (total int) {
for _, score := range scores {
total += score.Value
}
if total == 0 {
return
}
player.LastScore = player.Score
player.Score += total
return
}
// TakeDeal gives dealt cards to a player
func (player *Player) TakeDeal(hand Hand, deck *Deck, isDealer bool) {
player.DealtHand = hand
if player.IsComputer {
discards := hand.GetDiscards(deck, isDealer)
skillAdjust := player.GetSkillAdjust(len(discards))
player.SetDiscard(discards[skillAdjust])
} else {
player.Discard = Discard{}
player.PlayingHand = Hand{}
}
}
// SetDiscard set's the player's discard and playing hand
func (player *Player) SetDiscard(discard Discard) {
playingHand := Hand{}
for _, card := range discard.Held {
playingHand = append(playingHand, card)
}
player.Discard = discard
player.PlayingHand = playingHand
sort.Sort(Hand(player.PlayingHand))
player.Gone = false
}
// GetSkillAdjust gets a random skill ajustment for player skill
func (player *Player) GetSkillAdjust(maxAdjust int) int {
rand.Seed(time.Now().UnixNano())
maxSkilllevel := math.Min(4, float64(player.SkillLevel))
largestOffset := math.Min(5-maxSkilllevel, float64(maxAdjust))
largestOffset = math.Max(largestOffset, 0)
return rand.Intn(int(largestOffset))
}