From 9e659ecf41b1f7c7037263cc0ebd1778732847a7 Mon Sep 17 00:00:00 2001 From: Kistaro Windrider Date: Sat, 1 Apr 2023 22:19:42 -0700 Subject: [PATCH] InitPlayer InitPlayer creates very not-ready Player with its basic data structures initialized to an empty, ready-for-data state. --- cardsim/player.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/cardsim/player.go b/cardsim/player.go index a0eeaac..539a780 100644 --- a/cardsim/player.go +++ b/cardsim/player.go @@ -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