Compare commits
2 Commits
333bc3b26c
...
6139db889d
Author | SHA1 | Date | |
---|---|---|---|
6139db889d | |||
12372522e9 |
@ -60,6 +60,11 @@ func (b *Bot) RegisterCommands() {
|
|||||||
Func: b.RpsCommand(),
|
Func: b.RpsCommand(),
|
||||||
NArgs: 1,
|
NArgs: 1,
|
||||||
})
|
})
|
||||||
|
AddCommand(&Command{
|
||||||
|
Name: "rpsls",
|
||||||
|
Func: b.RpslsCommand(),
|
||||||
|
NArgs: 1,
|
||||||
|
})
|
||||||
AddCommand(&Command{
|
AddCommand(&Command{
|
||||||
Name: "time",
|
Name: "time",
|
||||||
Func: b.TimeCommand(),
|
Func: b.TimeCommand(),
|
||||||
|
58
bot/rps.go
58
bot/rps.go
@ -5,43 +5,14 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git.kill0.net/chill9/beepboop/lib"
|
"git.kill0.net/chill9/beepboop/lib"
|
||||||
|
"git.kill0.net/chill9/beepboop/lib/rps"
|
||||||
"github.com/bwmarrin/discordgo"
|
"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 {
|
func (b *Bot) RpsCommand() CommandFunc {
|
||||||
return func(args []string, m *discordgo.MessageCreate) error {
|
return func(args []string, m *discordgo.MessageCreate) error {
|
||||||
var (
|
var (
|
||||||
bc, pc int
|
bc, pc, be, pe string
|
||||||
be, pe string
|
|
||||||
c string
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if len(args) != 1 {
|
if len(args) != 1 {
|
||||||
@ -51,33 +22,22 @@ func (b *Bot) RpsCommand() CommandFunc {
|
|||||||
return nil
|
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 {
|
if !ok {
|
||||||
b.Session.ChannelMessageSend(
|
b.Session.ChannelMessageSend(
|
||||||
m.ChannelID, "help: `!rps (rock | paper | scissors)`",
|
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
|
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(
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
56
bot/rpsls.go
56
bot/rpsls.go
@ -5,41 +5,14 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git.kill0.net/chill9/beepboop/lib"
|
"git.kill0.net/chill9/beepboop/lib"
|
||||||
|
"git.kill0.net/chill9/beepboop/lib/rps"
|
||||||
"github.com/bwmarrin/discordgo"
|
"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 {
|
func (b *Bot) RpslsCommand() CommandFunc {
|
||||||
return func(args []string, m *discordgo.MessageCreate) error {
|
return func(args []string, m *discordgo.MessageCreate) error {
|
||||||
var (
|
var (
|
||||||
bc, pc int
|
bc, pc, be, pe string
|
||||||
be, pe string
|
|
||||||
c string
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if len(args) != 1 {
|
if len(args) != 1 {
|
||||||
@ -49,33 +22,22 @@ func (b *Bot) RpslsCommand() CommandFunc {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
c = strings.ToLower(args[0])
|
pc = strings.ToLower(args[0])
|
||||||
|
|
||||||
pc, ok := rpslsChoiceMap[c] // player's choice
|
_, ok := rps.EmojiMapRpsls[pc] // player's choice
|
||||||
if !ok {
|
if !ok {
|
||||||
b.Session.ChannelMessageSend(
|
b.Session.ChannelMessageSend(
|
||||||
m.ChannelID, "help: `!rpsls (rock | paper | scissors | lizard | spock)`",
|
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
|
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(
|
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf(
|
||||||
"%s v %s: %s wins", be, pe, lib.MapKey(rpslsChoiceMap, pc),
|
"%s v %s: %s", pe, be, rps.Play(rps.RulesRpsls, bc, pc),
|
||||||
))
|
))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -49,3 +49,9 @@ func MapRand[K comparable, V any](m map[K]V) V {
|
|||||||
}
|
}
|
||||||
panic("unreachable")
|
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
62
lib/rps/rps.go
Normal 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 ""
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user