Compare commits

..

1 Commits

Author SHA1 Message Date
333bc3b26c
Add rock, paper, scissors, lizard, spock 2022-09-14 08:35:21 -05:00
5 changed files with 97 additions and 92 deletions

View File

@ -60,11 +60,6 @@ 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,14 +5,43 @@ import (
"strings"
"git.kill0.net/chill9/beepboop/lib"
"git.kill0.net/chill9/beepboop/lib/rps"
"github.com/bwmarrin/discordgo"
)
const (
Rock = iota
Paper
Scissors
Lizard
Spock
)
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, be, pe string
bc, pc int
be, pe string
c string
)
if len(args) != 1 {
@ -22,22 +51,33 @@ func (b *Bot) RpsCommand() CommandFunc {
return nil
}
pc = strings.ToLower(args[0])
c = strings.ToLower(args[0])
_, ok := rps.EmojiMapRps[pc] // player's choice
pc, ok := rpsChoiceMap[c] // 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", pe, be, rps.Play(rps.RulesRps, bc, pc),
"%s v %s: %s wins", be, pe, lib.MapKey(rpsChoiceMap, pc),
))
return nil
}

View File

@ -5,39 +5,77 @@ import (
"strings"
"git.kill0.net/chill9/beepboop/lib"
"git.kill0.net/chill9/beepboop/lib/rps"
"github.com/bwmarrin/discordgo"
)
var (
rpslsVictoryMap map[int][]int = map[int][]int{
Rock: {Scissors, Lizard},
Paper: {Rock, Spock},
Scissors: {Paper, Lizard},
Lizard: {Paper, Spock},
Spock: {Scissors, Rock},
}
rpslsChoiceMap map[string]int = map[string]int{
"rock": Rock,
"paper": Paper,
"scissors": Scissors,
"lizard": Lizard,
"spock": Spock,
}
rpslsEmojiMap map[int]string = map[int]string{
Rock: "🪨️",
Paper: "📝",
Scissors: "✂️",
Lizard: "🦎",
Spock: "🖖",
}
)
func (b *Bot) RpslsCommand() CommandFunc {
return func(args []string, m *discordgo.MessageCreate) error {
var (
bc, pc, be, pe string
bc, pc int
be, pe string
c string
)
if len(args) != 1 {
b.Session.ChannelMessageSend(
m.ChannelID, "help: `!rpsls (rock | paper | scissors | lizard | spock)`",
m.ChannelID, "help: `!rpsls (rock | paper | scissors | lizard | spock )`",
)
return nil
}
pc = strings.ToLower(args[0])
c = strings.ToLower(args[0])
_, ok := rps.EmojiMapRpsls[pc] // player's choice
pc, ok := rpslsChoiceMap[c] // player's choice
if !ok {
b.Session.ChannelMessageSend(
m.ChannelID, "help: `!rpsls (rock | paper | scissors | lizard | spock)`",
)
}
bc = lib.MapRand(rpslsChoiceMap) // bot's choice
pe = rpslsEmojiMap[pc] // player's emoji
be = rpslsEmojiMap[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(rpslsVictoryMap[bc], pc) {
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf(
"%s v %s: %s wins", be, pe, lib.MapKey(rpslsChoiceMap, bc),
))
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),
"%s v %s: %s wins", be, pe, lib.MapKey(rpslsChoiceMap, pc),
))
return nil
}

View File

@ -49,9 +49,3 @@ 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]
}

View File

@ -1,62 +0,0 @@
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 ""
}