commit 583ce1ab52fdf6d7df3662ddf9171c9283ce2363
parent 4e708a2456a68391941e8ad85679fd224771bcec
Author: sin <sin@2f30.org>
Date: Thu, 18 Apr 2013 11:32:05 +0100
games: Add a hangman game
Diffstat:
A | src/games/games.go | | | 218 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 218 insertions(+), 0 deletions(-)
diff --git a/src/games/games.go b/src/games/games.go
@@ -0,0 +1,218 @@
+package games
+
+import (
+ "bytes"
+ "fmt"
+ "irc"
+ "strings"
+)
+
+type game struct {
+ On bool
+ name string
+ creator string
+ goal string
+ goalmaster string
+}
+
+type Gamer interface {
+ IsOn() bool
+ SetOn(bool)
+ ParseGameInput(a string) string
+ GameStatus() string
+ SetGameGoal(a string, who string) string
+ GetGoal() string
+}
+
+type GameEnger interface {
+ List() []string
+ New(string, *irc.IrcContext, *chan irc.IrcMessage)
+}
+
+type gameEng struct {
+ games map[string]func(*irc.IrcContext, *chan irc.IrcMessage)
+}
+
+type hangmanGame struct {
+ game
+ lives uint
+ foundnow string
+ tried string
+}
+
+func cmdTest(a *irc.IrcContext, gc *chan irc.IrcMessage) {
+ a.SendPrivmsg("#2f30-devel", "test workd")
+ a.AddIntercept(gc)
+ name := "unset"
+ for {
+ select {
+ case msg := <-*gc:
+ if msg.Cmd == "SETNICK" {
+ name = msg.Params[0]
+ continue
+ }
+
+ if msg.Params[0] == name {
+ who := msg.Prefix[:strings.Index(msg.Prefix, "!")]
+ a.SendPrivmsg(who, "hello there "+who)
+ } else {
+ a.SendPrivmsg(msg.Params[0], "caught string")
+ }
+ }
+ }
+ /*
+ for {
+ select {
+ case msg := <- a.incomingMsg :
+ a.SendPrivmsg(msg.Params[0],"intercepted by game reemmiting")
+ a.incomingMsg<-msg
+ }
+ }*/
+}
+
+func cmdTest1(a *irc.IrcContext, gc *chan irc.IrcMessage) {
+ a.SendPrivmsg("#2f30-devel", "OLEEEEEEEEE")
+}
+
+func (g *gameEng) List() []string {
+ r := []string{}
+ for k, _ := range g.games {
+ r = append(r, k)
+ }
+ return r
+}
+
+func NewGameEng() *gameEng {
+ return &gameEng{games: map[string]func(*irc.IrcContext, *chan irc.IrcMessage){"test": cmdTest, "test1": cmdTest1}}
+
+}
+
+func (g *gameEng) New(a string, ic *irc.IrcContext, gc *chan irc.IrcMessage) {
+ if f, ok := g.games[a]; ok {
+ go f(ic, gc)
+ }
+}
+
+func (g *hangmanGame) ParseGameInput(a string) string {
+ q := strings.Fields(a)
+ if q[0] == "hangman:" {
+ if g.goal == "" {
+ return "Goal is not yet set"
+ }
+ if len(q) != 2 {
+ return "Syntax is hangman: <letter> or <goal>"
+ }
+ if len(q[1]) == 1 {
+ hm := g.checkLetter([]rune(q[1])[0])
+ if hm == -1 {
+ return "I heard you the first time"
+ }
+ if hm == 0 {
+ if g.lives >= 1 {
+ g.lives--
+ return fmt.Sprintf("You lost a life punk.Remaining:%d [%s tries]", g.lives, g.tried)
+ } else {
+ g.SetOn(false)
+ return fmt.Sprintf("You are hanged mate. It was %s", g.goal)
+ }
+ } else {
+ winstr := ""
+ if g.foundnow == g.goal {
+ winstr = " You win!"
+ g.SetOn(false)
+ }
+ return fmt.Sprintf("Great! You said '%s' and found %d new letters: >> %s << [%d lives]%s",
+ q[1], hm, g.foundnow, g.lives, winstr)
+ }
+ } else {
+ if q[1] == g.goal {
+ g.SetOn(false)
+ return "You win!"
+ } else {
+ if g.lives >= 1 {
+ g.lives--
+ return fmt.Sprintf("You lost a life punk.Remaining:%d", g.lives)
+ } else {
+ g.SetOn(false)
+ return fmt.Sprintf("You are hanged mate. It was %s", g.goal)
+ }
+ }
+ }
+ }
+ return ""
+}
+
+func (g *hangmanGame) GetGoal() string {
+ return g.goal
+}
+
+func (g *hangmanGame) GameStatus() string {
+ return fmt.Sprintf("Game: %s by %s, >> %s << [%d lives] [%s tries]",
+ g.name, g.creator, g.foundnow, g.lives, g.tried)
+}
+
+func (g *hangmanGame) IsOn() bool {
+ return g.On
+}
+
+func (g *hangmanGame) SetOn(a bool) {
+ g.On = a
+}
+
+func (g *hangmanGame) SetGameGoal(a string, who string) string {
+ q := strings.Fields(a)
+ if len(q) != 2 || q[0] != "setgoal" {
+ return "Now we are playing hangman, i need : setgoal stringToFind"
+ }
+ if g.goal != "" {
+ return fmt.Sprintf("%s already has set the goal", g.goalmaster)
+ }
+ g.goalmaster = who
+ g.goal = q[1]
+ runes := make([]rune, len(g.goal))
+ for i := 0; i < len(runes); i++ {
+ runes[i] = '_'
+ }
+ g.foundnow = string(runes)
+ g.SetOn(true)
+ return "goal is now set"
+}
+
+/* checks letter against the goal
+ returns the number of new letters discovered
+ returns -1 if repeated check is detected */
+
+func (g *hangmanGame) checkLetter(c rune) int {
+ now := []rune(g.foundnow)
+ all := []rune(g.goal)
+ var buf bytes.Buffer
+ for _, v := range g.tried {
+ if c == v {
+ return -1
+ }
+ }
+ buf.WriteString(g.tried + " " + string(c))
+ g.tried = buf.String()
+ hm := 0
+ for i, v := range all {
+ if v == c {
+ if now[i] == v {
+ return -1
+ } /* again */
+ now[i] = v
+ hm++
+ }
+ }
+ g.foundnow = string(now)
+ return hm
+}
+
+func NewHangmanGame() *hangmanGame {
+ r := new(hangmanGame)
+ r.On = true
+ r.name = "HangMan"
+ r.creator = "lostd|dsp"
+ r.lives = 5
+ r.tried = ""
+ return r
+}