-
Notifications
You must be signed in to change notification settings - Fork 2
/
hand_play.go
65 lines (51 loc) · 1.05 KB
/
hand_play.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
package rung
import (
"fmt"
"github.com/minhajuddinkhan/pattay"
)
func (h *hand) AddCard(pl pattay.Player, cardAtHandIndex int) error {
c, err := pl.DrawCard(cardAtHandIndex)
if err != nil {
return err
}
if err = h.validateCard(pl, c); err != nil {
return err
}
defer func() {
h.hasPlayed = append(h.hasPlayed, pl)
h.cards = append(h.cards, c)
}()
if h.IsEmpty() {
h.house = c.House()
h.head = pl
return nil
}
if !h.isTrumpDeclared() {
if h.isSameHouse(c) {
h.setHeadForBiggestCard(h.cards, c, h.house, pl)
return nil
}
if pl.HasHouse(h.house) {
return fmt.Errorf("player has cards of the same house")
}
h.declareTrump(c, pl)
return nil
}
//if trump is running at hand
if h.trump == h.house {
if !h.isSameHouse(c) {
return nil
}
h.setHeadForBiggestCard(h.cards, c, h.trump, pl)
return nil
}
if c.House() == h.trump {
trumpCards := h.trumpCardsAtHand()
if len(trumpCards) == 0 {
h.head = pl
return nil
}
h.setHeadForBiggestCard(h.trumpCardsAtHand(), c, h.trump, pl)
}
return nil
}