card_id/cmd/cli/main.go

81 lines
1.6 KiB
Go

package main
import (
"fmt"
"log"
"os"
"strconv"
"strings"
"git.tuxpa.in/a/card_id/common/game"
"git.tuxpa.in/a/card_id/common/game/card"
"git.tuxpa.in/a/card_id/common/game/score"
)
func main() {
deal, err := card.NewDealer().ReadFromRoot(os.Getenv("CARD_DATA_DIR"))
if err != nil {
log.Panicln(err)
}
g := game.Game{Dealer: deal, Lives: 3}
trial := g.CreateTrial(3, 1)
g.CurrentTrial = 1
fmt.Println("type c[n] to select a card, e.g. c1")
fmt.Println("exit to exit")
for {
if trial != nil {
sc, lives := trial.CheckSelection()
if sc != 0 {
fmt.Println(" match found!!!")
g.Combo = g.Combo + 1
g.Score = g.Score + sc*score.DefaultTable.Ratio(g.Combo)*g.CurrentTrial
} else {
g.Combo = 0
}
g.Lives = g.Lives + lives
msg := fmt.Sprintf(`
lives: %0.4v | score: %d | combo: %d
`, g.Lives, g.Score, g.Combo)
fmt.Printf(msg)
}
fmt.Printf("\ncardid > ")
var args [4]string
alen, _ := fmt.Scanln(&args[0], &args[1], &args[2], &args[3])
if alen < 0 {
continue
}
if strings.HasPrefix(args[0], "c") {
args[1] = string(strip([]byte(args[0])))
args[0] = "c"
alen = 2
}
switch args[0] {
case "help":
fmt.Printf("show, select, update")
case "show":
fmt.Printf("current hand:\n %+v", trial.Show())
case "select", "sel", "c":
if alen > 1 {
num, _ := strconv.Atoi(args[1])
_, err := trial.SelectCard(num)
if err != nil {
log.Println(err)
}
}
fmt.Printf("hand: %s\n", trial.ShowString())
}
}
}
func strip(s []byte) []byte {
n := 0
for _, b := range s {
if ('0' <= b && b <= '9') ||
b == ' ' {
s[n] = b
n++
}
}
return s[:n]
}