aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fl.fischer@fau.de>2017-02-06 14:17:55 +0100
committerFlorian Fischer <florian.fl.fischer@fau.de>2017-02-06 23:26:07 +0100
commitfdda6a71a855d0a671b8eadee9c1bf395c9b4f8a (patch)
tree6b5062b765787baa07d6aee9f29b16ddc72de147
parent6c60d2fe8acdbcdfd7ea408350fb8b5461833677 (diff)
downloadgoffel-fdda6a71a855d0a671b8eadee9c1bf395c9b4f8a.tar.gz
goffel-fdda6a71a855d0a671b8eadee9c1bf395c9b4f8a.zip
interactive: use new cmd parsing code
-rw-r--r--uis/interactive.go132
1 files changed, 31 insertions, 101 deletions
diff --git a/uis/interactive.go b/uis/interactive.go
index bc2f44e..b047e92 100644
--- a/uis/interactive.go
+++ b/uis/interactive.go
@@ -1,9 +1,9 @@
package uis
import (
+ "bufio"
"fmt"
- "strconv"
- "strings"
+ "os"
. "muhq.space/go/muhq/goffel/logic"
)
@@ -18,6 +18,7 @@ type Interactive struct {
}
var dices Dices
+var reader = bufio.NewReader(os.Stdin)
func (i *Interactive) Init() error {
name := ""
@@ -73,65 +74,48 @@ func turn(p *intPlayer) {
fmt.Printf("%s's turn:\n", p.name)
fmt.Println(dices)
- var cmd string
- var args [5]string
+ var cmd Cmd
rerolls := 0
-outer:
for {
- cmd = " "
- args[0], args[1], args[2], args[3], args[4] = "", "", "", "", ""
fmt.Print("Please enter a command(h for help): ")
- fmt.Scanln(&cmd, &args[0], &args[1], &args[2], &args[3], &args[4])
- switch cmd[0] {
- case 'h':
- cmdHelp(args[0])
- case 'i':
- pos, err := strconv.Atoi(args[0])
- if err != nil {
- fmt.Println("[pos] musst be a number.")
- continue
- }
- points, err := p.score.Insert(dices, pos-1)
- if err != nil {
- fmt.Println("Insert failed:", err)
- continue
- }
- fmt.Printf("You inserted %d into %d.\n", points, pos)
- break outer
- case 'p':
+ input, err := reader.ReadString('\n')
+ if err != nil {
+ panic(err)
+ }
+ cmd, err = ParseCmd(input[0 : len(input)-1])
+ if err != nil {
+ fmt.Printf("%v. Try \"h\".\n", err)
+ continue
+ }
+ switch cmd.Cmd {
+ case "h":
+ print(CmdHelp(cmd.Argv[0]))
+ case "p":
fmt.Println(p.score)
- case 'd':
+ case "d":
fmt.Println(dices)
- case 'c':
- pos, err := strconv.Atoi(args[0])
+ case "q":
+ os.Exit(0)
+ case "i":
+ points, err := p.score.Insert(dices, cmd.Argv[0])
if err != nil {
- fmt.Println("[pos] must be a number")
+ fmt.Println("Insert failed:", err)
continue
}
- err = p.score.Cancel(pos)
+ fmt.Printf("You inserted %d into %v.\n", points, ScoreNames[cmd.Argv[0]])
+ return
+ case "c":
+ err = p.score.Cancel(cmd.Argv[0])
if err != nil {
fmt.Println(err)
continue
}
- fmt.Println("You cancelled", pos)
- break outer
- case 'r':
+ fmt.Println("You cancelled", ScoreNames[cmd.Argv[0]])
+ return
+ case "r":
if rerolls < 2 {
- var idx []int
- for _, c := range args {
- if c == "" {
- break
- }
- if n, err := strconv.Atoi(c); err == nil {
- idx = append(idx, n)
- } else {
- fmt.Println(err)
- fmt.Println("cant parse", c)
- break
- }
- }
- err := dices.Roll(idx)
+ err := dices.Roll(cmd.Argv)
if err != nil {
fmt.Println("Reroll failed:", err)
continue
@@ -142,60 +126,6 @@ outer:
}
fmt.Println("You are not allowed to reroll more than twice.")
default:
- fmt.Println("Not a valid command. Try \"h\".")
}
}
}
-
-func cmdHelp(cmd string) {
- cmd = strings.TrimLeft(cmd, " ")
- switch cmd {
- case "d":
- fmt.Println("d - print dices")
- fmt.Println("Print out the dices")
- case "p":
- fmt.Println("p - print score")
- fmt.Println("Print out your score<M-Escape>")
- case "i":
- fmt.Println("i [pos] - insert into score")
- fmt.Println("Insert your current dices into your score.")
- fmt.Println("See \"h pos\" for a explenation of the score entries")
- case "r":
- fmt.Println("r [dices] - reroll some dices")
- fmt.Println("Reroll the specified dices.")
- fmt.Println("The dices to reroll are represented by a list of their indices in the dice set.")
- fmt.Println("Example:")
- fmt.Println("\t\"r 1 4 5\" rerolls dice number 1, 4 and 5")
- fmt.Println("\t\"r\" rerolls all dices")
- case "h":
- fmt.Println("h [cmd] - print help")
- fmt.Println("Print the help for a specific command or gernal help.")
- case "c":
- fmt.Println("c [pos] - cancel a entry")
- fmt.Println("Write 0 into your score.")
- fmt.Println("See \"h pos\" for a explenation of the score entries")
-
- case "pos":
- fmt.Println("Positions:")
- fmt.Println("\t1: Aces")
- fmt.Println("\t2: Twos")
- fmt.Println("\t3: Threes")
- fmt.Println("\t4: Fours")
- fmt.Println("\t5: Fives")
- fmt.Println("\t6: Sixes")
- fmt.Println("\t7: ThreeOfAKind")
- fmt.Println("\t8: FourOfAKind")
- fmt.Println("\t9: FullHouse")
- fmt.Println("\t10: SmallStraight")
- fmt.Println("\t11: LargeStraight")
- fmt.Println("\t12: Yathzze")
- fmt.Println("\t13: Chance")
- default:
- fmt.Println("h [cmd] - print help")
- fmt.Println("p - print score")
- fmt.Println("d - print dices")
- fmt.Println("i [pos] - insert into score")
- fmt.Println("r [dices] - reroll some dices")
- fmt.Println("c [pos] - cancel a entry")
- }
-}