Skip to content

Commit

Permalink
Implemented game state initialisation, refs Miniand/brdg.me-issues#52.
Browse files Browse the repository at this point in the history
  • Loading branch information
beefsack committed Jun 15, 2015
1 parent 4e2e6d5 commit 5e617f3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
44 changes: 41 additions & 3 deletions game/bang_dice/game.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package bang_dice

import (
"errors"
"fmt"
"math/rand"
"time"

"github.com/Miniand/brdg.me/command"
"github.com/Miniand/brdg.me/game/helper"
"github.com/Miniand/brdg.me/game/log"
Expand All @@ -13,6 +18,8 @@ const (
RoleRenegade
)

var rnd = rand.New(rand.NewSource(time.Now().UnixNano()))

var Roles = []int{
RoleSheriff,
RoleRenegade,
Expand All @@ -25,8 +32,10 @@ var Roles = []int{
}

type Game struct {
Players []string
Log *log.Log
Players []string
Roles, Chars, Life []int
Log *log.Log
CurrentTurn int
}

func (g *Game) Commands() []command.Command {
Expand All @@ -50,8 +59,37 @@ func (g *Game) Decode(data []byte) error {
}

func (g *Game) Start(players []string) error {
l := len(players)
if l < 4 || l > 8 {
return errors.New("only for 4 to 8 players")
}
g.Players = players
g.Log = log.New()

g.Chars = make([]int, l)
for i, c := range rnd.Perm(len(Chars))[:l] {
g.Chars[i] = c
g.Log.Add(log.NewPublicMessage(fmt.Sprintf(
"%s is {{b}}%s{{_b}} with {{b}}%d{{_b}} life. %s",
g.PlayerName(i),
Chars[c].Name(),
Chars[c].StartingLife(),
Chars[c].Description(),
)))
}

g.Roles = make([]int, l)
for i, p := range rnd.Perm(l) {
g.Roles[i] = Roles[p]
if Roles[p] == RoleSheriff {
g.Log.Add(log.NewPublicMessage(fmt.Sprintf(
"{{b}}%s is the Sherrif{{_b}}, they start with an extra life and take the first turn.",
g.PlayerName(i),
)))
g.CurrentTurn = i
}
}

return nil
}

Expand All @@ -68,7 +106,7 @@ func (g *Game) Winners() []string {
}

func (g *Game) WhoseTurn() []string {
return []string{}
return []string{g.Players[g.CurrentTurn]}
}

func (g *Game) GameLog() *log.Log {
Expand Down
11 changes: 4 additions & 7 deletions game/bang_dice/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ import "github.com/Miniand/brdg.me/render"

func (g *Game) RenderForPlayer(string) (string, error) {
cells := [][]interface{}{}
for _, c := range Chars {
cells = append(cells, []interface{}{
render.Bold(c.StartingLife()),
render.Bold(c.Name()),
render.Colour(c.Description(), "gray"),
})
}
return render.Table(cells, 0, 2), nil
}

func (g *Game) PlayerName(player int) string {
return render.PlayerName(player, g.Players[player])
}

0 comments on commit 5e617f3

Please sign in to comment.