InitPlayer

InitPlayer creates very not-ready Player with its basic data structures initialized to an empty, ready-for-data state.
This commit is contained in:
Kistaro Windrider 2023-04-01 22:19:42 -07:00
parent 09fdf19948
commit 9e659ecf41
Signed by: kistaro
SSH Key Fingerprint: SHA256:TBE2ynfmJqsAf0CP6gsflA0q5X5wD5fVKWPsZ7eVUg8

View File

@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"math/rand"
"time"
)
var (
@ -73,7 +74,7 @@ type Player[C StatsCollection] struct {
Name string
// Rand is a source of randomness that other components can use.
Rand rand.Rand
Rand *rand.Rand
Deck *Deck[C]
Hand []Card[C]
@ -151,6 +152,27 @@ const (
GameStalled
)
// InitPlayer returns a mostly-uninitialized Player with the fields
// that require specific initialization already configured and the
// provided StatsCollection (if any) already assigned to its Stats.
// Most fields are not configured and need to be assigned after this.
//
// The Player is initialized with an empty deck, empty rule collection,
// a hand limit of 1, an actions-per-turn limit of 1, and a random
// number generator seeded with the nanosecond component of the current time.
// The Deck shares this random number generator.
func InitPlayer[C StatsCollection](stats C) *Player[C] {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return &Player[C]{
Stats: stats,
Rand: r,
Deck: &Deck[C]{rand: r},
HandLimit: 1,
ActionsPerTurn: 1,
Rules: NewRuleCollection[C](),
}
}
// Over returns whether this state represents a game that is over.
func (g GameState) Over() bool {
return g == GameLost || g == GameWon || g == GameCrashed || g == GameStalled