-
Notifications
You must be signed in to change notification settings - Fork 1
/
player.go
173 lines (138 loc) · 3.57 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
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
package pattay
import (
"fmt"
)
const (
FirstCardAtHand = iota
SecondCardAtHand = iota
ThirdCardAtHand = iota
FourthCardAtHand = iota
FifthCardAtHand = iota
SixthCardAtHand = iota
SeventhCardAtHand = iota
EidthCardAtHand = iota
NinthCardAtHand = iota
TenthCardAtHand = iota
EleventhCardAtHand = iota
TwelvthCardAtHand = iota
ThirteenthCardAtHand = iota
)
const (
SouthPlayer = "South Player"
EastPlayer = "East Player"
WestPlayer = "West Player"
NorthPlayer = "North Player"
)
//Player Player
type Player interface {
//Name returns name of the player
Name() string
//CardsAtHand returns card at hand
CardsAtHand() []Card
//DrawCard draws a card
DrawCard(i int) (Card, error)
//ReceiveCard receives a card
ReceiveCard(c Card) error
//HasHouse returns if a player has any card of the given house
HasHouse(house string) bool
//HasCard(c Card) bool
HasCard(c Card) (hasCard bool, cardAtIndex int)
//Throw enqueues throwing of a card from players deck so the game can receive it
ThrowCard(cardAt int)
//CardOnTable receives the card from the queue to be added in the game's current hand
CardOnTable() int
//AnySpade returns any spade if it has one
AnySpade() (Card, int, error)
//AnyClub returns any club if it has one
AnyClub() (Card, int, error)
//AnyHeart any heart if it has one
AnyHeart() (Card, int, error)
//AnyDiamond returns any diamond if it has one
AnyDiamond() (Card, int, error)
}
type player struct {
cardsAtHand []Card
name string
//TODO:: come up with a better name for the queue
decisionChannel chan int
}
//NewPlayer NewPlayer
func NewPlayer(name string) Player {
return &player{cardsAtHand: []Card{}, name: name, decisionChannel: make(chan int)}
}
func (p *player) Name() string {
return p.name
}
func (p *player) HasCard(c Card) (bool, int) {
for cardAtIndex, card := range p.cardsAtHand {
if IsSameCard(c, card) {
return true, cardAtIndex
}
}
return false, -1
}
func (p *player) CardsAtHand() []Card {
return p.cardsAtHand
}
func (p *player) DrawCard(i int) (Card, error) {
if len(p.cardsAtHand) <= i {
return nil, fmt.Errorf("not this many cards")
}
card := p.cardsAtHand[i]
p.cardsAtHand = append(p.cardsAtHand[:i], p.cardsAtHand[i+1:]...)
return card, nil
}
func (p *player) alreadyAtHand(c Card) bool {
for _, atHand := range p.cardsAtHand {
if IsSameCard(atHand, c) {
return true
}
}
return false
}
func (p *player) ReceiveCard(c Card) error {
if len(p.cardsAtHand) == 13 {
return fmt.Errorf("cannot receive more cards. all thirteen at hand")
}
if p.alreadyAtHand(c) {
return fmt.Errorf("cannot receive a card it already has")
}
p.cardsAtHand = append(p.cardsAtHand, c)
return nil
}
func (p *player) HasHouse(house string) bool {
for _, c := range p.CardsAtHand() {
if c.House() == house {
return true
}
}
return false
}
func (p *player) ThrowCard(cardAt int) {
go func() {
p.decisionChannel <- cardAt
}()
}
func (p *player) CardOnTable() int {
return <-p.decisionChannel
}
func (p *player) anyCardOfHouse(house string) (Card, int, error) {
for at, c := range p.CardsAtHand() {
if c.House() == house {
return c, at, nil
}
}
return nil, -1, fmt.Errorf("player doesn't have any %s cards", house)
}
func (p *player) AnyDiamond() (Card, int, error) {
return p.anyCardOfHouse(Diamond)
}
func (p *player) AnySpade() (Card, int, error) {
return p.anyCardOfHouse(Spade)
}
func (p *player) AnyClub() (Card, int, error) {
return p.anyCardOfHouse(Club)
}
func (p *player) AnyHeart() (Card, int, error) {
return p.anyCardOfHouse(Heart)
}