Compare commits

...

2 Commits

Author SHA1 Message Date
6139db889d
Refactor RPS/RPSLS 2022-09-15 09:32:37 -05:00
12372522e9
Add rock, paper, scissors, lizard, spock 2022-09-15 09:32:01 -05:00
5 changed files with 126 additions and 47 deletions

View File

@ -60,6 +60,11 @@ func (b *Bot) RegisterCommands() {
Func: b.RpsCommand(),
NArgs: 1,
})
AddCommand(&Command{
Name: "rpsls",
Func: b.RpslsCommand(),
NArgs: 1,
})
AddCommand(&Command{
Name: "time",
Func: b.TimeCommand(),

View File

@ -5,41 +5,14 @@ import (
"strings"
"git.kill0.net/chill9/beepboop/lib"
"git.kill0.net/chill9/beepboop/lib/rps"
"github.com/bwmarrin/discordgo"
)
const (
Rock = iota
Paper
Scissors
)
var (
rpsVictoryMap map[int][]int = map[int][]int{
Rock: {Scissors},
Paper: {Rock},
Scissors: {Paper},
}
rpsChoiceMap map[string]int = map[string]int{
"rock": Rock,
"paper": Paper,
"scissors": Scissors,
}
rpsEmojiMap map[int]string = map[int]string{
Rock: "🪨️",
Paper: "📝",
Scissors: "✂️",
}
)
func (b *Bot) RpsCommand() CommandFunc {
return func(args []string, m *discordgo.MessageCreate) error {
var (
bc, pc int
be, pe string
c string
bc, pc, be, pe string
)
if len(args) != 1 {
@ -49,33 +22,22 @@ func (b *Bot) RpsCommand() CommandFunc {
return nil
}
c = strings.ToLower(args[0])
pc = strings.ToLower(args[0])
pc, ok := rpsChoiceMap[c] // player's choice
_, ok := rps.EmojiMapRps[pc] // player's choice
if !ok {
b.Session.ChannelMessageSend(
m.ChannelID, "help: `!rps (rock | paper | scissors)`",
)
}
bc = lib.MapRand(rpsChoiceMap) // bot's choice
pe = rpsEmojiMap[pc] // player's emoji
be = rpsEmojiMap[bc] // bot's emoji
if bc == pc {
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf(
"%s v %s: draw", be, pe,
))
return nil
} else if lib.Contains(rpsVictoryMap[bc], pc) {
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf(
"%s v %s: %s wins", be, pe, lib.MapKey(rpsChoiceMap, bc),
))
return nil
}
bc = lib.MapRandKey(rps.EmojiMapRps) // bot's choice
pe = rps.EmojiMapRps[pc] // player's emoji
be = rps.EmojiMapRps[bc] // bot's emoji
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf(
"%s v %s: %s wins", be, pe, lib.MapKey(rpsChoiceMap, pc),
"%s v %s: %s", pe, be, rps.Play(rps.RulesRps, bc, pc),
))
return nil
}

44
bot/rpsls.go Normal file
View File

@ -0,0 +1,44 @@
package bot
import (
"fmt"
"strings"
"git.kill0.net/chill9/beepboop/lib"
"git.kill0.net/chill9/beepboop/lib/rps"
"github.com/bwmarrin/discordgo"
)
func (b *Bot) RpslsCommand() CommandFunc {
return func(args []string, m *discordgo.MessageCreate) error {
var (
bc, pc, be, pe string
)
if len(args) != 1 {
b.Session.ChannelMessageSend(
m.ChannelID, "help: `!rpsls (rock | paper | scissors | lizard | spock)`",
)
return nil
}
pc = strings.ToLower(args[0])
_, ok := rps.EmojiMapRpsls[pc] // player's choice
if !ok {
b.Session.ChannelMessageSend(
m.ChannelID, "help: `!rpsls (rock | paper | scissors | lizard | spock)`",
)
return nil
}
bc = lib.MapRandKey(rps.EmojiMapRpsls) // bot's choice
pe = rps.EmojiMapRpsls[pc] // player's emoji
be = rps.EmojiMapRpsls[bc] // bot's emoji
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf(
"%s v %s: %s", pe, be, rps.Play(rps.RulesRpsls, bc, pc),
))
return nil
}
}

View File

@ -49,3 +49,9 @@ func MapRand[K comparable, V any](m map[K]V) V {
}
panic("unreachable")
}
func MapRandKey[K comparable, V any](m map[K]V) K {
keys := MapKeys(m)
n := rand.Intn(len(m))
return keys[n]
}

62
lib/rps/rps.go Normal file
View File

@ -0,0 +1,62 @@
package rps
import (
"fmt"
)
type (
Game struct {
rules [][]string
emojiMap map[string]string
}
)
var (
RulesRps [][]string = [][]string{
{"rock", "scissors", "crushes"},
{"paper", "rock", "covers"},
{"scissors", "paper", "cuts"},
}
EmojiMapRps map[string]string = map[string]string{
"rock": "🪨️",
"paper": "📝",
"scissors": "✂️",
}
RulesRpsls [][]string = [][]string{
{"rock", "scissors", "crushes"},
{"rock", "lizard", "crushes"},
{"paper", "rock", "covers"},
{"paper", "spock", "disproves"},
{"scissors", "paper", "cuts"},
{"scissors", "lizard", "decapitates"},
{"lizard", "paper", "eats"},
{"lizard", "spock", "poisons"},
{"spock", "scissors", "smashes"},
{"spock", "rock", "vaporizes"},
}
EmojiMapRpsls map[string]string = map[string]string{
"rock": "🪨️",
"paper": "📝",
"scissors": "✂️",
"lizard": "🦎",
"spock": "🖖",
}
)
func Play(rules [][]string, c1, c2 string) string {
for _, rule := range rules {
if c1 == c2 {
return "draw"
}
if c1 == rule[0] && c2 == rule[1] {
return fmt.Sprintf("%s %s %s", c1, rule[2], c2)
} else if c2 == rule[0] && c1 == rule[1] {
return fmt.Sprintf("%s %s %s", c2, rule[2], c1)
}
}
return ""
}