Skip to content

Commit

Permalink
Minor refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
blynn committed Sep 29, 2012
1 parent 3a01d0d commit 9245051
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 43 deletions.
6 changes: 1 addition & 5 deletions cards_base.go
Expand Up @@ -43,11 +43,7 @@ Adventurer,6,Action
Fun: map[string]func(game *Game){
"Cellar": func(game *Game) {
p := game.p
selected := game.pickHand(p, pickOpts{n: len(p.hand)})
if len(selected) > 0 {
game.DiscardList(p, selected)
game.draw(p, len(selected))
}
game.draw(p, len(game.DiscardList(p, game.pickHand(p, pickOpts{n: len(p.hand)}))))
},
"Chapel": func(game *Game) {
game.TrashList(game.p, game.pickHand(game.p, pickOpts{n: 4}))
Expand Down
11 changes: 2 additions & 9 deletions cards_intrigue.go
Expand Up @@ -57,11 +57,7 @@ Nobles,6,Action-Victory,#2
},
"Secret Chamber": func(game *Game) {
p := game.p
selected := game.pickHand(p, pickOpts{n: len(p.hand)})
if len(selected) > 0 {
game.DiscardList(p, selected)
game.c += len(selected)
}
game.c += len(game.DiscardList(p, game.pickHand(p, pickOpts{n: len(p.hand)})))
},
"Masquerade": func(game *Game) {
m := len(game.players)
Expand Down Expand Up @@ -239,10 +235,7 @@ Nobles,6,Action-Victory,#2
v := game.getInts(other, "discard 2; gain Curse in hand", 1)
switch v[0] - 1 {
case 0:
selected := game.pickHand(other, pickOpts{n: 2, exact: true})
if len(selected) > 0 {
game.DiscardList(other, selected)
}
game.DiscardList(other, game.pickHand(other, pickOpts{n: 2, exact: true}))
case 1:
game.MaybeGain(other, GetCard("Curse"))
}
Expand Down
59 changes: 31 additions & 28 deletions main.go
Expand Up @@ -125,13 +125,14 @@ func (game *Game) TrashList(p *Player, list Pile) {
}
}

func (game *Game) DiscardList(p *Player, list Pile) {
func (game *Game) DiscardList(p *Player, list Pile) Pile {
if len(list) > 0 {
p.discard = append(p.discard, list...)
// TODO: Race condition: what if player shuffles discards into deck
// while another is trying look at the top discard?
game.Report(Event{s: "discard", n: p.n, i: len(list)})
}
return list
}

func (game *Game) LeftOf(p *Player) *Player {
Expand Down Expand Up @@ -321,35 +322,37 @@ func (game *Game) Report(ev Event) {

func (game *Game) draw(p *Player, n int) int {
count := 0
if game.isServer {
s := ""
sSecret := ""
i := 0
for ; i < n && p.MaybeShuffle(); i++ {
c := p.deck[0]
p.deck, p.hand = p.deck[1:], append(p.hand, c)
s += string(c.key)
sSecret += "?"
}
game.castCond(func(x *Player) bool { return x == p }, "draw", s)
game.castCond(func(x *Player) bool { return x != p }, "draw", sSecret)
count = i
} else {
w := game.fetch()
for _, b := range []byte(w[0]) {
if len(p.deck) == 0 {
p.deck, p.discard = p.discard, nil
if n > 0 {
if game.isServer {
s := ""
sSecret := ""
i := 0
for ; i < n && p.MaybeShuffle(); i++ {
c := p.deck[0]
p.deck, p.hand = p.deck[1:], append(p.hand, c)
s += string(c.key)
sSecret += "?"
}
p.deck = p.deck[1:]
if b != '?' {
p.hand = append(p.hand, game.keyToCard(b))
} else {
p.hand = append(p.hand, nil)
game.castCond(func(x *Player) bool { return x == p }, "draw", s)
game.castCond(func(x *Player) bool { return x != p }, "draw", sSecret)
count = i
} else {
w := game.fetch()
for _, b := range []byte(w[0]) {
if len(p.deck) == 0 {
p.deck, p.discard = p.discard, nil
}
p.deck = p.deck[1:]
if b != '?' {
p.hand = append(p.hand, game.keyToCard(b))
} else {
p.hand = append(p.hand, nil)
}
}
count = len(w[0])
}
count = len(w[0])
game.Report(Event{s: "draw", n: p.n, i: count})
}
game.Report(Event{s: "draw", n: p.n, i: count})
return count
}

Expand Down Expand Up @@ -910,9 +913,9 @@ func main() {
out: make(chan string),
}
game.players = []*Player{
//&Player{name: "Anonymous", fun: ng},
&Player{name: "Anonymous", fun: ng},
&Player{name: "Ben", fun: consoleGamer{}},
&Player{name: "AI", fun: SimpleBuyer{[]string{"Province", "Gold", "Silver"}}},
//&Player{name: "AI", fun: SimpleBuyer{[]string{"Province", "Gold", "Silver"}}},
}
players := game.players

Expand Down
2 changes: 1 addition & 1 deletion simple.go
Expand Up @@ -55,7 +55,7 @@ func (this SimpleBuyer) start(game *Game, p *Player) {
}
for _, s := range this.list {
c := GetCard(s)
if game.c >= game.Cost(c) {
if game.c >= game.Cost(c) && c.supply > 0 {
return Command{s: "buy", c: c}
}
}
Expand Down

0 comments on commit 9245051

Please sign in to comment.