From cfe34944094ffff5b61878d55aa14798d7701297 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Sat, 28 Jan 2017 14:52:38 +0100 Subject: seperate UIs in new uis package add Server and Client ui sceleton --- goffel.go | 18 ++++- interactive.go | 201 ----------------------------------------------------- uis/client.go | 16 +++++ uis/interactive.go | 201 +++++++++++++++++++++++++++++++++++++++++++++++++++++ uis/server.go | 16 +++++ 5 files changed, 250 insertions(+), 202 deletions(-) delete mode 100644 interactive.go create mode 100644 uis/client.go create mode 100644 uis/interactive.go create mode 100644 uis/server.go diff --git a/goffel.go b/goffel.go index 3bc3311..5e5c6b9 100644 --- a/goffel.go +++ b/goffel.go @@ -1,8 +1,11 @@ package main import ( + "flag" "math/rand" "time" + + "muhq.space/go/muhq/goffel/uis" ) type UI interface { @@ -29,7 +32,20 @@ func play(ui UI) { func main() { rand.Seed(time.Now().Unix()) + var port = flag.Int("p", 1337, "port to listen for connections") + var server = flag.Bool("s", false, "start a goffel server") + var client = flag.Bool("c", false, "start a goffel client") + // var fancy = flag.Bool("f", false, "print utf8 dice runes") + flag.Parse() + var ui UI - play(&Interactive{}) + if *server { + ui = &uis.Server{Port: *port} + } else if *client { + ui = &uis.Client{Port: *port} + } else { + ui = &uis.Interactive{} + } + play(ui) } diff --git a/interactive.go b/interactive.go deleted file mode 100644 index bb257d8..0000000 --- a/interactive.go +++ /dev/null @@ -1,201 +0,0 @@ -package main - -import ( - "fmt" - "strconv" - "strings" - - . "muhq.space/go/muhq/goffel/logic" -) - -type intPlayer struct { - name string - score Score -} - -type Interactive struct { - players []intPlayer -} - -var dices Dices - -func (i *Interactive) Init() error { - name := "" - count := 0 - fmt.Println("Please enter your player names. An empty line ends the player registration.") - for { - fmt.Printf("%d: ", count+1) - _, err := fmt.Scanln(&name) - if err != nil { - break - } - i.players = append(i.players, intPlayer{name, NewScore()}) - count++ - - } - return nil -} -func (ui *Interactive) Round(r int) error { - fmt.Printf("Round %d\n", r) - for i := range ui.players { - turn(&ui.players[i]) - } - return nil -} - -func (ui *Interactive) BroadcastWinner() { - w := []intPlayer{} - s := 0 - - for _, p := range ui.players { - t := p.score.Score() - if t > s || len(w) == 0 { - s = t - w = []intPlayer{p} - } else if t == s { - w = append(w, p) - } - } - - if len(w) == 1 { - fmt.Println("The Winner is:") - } else { - fmt.Println("The Winners are:") - } - for _, p := range w { - fmt.Println(p.name, "with", p.score.Score(), "points") - } -} - -func turn(p *intPlayer) { - dices.Roll(nil) - - fmt.Printf("%s's turn:\n", p.name) - fmt.Println(dices) - - var cmd string - var args [5]string - 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': - fmt.Println(p.score) - case 'd': - fmt.Println(dices) - case 'c': - pos, err := strconv.Atoi(args[0]) - if err != nil { - fmt.Println("[pos] must be a number") - continue - } - err = p.score.Cancel(pos) - if err != nil { - fmt.Println(err) - continue - } - fmt.Println("You cancelled", pos) - break outer - 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) - if err != nil { - fmt.Println("Reroll failed:", err) - continue - } - rerolls++ - fmt.Println(dices) - continue - } - 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") - 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") - } -} diff --git a/uis/client.go b/uis/client.go new file mode 100644 index 0000000..f0f05f6 --- /dev/null +++ b/uis/client.go @@ -0,0 +1,16 @@ +package uis + +type Client struct { + Port int +} + +func (ui *Client) Init() error { + return nil +} + +func (ui *Client) Round(r int) error { + return nil +} + +func (ui *Client) BroadcastWinner() {} + diff --git a/uis/interactive.go b/uis/interactive.go new file mode 100644 index 0000000..bc2f44e --- /dev/null +++ b/uis/interactive.go @@ -0,0 +1,201 @@ +package uis + +import ( + "fmt" + "strconv" + "strings" + + . "muhq.space/go/muhq/goffel/logic" +) + +type intPlayer struct { + name string + score Score +} + +type Interactive struct { + players []intPlayer +} + +var dices Dices + +func (i *Interactive) Init() error { + name := "" + count := 0 + fmt.Println("Please enter your player names. An empty line ends the player registration.") + for { + fmt.Printf("%d: ", count+1) + _, err := fmt.Scanln(&name) + if err != nil { + break + } + i.players = append(i.players, intPlayer{name, NewScore()}) + count++ + + } + return nil +} +func (ui *Interactive) Round(r int) error { + fmt.Printf("Round %d\n", r) + for i := range ui.players { + turn(&ui.players[i]) + } + return nil +} + +func (ui *Interactive) BroadcastWinner() { + w := []intPlayer{} + s := 0 + + for _, p := range ui.players { + t := p.score.Score() + if t > s || len(w) == 0 { + s = t + w = []intPlayer{p} + } else if t == s { + w = append(w, p) + } + } + + if len(w) == 1 { + fmt.Println("The Winner is:") + } else { + fmt.Println("The Winners are:") + } + for _, p := range w { + fmt.Println(p.name, "with", p.score.Score(), "points") + } +} + +func turn(p *intPlayer) { + dices.Roll(nil) + + fmt.Printf("%s's turn:\n", p.name) + fmt.Println(dices) + + var cmd string + var args [5]string + 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': + fmt.Println(p.score) + case 'd': + fmt.Println(dices) + case 'c': + pos, err := strconv.Atoi(args[0]) + if err != nil { + fmt.Println("[pos] must be a number") + continue + } + err = p.score.Cancel(pos) + if err != nil { + fmt.Println(err) + continue + } + fmt.Println("You cancelled", pos) + break outer + 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) + if err != nil { + fmt.Println("Reroll failed:", err) + continue + } + rerolls++ + fmt.Println(dices) + continue + } + 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") + 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") + } +} diff --git a/uis/server.go b/uis/server.go new file mode 100644 index 0000000..e83e6a9 --- /dev/null +++ b/uis/server.go @@ -0,0 +1,16 @@ +package uis + +type Server struct { + Port int +} + +func (s *Server) Init() error { + return nil +} + +func (s *Server) Round(r int) error { + return nil +} + +func (s *Server) BroadcastWinner() { +} -- cgit v1.2.3