card_id/cmd/cli/main.go

81 lines
1.6 KiB
Go
Raw Normal View History

2022-04-13 07:48:22 +00:00
package main
import (
2022-04-13 23:09:58 +00:00
"fmt"
2022-04-13 07:48:22 +00:00
"log"
"os"
2022-04-13 23:09:58 +00:00
"strconv"
"strings"
2022-04-13 07:48:22 +00:00
"git.tuxpa.in/a/card_id/common/game"
2022-04-13 23:09:58 +00:00
"git.tuxpa.in/a/card_id/common/game/card"
2022-04-14 00:20:00 +00:00
"git.tuxpa.in/a/card_id/common/game/score"
2022-04-13 07:48:22 +00:00
)
func main() {
2022-04-13 23:09:58 +00:00
deal, err := card.NewDealer().ReadFromRoot(os.Getenv("CARD_DATA_DIR"))
2022-04-13 07:48:22 +00:00
if err != nil {
log.Panicln(err)
}
2022-04-13 23:09:58 +00:00
g := game.Game{Dealer: deal, Lives: 3}
2022-04-13 07:48:22 +00:00
2022-04-13 23:09:58 +00:00
trial := g.CreateTrial(3, 1)
2022-04-14 00:20:00 +00:00
g.CurrentTrial = 1
fmt.Println("type c[n] to select a card, e.g. c1")
fmt.Println("exit to exit")
2022-04-13 23:09:58 +00:00
for {
if trial != nil {
2022-04-14 00:20:00 +00:00
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
2022-04-13 23:09:58 +00:00
}
2022-04-14 00:20:00 +00:00
g.Lives = g.Lives + lives
msg := fmt.Sprintf(`
lives: %0.4v | score: %d | combo: %d
`, g.Lives, g.Score, g.Combo)
fmt.Printf(msg)
2022-04-13 23:09:58 +00:00
}
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])
2022-04-14 00:20:00 +00:00
_, err := trial.SelectCard(num)
2022-04-13 23:09:58 +00:00
if err != nil {
log.Println(err)
}
}
2022-04-14 00:20:00 +00:00
fmt.Printf("hand: %s\n", trial.ShowString())
2022-04-13 23:09:58 +00:00
}
}
}
func strip(s []byte) []byte {
n := 0
for _, b := range s {
if ('0' <= b && b <= '9') ||
b == ' ' {
s[n] = b
n++
}
}
return s[:n]
2022-04-13 07:48:22 +00:00
}