initial commit
This commit is contained in:
commit
ac9aa86d33
14 changed files with 1021 additions and 0 deletions
81
internal/game/game.go
Normal file
81
internal/game/game.go
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
package game
|
||||
|
||||
type GameState int
|
||||
|
||||
const (
|
||||
StateWaiting GameState = iota
|
||||
StatePlaying
|
||||
)
|
||||
|
||||
type Game struct {
|
||||
ID string
|
||||
Player1 *Player
|
||||
Player2 *Player
|
||||
Current int // 1 or 2, whose turn it is
|
||||
State GameState
|
||||
Deck []Card
|
||||
BookPile [][]Card
|
||||
}
|
||||
|
||||
func NewGame(p1, p2 *Player) *Game {
|
||||
deck := Shuffle(NewDeck())
|
||||
return &Game{
|
||||
ID: "",
|
||||
Player1: p1,
|
||||
Player2: p2,
|
||||
Current: 1,
|
||||
State: StatePlaying,
|
||||
Deck: deck,
|
||||
BookPile: [][]Card{},
|
||||
}
|
||||
}
|
||||
|
||||
func (g *Game) DrawCard() Card {
|
||||
if len(g.Deck) == 0 {
|
||||
// Reshuffle book pile
|
||||
var all []Card
|
||||
for _, book := range g.BookPile {
|
||||
all = append(all, book...)
|
||||
}
|
||||
if len(all) > 0 {
|
||||
g.Deck = Shuffle(all)
|
||||
g.BookPile = [][]Card{}
|
||||
}
|
||||
}
|
||||
card := g.Deck[0]
|
||||
g.Deck = g.Deck[1:]
|
||||
return card
|
||||
}
|
||||
|
||||
func (g *Game) Ask(target *Player, rank Rank) bool {
|
||||
if !target.HasCard(rank) {
|
||||
// Player draws from deck
|
||||
card := g.DrawCard()
|
||||
target.Draw(card)
|
||||
return false // turn passes
|
||||
}
|
||||
// Target gives the card
|
||||
removed := target.RemoveCard(rank)
|
||||
if len(removed) > 0 {
|
||||
// Add to asker's hand
|
||||
g.Player1.Hand = append(g.Player1.Hand, removed...)
|
||||
// Check for book
|
||||
if g.Player1.HasBook(rank) {
|
||||
// Find and remove the 4 cards from hand
|
||||
var book []Card
|
||||
var remaining []Card
|
||||
count := 0
|
||||
for _, c := range g.Player1.Hand {
|
||||
if c.Rank == rank && count < 4 {
|
||||
book = append(book, c)
|
||||
count++
|
||||
} else {
|
||||
remaining = append(remaining, c)
|
||||
}
|
||||
}
|
||||
g.Player1.Hand = remaining
|
||||
g.BookPile = append(g.BookPile, book)
|
||||
}
|
||||
}
|
||||
return true // same player continues
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue