-
Notifications
You must be signed in to change notification settings - Fork 2
/
game.go
87 lines (68 loc) · 1.68 KB
/
game.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package rung
import (
"github.com/minhajuddinkhan/pattay"
)
//Game a game of court piece
type Game interface {
//Players returns the players
Players() []pattay.Player
//DistributeCards distrubutes card among players of the game
DistributeCards() error
//PlayHand begins play the hand
PlayHand(turn int, trump *string, lastHead pattay.Player) (Hand, error)
//ShuffleDeck shuffes the deck n times
ShuffleDeck(n int) error
//HandsOnGround returns the hands on ground that are not won yet.
HandsOnGround() []Hand
//HandsWonBy returns the number of hands won by a player
HandsWonBy(player pattay.Player) int
}
type game struct {
players []pattay.Player
deck pattay.Deck
handsOnGround []Hand
handsWon map[string]int
ring pattay.Ring
}
//NewGame NewGame
func NewGame() Game {
deck := pattay.NewDeck()
players := makePlayers()
r := makeRing(players)
return &game{
ring: r,
players: players,
deck: deck,
handsWon: make(map[string]int, 4),
}
}
func (g *game) PlayHand(turn int, trump *string, lastHead pattay.Player) (Hand, error) {
hand := NewHand(trump)
cardsDelt := 0
if isFirstHand(turn) {
g.playFirstHand(hand)
cardsDelt++
}
if cardsDelt == 0 {
g.ring.SetCurrentPlayer(lastHead)
}
for i := 0; i < 4-cardsDelt; i++ {
if err := g.playRing(cardsDelt, hand); err != nil {
return nil, err
}
}
g.handsOnGround = append(g.handsOnGround, hand)
head, err := hand.Head()
if err != nil {
return nil, err
}
g.ring.SetCurrentPlayer(head)
if !canWinHand(turn) {
return hand, nil
}
if head.Name() == lastHead.Name() {
g.handsWon[lastHead.Name()] = len(g.handsOnGround)
g.handsOnGround = nil
}
return hand, nil
}