Created
December 18, 2017 10:24
-
-
Save cirias/29e1728aac0a0e3d4355ea0689661907 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package hanabi | |
import ( | |
"math/rand" | |
"time" | |
) | |
const ( | |
white = iota | |
yellow | |
green | |
blue | |
red | |
) | |
type Card struct { | |
color int | |
number int | |
} | |
type Action interface { | |
Apply(*Game) | |
} | |
type Knowledge struct { | |
Type string | |
Cards []int | |
Value int | |
} | |
type GameInfo struct { | |
others [][]Card | |
played [5]int | |
infoToken int | |
fuseToken int | |
} | |
type Player interface { | |
SetGameInfo(func() GameInfo) | |
Notice(Action) | |
Learn(*Knowledge) | |
Play() Action | |
} | |
type Game struct { | |
deck []Card | |
played [5]int | |
players []Player | |
playersCards [][]Card | |
infoToken int | |
fuseToken int | |
} | |
func NewGame(players []Player) *Game { | |
cardsPerNumber := []int{3, 2, 2, 2, 1} | |
deck := make([]Card, 0) | |
for color := 0; color < 6; color += 1 { | |
for i, count := range cardsPerNumber { | |
for j := 0; j < count; j += 1 { | |
deck = append(deck, Card{ | |
color: color, | |
number: i + 1, | |
}) | |
} | |
} | |
} | |
rand.Seed(time.Now().Unix()) | |
for i := range deck { | |
j := rand.Intn(i + 1) | |
deck[i], deck[j] = deck[j], deck[i] | |
} | |
playerCount := len(players) | |
if playerCount < 2 || playerCount > 5 { | |
return nil | |
} | |
cardsPerPlayer := 5 | |
if playerCount < 4 { | |
cardsPerPlayer = 4 | |
} | |
playersCards := make([][]Card, len(players)) | |
for i, _ := range playersCards { | |
playersCards[i] = deck[:cardsPerPlayer] | |
deck = deck[cardsPerPlayer:] | |
} | |
return &Game{ | |
deck: deck, | |
players: players, | |
playersCards: playersCards, | |
infoToken: 8, | |
fuseToken: 3, | |
} | |
} | |
func (g *Game) End() bool { | |
return false | |
} | |
func (g *Game) Start() { | |
currentPlayer := 0 | |
for !g.End() { | |
action := g.players[currentPlayer].Play() | |
action.Apply(g) | |
for i, p := range g.players { | |
if i == currentPlayer { | |
continue | |
} | |
// TODO learn | |
// if k != nil | |
p.Notice(action) | |
} | |
currentPlayer += 1 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment