// Copyright (c) 2016 Florian Fischer. All rights reserved. // Use of this source code is governed by a MIT license found in the LICENSE file. package logic import ( "errors" "fmt" "strconv" "strings" ) type Cmd struct { Cmd string Argv []int Argc int } var cmdTable = map[string]int{ "d": 1, "dice": 1, "p": 2, "print": 2, "q": 3, "quit": 3, "h": 4, "help": 4, "i": 5, "insert": 5, "c": 6, "cancel": 6, "r": 7, "reroll": 7, "pos": 8, "positions": 8, } // we ignore arguments which are to much func ParseCmd(s string) (Cmd, error) { s = strings.Trim(s, " \t\r\n") split := strings.Split(s, " ") if len(split) == 0 { return Cmd{}, errors.New("Empty command") } argv := make([]int, 0, 5) argc := 0 switch split[0] { case "d", "dice": fallthrough case "p", "print": fallthrough case "q", "quit": return Cmd{Cmd: split[0]}, nil case "h", "help": if len(split) > 1 { if i, exists := cmdTable[split[1]]; exists { argv = append(argv, i) argc++ } else { return Cmd{}, fmt.Errorf("Invalid help topic \"%s\"", split[1]) } } if argc < 1 { argv = append(argv, 0) argc++ } case "i", "insert": fallthrough case "c", "cancel": if len(split) < 2 { return Cmd{}, errors.New(split[0] + ": missing argument") } if e, exists := ScorePositions[split[1]]; exists { argv = append(argv, e) } else if i, err := strconv.Atoi(split[1]); err != nil { return Cmd{}, err } else if i < 1 || i > 13 { return Cmd{}, fmt.Errorf("%s: %d out of range", split[0], i) } else { argv = append(argv, i-1) } argc++ case "r", "reroll": for _, v := range split[1:] { i, err := strconv.Atoi(v) if err != nil { return Cmd{}, err } if i < 1 || i > 5 { return Cmd{}, fmt.Errorf("r: %d out of range", i) } argv = append(argv, i) argc++ // ignore additional arguments if argc == 5 { break } } case "pos", "position": split[0] = "h" argv = append(argv, cmdTable["pos"]) argc++ default: return Cmd{}, fmt.Errorf("Invalid command \"%v\"", split[0]) } return Cmd{split[0], argv[0:argc], argc}, nil } func CmdHelp(cmd int) []string { s := make([]string, 0, 6) switch cmd { case 1: s = append(s, fmt.Sprint("d - print dice")) s = append(s, fmt.Sprint("Print out the dice")) case 2: s = append(s, fmt.Sprint("p - print score")) s = append(s, fmt.Sprint("Print out your score")) case 3: s = append(s, fmt.Sprint("q - quit the game")) s = append(s, fmt.Sprint("Terminates the game")) case 4: s = append(s, fmt.Sprint("h [cmd] - print help")) s = append(s, fmt.Sprint("Print the help for a specific command or gernal help.")) case 5: s = append(s, fmt.Sprint("i [pos] - insert into score")) s = append(s, fmt.Sprint("Insert your current dice into your score.")) s = append(s, fmt.Sprint("See \"h pos\" for a explenation of the score entries")) case 6: s = append(s, fmt.Sprint("c [pos] - cancel a entry")) s = append(s, fmt.Sprint("Write 0 into your score.")) s = append(s, fmt.Sprint("See \"h pos\" for a explenation of the score entries")) case 7: s = append(s, fmt.Sprint("r [dice] - reroll some dice")) s = append(s, fmt.Sprint("Reroll the specified dice.")) s = append(s, fmt.Sprint("The dice to reroll are represented by a list of their indices in the dice set.")) s = append(s, fmt.Sprint("Example:")) s = append(s, fmt.Sprint("\t\"r 1 4 5\" rerolls dice number 1, 4 and 5")) s = append(s, fmt.Sprint("\t\"r\" rerolls all dice")) case 8: s = append(s, fmt.Sprint("Positions:")) s = append(s, fmt.Sprint("1: Aces")) s = append(s, fmt.Sprint("2: Twos")) s = append(s, fmt.Sprint("3: Threes")) s = append(s, fmt.Sprint("4: Fours")) s = append(s, fmt.Sprint("5: Fives")) s = append(s, fmt.Sprint("6: Sixes")) s = append(s, fmt.Sprint("7: ThreeOfAKind")) s = append(s, fmt.Sprint("8: FourOfAKind")) s = append(s, fmt.Sprint("9: FullHouse")) s = append(s, fmt.Sprint("10: SmallStraight")) s = append(s, fmt.Sprint("11: LargeStraight")) s = append(s, fmt.Sprint("12: Yahtzee")) s = append(s, fmt.Sprint("13: Chance")) default: s = append(s, fmt.Sprint("h [cmd] - print help")) s = append(s, fmt.Sprint("p - print score")) s = append(s, fmt.Sprint("d - print dice")) s = append(s, fmt.Sprint("q - quit the game")) s = append(s, fmt.Sprint("i [pos] - insert into score")) s = append(s, fmt.Sprint("r [dice] - reroll some dice")) s = append(s, fmt.Sprint("c [pos] - cancel a entry")) } return s }