Compare commits

...

2 Commits

5 changed files with 177 additions and 31 deletions

View File

@ -2,9 +2,9 @@ package bot
import ( import (
"fmt" "fmt"
"math/rand"
"strings" "strings"
"git.kill0.net/chill9/beepboop/lib"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
@ -12,13 +12,15 @@ const (
Rock = iota Rock = iota
Paper Paper
Scissors Scissors
Lizard
Spock
) )
var ( var (
rpsVictoryMap map[int]int = map[int]int{ rpsVictoryMap map[int][]int = map[int][]int{
Rock: Scissors, Rock: {Scissors},
Paper: Rock, Paper: {Rock},
Scissors: Paper, Scissors: {Paper},
} }
rpsChoiceMap map[string]int = map[string]int{ rpsChoiceMap map[string]int = map[string]int{
@ -34,51 +36,49 @@ var (
} }
) )
func RpsRand() int {
n := rand.Intn(len(rpsVictoryMap))
i := 0
for _, v := range rpsChoiceMap {
if i == n {
return v
}
i++
}
panic("unreachable")
}
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 (
botChoice, playerChoice int bc, pc int
botEmoji, playerEmoji string be, pe string
c string c string
) )
if len(args) != 1 { if len(args) != 1 {
b.Session.ChannelMessageSend(m.ChannelID, "help: `!rps (rock | paper | scissors)`") b.Session.ChannelMessageSend(
m.ChannelID, "help: `!rps (rock | paper | scissors)`",
)
return nil return nil
} }
c = strings.ToLower(args[0]) c = strings.ToLower(args[0])
playerChoice, ok := rpsChoiceMap[c] pc, ok := rpsChoiceMap[c] // player's choice
if !ok { if !ok {
b.Session.ChannelMessageSend(m.ChannelID, "help: `!rps (rock | paper | scissors)`") b.Session.ChannelMessageSend(
m.ChannelID, "help: `!rps (rock | paper | scissors)`",
)
} }
botChoice = RpsRand() bc = lib.MapRand(rpsChoiceMap) // bot's choice
botEmoji = rpsEmojiMap[botChoice] pe = rpsEmojiMap[pc] // player's emoji
playerEmoji = rpsEmojiMap[playerChoice] be = rpsEmojiMap[bc] // bot's emoji
if botChoice == playerChoice { if bc == pc {
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf("%s v %s: draw", botEmoji, playerEmoji)) b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf(
"%s v %s: draw", be, pe,
))
return nil return nil
} else if rpsVictoryMap[botChoice] == playerChoice { } else if lib.Contains(rpsVictoryMap[bc], pc) {
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf("%s v %s: bot wins", botEmoji, playerEmoji)) b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf(
"%s v %s: %s wins", be, pe, lib.MapKey(rpsChoiceMap, bc),
))
return nil return nil
} }
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf("%s v %s: you win", botEmoji, playerEmoji)) b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf(
"%s v %s: %s wins", be, pe, lib.MapKey(rpsChoiceMap, pc),
))
return nil return nil
} }
} }

82
bot/rpsls.go Normal file
View File

@ -0,0 +1,82 @@
package bot
import (
"fmt"
"strings"
"git.kill0.net/chill9/beepboop/lib"
"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 int
be, pe string
c string
)
if len(args) != 1 {
b.Session.ChannelMessageSend(
m.ChannelID, "help: `!rpsls (rock | paper | scissors | lizard | spock )`",
)
return nil
}
c = strings.ToLower(args[0])
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
}
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf(
"%s v %s: %s wins", be, pe, lib.MapKey(rpslsChoiceMap, pc),
))
return nil
}
}

View File

@ -141,3 +141,21 @@ func SplitArgs(s string, n int) (args []string) {
} }
return return
} }
func MapKeys[K comparable, V any](m map[K]V) []K {
keys := make([]K, 0, len(m))
for k := range m {
keys = append(keys, k)
}
return keys
}
func MapKey[K comparable, V comparable](m map[K]V, v V) K {
var r K
for k := range m {
if m[k] == v {
return k
}
}
return r
}

View File

@ -123,3 +123,37 @@ func TestSplitArgs(t *testing.T) {
} }
} }
} }
func TestMapKeys(t *testing.T) {
tables := []struct {
m map[string]int
want []string
}{
{map[string]int{"a": 0, "b": 1, "c": 3}, []string{"a", "b", "c"}},
}
for _, table := range tables {
if got, want := MapKeys(table.m), table.want; !reflect.DeepEqual(got, want) {
t.Errorf("got: %#v, want: %#v", got, want)
}
}
}
func TestMapKey(t *testing.T) {
tables := []struct {
m map[string]int
n int
want string
}{
{map[string]int{"a": 0, "b": 1, "c": 2}, 0, "a"},
{map[string]int{"a": 0, "b": 1, "c": 2}, 1, "b"},
{map[string]int{"a": 0, "b": 1, "c": 2}, 2, "c"},
{map[string]int{"a": 0, "b": 1, "c": 2, "d": 0}, 0, "a"},
}
for _, table := range tables {
if got, want := MapKey(table.m, table.n), table.want; got != want {
t.Errorf("got: %#v, want: %#v", got, want)
}
}
}

View File

@ -37,3 +37,15 @@ func SeedMathRand() error {
func RandInt(min int, max int) int { func RandInt(min int, max int) int {
return rand.Intn(max-min+1) + min return rand.Intn(max-min+1) + min
} }
func MapRand[K comparable, V any](m map[K]V) V {
n := rand.Intn(len(m))
i := 0
for _, v := range m {
if i == n {
return v
}
i++
}
panic("unreachable")
}