Compare commits

..

No commits in common. "22c4718faf74f86eeea076a77150684d92a00a34" and "8d9303c8bca6565c46b392a30341205d6ce7dffe" have entirely different histories.

5 changed files with 53 additions and 172 deletions

View File

@ -12,12 +12,6 @@ type Message interface {
fmt.Stringer
}
// Titled desccribes any type that returns a Message as a title, given a Player
// (which it may ignore).
type Titled[C StatsCollection] interface {
Title(*Player[C]) Message
}
type stringMessage string
func (s stringMessage) String() string {

View File

@ -13,7 +13,6 @@ var (
ErrInvalidChoice = errors.New("invalid choice specified")
ErrNotUrgent = errors.New("action not urgent when urgent card is available")
ErrNoActions = errors.New("no actions remaining")
ErrNotDebugging = errors.New("this is a debug-only feature and you're not in debug mode")
WarningStalemate = errors.New("no actions can be taken")
)
@ -105,10 +104,6 @@ type Player[C StatsCollection] struct {
// card is in the hand.
PermanentActions []Card[C]
// DebugActions are PermanentActions only available when the player is in
// debug mode.
DebugActions []Card[C]
// InfoPanels lists informational views available to the player. The Prompt
// is the InfoPanel shown before the main action menu.
InfoPanels []InfoPanel[C]
@ -313,38 +308,6 @@ func (p *Player[C]) HasUrgentCards() bool {
return false
}
// EnactableType is an enumeration representing a category of enactable thing.
// Debug actions, permanent actions, and cards behave equivalently in many ways,
// so EnactableType allows parts of the program to work with any of these and
// represent which one they apply to.
type EnactableType int
const (
// InvalidEnactable is an uninitialized EnactableType value with no meaning.
// Using it is generally an error. If you initialize EnactableType fields
// with this value when your program has not yet calculated what type of
// enactable will be used, CardSimEngine will be able to detect bugs where
// such a calcualation, inadvertently, does not come to any conclusion.
// Unlike NothingEnactable, there are no circumstances where this has a
// specific valid meaning.
InvalidEnactable = EnactableType(iota)
// NothingEnactable specifically represents not enacting anything. In some
// contexts, it's an error to use it; in others, it is a sentinel value
// for "do not enact anything". Unlike InvalidEnactable, this has a specific
// valid meaning, it's just that the meaning is specifically "nothing".
NothingEnactable
// CardEnactable refers to a card in the hand.
CardEnactable
// PermanentActionEnactable refers to an item in the permanent actions list.
PermanentActionEnactable
// DebugActionEnactable refers to an item in the debug actions list.
DebugActionEnactable
)
// EnactCardUnchecked executes a card choice, removes it from the hand, and
// decrements the ActionsRemaining. It does not check for conflicting Urgent
// cards or already being out of actions. If no such card or card choice
@ -418,31 +381,10 @@ func (p *Player[C]) EnactCard(cardIdx, choiceIdx int) (Message, error) {
// result of enacting the permanent action. If enacting the card causes a
// serious error, the State becomes GameCrashed.
func (p *Player[C]) EnactPermanentActionUnchecked(actionIdx, choiceIdx int) (Message, error) {
return p.enactActionUnchecked(p.PermanentActions, actionIdx, choiceIdx)
}
// EnactDebugActionUnchecked executes a debug action and decrements the
// ActionsRemaining, even though most debug actions will want to refund that
// action point. (Consistency with other actions is important.) It does not
// check for Urgent cards or for already being out of actions. If no such action
// or card option exists, or the option is not enabled, this returns nil and
// ErrInvalidCard or ErrInvalidChoice without changing anything. If the player
// is not in debug mode (DebugLevel >= 1), this returns ErrNotDebugging.
// Otherwise, this returns the result of enacting the debug action. If enacting
// the action causes a serious error, the State becomes GameCrashed.
func (p *Player[C]) EnactDebugActionUnchecked(actionIdx, choiceIdx int) (Message, error) {
if p.DebugLevel < 1 {
return nil, ErrNotDebugging
if actionIdx < 0 || actionIdx >= len(p.PermanentActions) {
return nil, fmt.Errorf("%w: no action #%d when %d permanent actions exist", ErrInvalidCard, actionIdx, len(p.PermanentActions))
}
return p.enactActionUnchecked(p.DebugActions, actionIdx, choiceIdx)
}
// enactActionUnchecked implements EnactPermanentActionUnchecked and EnactDebugActionUnchecked.
func (p *Player[C]) enactActionUnchecked(actionSource []Card[C], actionIdx, choiceIdx int) (Message, error) {
if actionIdx < 0 || actionIdx >= len(actionSource) {
return nil, fmt.Errorf("%w: no action #%d when %d actions exist", ErrInvalidCard, actionIdx, len(actionSource))
}
card := actionSource[actionIdx]
card := p.PermanentActions[actionIdx]
var errs ErrorCollector
options, err := card.Options(p)
if IsSeriousError(err) {
@ -451,12 +393,12 @@ func (p *Player[C]) enactActionUnchecked(actionSource []Card[C], actionIdx, choi
}
errs.Add(err)
if choiceIdx < 0 || choiceIdx > len(options) {
errs.Add(fmt.Errorf("%w: no option #%d on action #%d with %d options", ErrInvalidChoice, choiceIdx, actionIdx, len(options)))
errs.Add(fmt.Errorf("%w: no option #%d on permanent action #%d with %d options", ErrInvalidChoice, choiceIdx, actionIdx, len(options)))
return nil, errs.Emit()
}
chosen := options[choiceIdx]
if !chosen.Enabled(p) {
errs.Add(fmt.Errorf("%w: option #%d on action #%d is not enabled", ErrInvalidChoice, choiceIdx, actionIdx))
errs.Add(fmt.Errorf("%w: option #%d on permanent action #%d is not enabled", ErrInvalidChoice, choiceIdx, actionIdx))
return nil, errs.Emit()
}

View File

@ -17,7 +17,7 @@ func RunSimpleTerminalUI[C StatsCollection](p *Player[C]) error {
for {
for p.CanAct() {
actionType, cardIdx, choiceIdx, err := pickNextAction(p)
isCard, cardIdx, choiceIdx, err := pickNextAction(p)
p.ReportError(err)
if IsSeriousError(err) {
if p.DebugLevel < 1 {
@ -26,18 +26,10 @@ func RunSimpleTerminalUI[C StatsCollection](p *Player[C]) error {
continue
}
var msg Message
switch actionType {
case CardEnactable:
if isCard {
msg, err = p.EnactCard(cardIdx, choiceIdx)
case DebugActionEnactable:
msg, err = p.EnactDebugActionUnchecked(cardIdx, choiceIdx)
case PermanentActionEnactable:
} else {
msg, err = p.EnactPermanentAction(cardIdx, choiceIdx)
case NothingEnactable:
continue
default:
msg = nil
err = fmt.Errorf("invalid enaction type in action loop: %d", actionType)
}
p.ReportError(err)
if IsSeriousError(err) {
@ -85,7 +77,7 @@ func wait() {
fmt.Scanln()
}
func displayMainMenu[C StatsCollection](p *Player[C]) (debugOffset, actionsOffset, handOffset, max int) {
func displayMainMenu[C StatsCollection](p *Player[C]) (actionsOffset, handOffset, max int) {
cls()
needsDivider := displayMessageSection(p)
if needsDivider {
@ -93,14 +85,10 @@ func displayMainMenu[C StatsCollection](p *Player[C]) (debugOffset, actionsOffse
}
displayOnePanel(p, p.Prompt)
divider()
debugOffset = displayStatsMenu(p)
if debugOffset > 0 {
actionsOffset = displayStatsMenu(p)
if actionsOffset > 0 {
divider()
}
actionsOffset = displayDebugActionsMenu(p, debugOffset)
if actionsOffset > debugOffset {
fmt.Println()
}
handOffset = displayPermanentActionsMenu(p, actionsOffset)
if handOffset > actionsOffset {
fmt.Println()
@ -109,9 +97,9 @@ func displayMainMenu[C StatsCollection](p *Player[C]) (debugOffset, actionsOffse
return // uses named return values
}
func pickNextAction[C StatsCollection](p *Player[C]) (actionType EnactableType, cardIdx int, choiceIdx int, err error) {
func pickNextAction[C StatsCollection](p *Player[C]) (isCard bool, cardIdx int, choiceIdx int, err error) {
for {
debugOffset, actionsOffset, handOffset, max := displayMainMenu(p)
actionsOffset, handOffset, max := displayMainMenu(p)
divider()
fmt.Printf("%d actions remaining.\n", p.ActionsRemaining)
@ -129,8 +117,9 @@ func pickNextAction[C StatsCollection](p *Player[C]) (actionType EnactableType,
case "s", "stat", "stats", "i", "info", "p", "panel", "panels", "infopanel", "infopanels":
statsMode(p)
case "a", "act", "actions":
actionType, cardIdx, choiceIdx, err = actionsMode(p, true)
if actionType != NothingEnactable {
var committed bool
isCard, cardIdx, choiceIdx, committed, err = actionsMode(p, true)
if committed {
return
}
case "q", "quit", "exit":
@ -143,27 +132,21 @@ func pickNextAction[C StatsCollection](p *Player[C]) (actionType EnactableType,
} else if i > max {
fmt.Println("That's not on this menu. If the menu is too big to read, choose a detail view.")
wait()
} else if i <= debugOffset {
} else if i <= actionsOffset {
cls()
displayOnePanel(p, p.InfoPanels[i-1])
wait()
} else if i <= actionsOffset {
i = i - debugOffset - 1
option, promptErr := promptCard(p, p.DebugActions[i])
if option >= 0 || IsSeriousError(promptErr) {
return DebugActionEnactable, i, option, promptErr
}
} else if i <= handOffset {
i = i - actionsOffset - 1
option, promptErr := promptCard(p, p.PermanentActions[i])
if option >= 0 || IsSeriousError(promptErr) {
return PermanentActionEnactable, i, option, promptErr
return false, i, option, promptErr
}
} else {
i = i - handOffset - 1
option, promptErr := promptCard(p, p.Hand[i])
if option >= 0 || IsSeriousError(promptErr) {
return CardEnactable, i, option, nil
return true, i, option, nil
}
}
}
@ -239,7 +222,9 @@ func displayStatsMenu[C StatsCollection](p *Player[C]) int {
}
fmt.Println("Info Panels")
fmt.Println("-----------")
displayNumberedTitles(p, p.InfoPanels, 0)
for i, s := range p.InfoPanels {
fmt.Printf("[%2d]: %s\n", i+1, s.Title(p).String())
}
return len(p.InfoPanels)
}
@ -249,18 +234,10 @@ func displayPermanentActionsMenu[C StatsCollection](p *Player[C], offset int) in
}
fmt.Println("Always Available")
fmt.Println("----------------")
displayNumberedTitles(p, p.PermanentActions, offset)
return offset + len(p.PermanentActions)
}
func displayDebugActionsMenu[C StatsCollection](p *Player[C], offset int) int {
if p.DebugLevel < 1 || len(p.DebugActions) == 0 {
return offset
for i, s := range p.PermanentActions {
fmt.Printf("[%2d]: %s\n", i+offset+1, s.Title(p))
}
fmt.Println("Debug Mode")
fmt.Println("----------")
displayNumberedTitles(p, p.DebugActions, offset)
return offset + len(p.DebugActions)
return offset + len(p.PermanentActions)
}
func displayHandMenu[C StatsCollection](p *Player[C], offset int) int {
@ -269,14 +246,10 @@ func displayHandMenu[C StatsCollection](p *Player[C], offset int) int {
}
fmt.Println("Hand")
fmt.Println("----")
displayNumberedTitles(p, p.Hand, offset)
return offset + len(p.Hand)
}
func displayNumberedTitles[C StatsCollection, T Titled[C]](p *Player[C], cards []T, offset int) {
for i, s := range cards {
for i, s := range p.Hand {
fmt.Printf("[%2d]: %s\n", i+offset+1, s.Title(p))
}
return offset + len(p.Hand)
}
// promptCard asks the player to take an action on a card. Returns the option
@ -412,16 +385,12 @@ func statsMode[C StatsCollection](p *Player[C]) error {
}
}
func actionsMode[C StatsCollection](p *Player[C], canAct bool) (actionType EnactableType, cardIdx, choiceIdx int, err error) {
func actionsMode[C StatsCollection](p *Player[C], canAct bool) (isCard bool, cardIdx, choiceIdx int, committed bool, err error) {
var errs ErrorCollector
for {
cls()
dOff := displayDebugActionsMenu(p, 0)
if dOff > 0 {
fmt.Println()
}
pOff := displayPermanentActionsMenu(p, dOff)
if pOff > dOff {
pOff := displayPermanentActionsMenu(p, 0)
if pOff > 0 {
fmt.Println()
}
max := displayHandMenu(p, pOff)
@ -431,7 +400,7 @@ func actionsMode[C StatsCollection](p *Player[C], canAct bool) (actionType Enact
fmt.Println("That's a problem. The game is stuck.")
confirmQuit()
errs.Add(WarningStalemate)
return NothingEnactable, -1, -1, errs.Emit()
return false, -1, -1, true, errs.Emit()
}
fmt.Println()
@ -443,7 +412,7 @@ func actionsMode[C StatsCollection](p *Player[C], canAct bool) (actionType Enact
input := getResponse()
switch input {
case "b", "back":
return NothingEnactable, -1, -1, errs.Emit()
return false, -1, -1, false, errs.Emit()
case "q", "quit":
confirmQuit()
default:
@ -454,35 +423,19 @@ func actionsMode[C StatsCollection](p *Player[C], canAct bool) (actionType Enact
} else if v < 1 || v > max {
fmt.Println("That's not a card or action.")
wait()
} else if v <= dOff {
v--
if canAct {
optIdx, err := promptCard(p, p.DebugActions[v])
errs.Add(err)
if optIdx >= 0 || IsSeriousError(err) {
return DebugActionEnactable, v, optIdx, errs.Emit()
}
} else {
_, _, err := displayCard(p, p.DebugActions[v], false)
errs.Add(err)
if IsSeriousError(err) {
return DebugActionEnactable, -1, -1, errs.Emit()
}
wait()
}
} else if v <= pOff {
v = v - dOff - 1
v--
if canAct {
optIdx, err := promptCard(p, p.PermanentActions[v])
errs.Add(err)
if optIdx >= 0 || IsSeriousError(err) {
return PermanentActionEnactable, v, optIdx, errs.Emit()
return false, v, optIdx, true, errs.Emit()
}
} else {
_, _, err := displayCard(p, p.PermanentActions[v], false)
errs.Add(err)
if IsSeriousError(err) {
return PermanentActionEnactable, -1, -1, errs.Emit()
return false, -1, -1, true, errs.Emit()
}
wait()
}
@ -492,13 +445,13 @@ func actionsMode[C StatsCollection](p *Player[C], canAct bool) (actionType Enact
optIdx, err := promptCard(p, p.Hand[v])
errs.Add(err)
if optIdx >= 0 || IsSeriousError(err) {
return CardEnactable, v, optIdx, errs.Emit()
return true, v, optIdx, false, errs.Emit()
}
} else {
_, _, err := displayCard(p, p.Hand[v], false)
errs.Add(err)
if IsSeriousError(err) {
return CardEnactable, -1, -1, errs.Emit()
return false, -1, -1, false, errs.Emit()
}
wait()
}
@ -511,7 +464,7 @@ func actionsMode[C StatsCollection](p *Player[C], canAct bool) (actionType Enact
func review[C StatsCollection](p *Player[C]) error {
var errs ErrorCollector
for {
debugOffset, actionsOffset, handOffset, max := displayMainMenu(p)
actionsOffset, handOffset, max := displayMainMenu(p)
divider()
fmt.Println("No actions remaining.")
fmt.Printf("(C)ontinue, review just (M)essages, (I)nfo Panels, (A)ctions, or an item (1-%d), or (Q)uit? > ", max)
@ -532,7 +485,7 @@ func review[C StatsCollection](p *Player[C]) error {
return errs.Emit()
}
case "a", "act", "actions":
_, _, _, err := actionsMode(p, false)
_, _, _, _, err := actionsMode(p, false)
errs.Add(err)
if IsSeriousError(err) {
return errs.Emit()
@ -545,18 +498,14 @@ func review[C StatsCollection](p *Player[C]) error {
i, err := strconv.Atoi(input)
if err != nil {
fmt.Println("Sorry, I don't understand.")
wait()
} else if i <= 0 || i > max {
fmt.Println("That's not on this menu. If the menu is too big to read, choose a detail view.")
} else if i <= debugOffset {
wait()
} else if i <= actionsOffset {
cls()
displayOnePanel(p, p.InfoPanels[i-1])
} else if i <= actionsOffset {
i = i - debugOffset - 1
_, _, err := displayCard(p, p.DebugActions[i], false)
errs.Add(err)
if IsSeriousError(err) {
return errs.Emit()
}
wait()
} else if i <= handOffset {
i = i - actionsOffset - 1
_, _, err := displayCard(p, p.PermanentActions[i], false)
@ -564,6 +513,7 @@ func review[C StatsCollection](p *Player[C]) error {
if IsSeriousError(err) {
return errs.Emit()
}
wait()
} else {
i = i - handOffset - 1
_, _, err := displayCard(p, p.Hand[i], false)
@ -571,8 +521,8 @@ func review[C StatsCollection](p *Player[C]) error {
if IsSeriousError(err) {
return errs.Emit()
}
wait()
}
wait()
}
}
}

View File

@ -119,8 +119,8 @@ func initDeck(d *cardsim.Deck[*SmokeTestCollection]) {
func installPermanentActions(pa *[]card) {
*pa = []card{
&cardsim.BasicCard[*SmokeTestCollection]{
CardTitle: cardsim.MsgStr("Reset Number"),
CardText: cardsim.MsgStr("Resets Number to a fixed value."),
CardTitle: cardsim.MsgStr("Reset to 0"),
CardText: cardsim.MsgStr("Resets Number to 0."),
CardOptions: []cardOption{
&cardsim.BasicOption[*SmokeTestCollection]{
Text: cardsim.MsgStr("Reset to 0."),
@ -130,6 +130,12 @@ func installPermanentActions(pa *[]card) {
},
Output: cardsim.MsgStr("Done."),
},
},
},
&cardsim.BasicCard[*SmokeTestCollection]{
CardTitle: cardsim.MsgStr("Reset to 1000000"),
CardText: cardsim.MsgStr("Resets Number to one million."),
CardOptions: []cardOption{
&cardsim.BasicOption[*SmokeTestCollection]{
Text: cardsim.MsgStr("Reset to 1,000,000"),
Effect: func(p *player) error {
@ -140,11 +146,6 @@ func installPermanentActions(pa *[]card) {
},
},
},
}
}
func installDebugActions(pa *[]card) {
*pa = []card{
&cardsim.BasicCard[*SmokeTestCollection]{
CardTitle: cardsim.MsgStr("Draw a card"),
CardText: cardsim.MsgStr("Draw an extra card."),
@ -157,10 +158,6 @@ func installDebugActions(pa *[]card) {
Output: cardsim.MsgStr("Drawn. Probably."),
},
},
AfterOption: func(c card, p *player, option cardOption) error {
p.ActionsRemaining++
return nil
},
},
}
}

View File

@ -40,7 +40,6 @@ func main() {
installRules(p.Rules)
initDeck(p.Deck)
installPermanentActions(&p.PermanentActions)
installDebugActions(&p.DebugActions)
p.InfoPanels = []cardsim.InfoPanel[*SmokeTestCollection]{
&cardsim.BasicStatsPanel[*SmokeTestCollection]{
Name: cardsim.MsgStr("Stats"),
@ -70,7 +69,6 @@ func (prompt) Info(p *cardsim.Player[*SmokeTestCollection]) ([]cardsim.Message,
return []cardsim.Message{
cardsim.MsgStr("Here, have some stuff."),
cardsim.Msgf("It's turn %d according to the player and turn %d according to me.", p.TurnNumber, p.Stats.Turns.Value),
cardsim.Msgf("The current Number is %d. It tastes like %s.", p.Stats.Number.Value, p.Stats.Flavor.Value),
}, nil
}