Compare commits
No commits in common. "333bc3b26c9daf977d016084deab40660fd8fbbf" and "7b3368eea4b62a855bc9f263a76421975b77792c" have entirely different histories.
333bc3b26c
...
7b3368eea4
60
bot/rps.go
60
bot/rps.go
@ -2,9 +2,9 @@ package bot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"strings"
|
||||
|
||||
"git.kill0.net/chill9/beepboop/lib"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
@ -12,15 +12,13 @@ const (
|
||||
Rock = iota
|
||||
Paper
|
||||
Scissors
|
||||
Lizard
|
||||
Spock
|
||||
)
|
||||
|
||||
var (
|
||||
rpsVictoryMap map[int][]int = map[int][]int{
|
||||
Rock: {Scissors},
|
||||
Paper: {Rock},
|
||||
Scissors: {Paper},
|
||||
rpsVictoryMap map[int]int = map[int]int{
|
||||
Rock: Scissors,
|
||||
Paper: Rock,
|
||||
Scissors: Paper,
|
||||
}
|
||||
|
||||
rpsChoiceMap map[string]int = map[string]int{
|
||||
@ -36,49 +34,51 @@ 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 {
|
||||
return func(args []string, m *discordgo.MessageCreate) error {
|
||||
var (
|
||||
bc, pc int
|
||||
be, pe string
|
||||
botChoice, playerChoice int
|
||||
botEmoji, playerEmoji string
|
||||
c string
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
c = strings.ToLower(args[0])
|
||||
|
||||
pc, ok := rpsChoiceMap[c] // player's choice
|
||||
playerChoice, ok := rpsChoiceMap[c]
|
||||
if !ok {
|
||||
b.Session.ChannelMessageSend(
|
||||
m.ChannelID, "help: `!rps (rock | paper | scissors)`",
|
||||
)
|
||||
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
|
||||
botChoice = RpsRand()
|
||||
botEmoji = rpsEmojiMap[botChoice]
|
||||
playerEmoji = rpsEmojiMap[playerChoice]
|
||||
|
||||
if bc == pc {
|
||||
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf(
|
||||
"%s v %s: draw", be, pe,
|
||||
))
|
||||
if botChoice == playerChoice {
|
||||
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf("%s v %s: draw", botEmoji, playerEmoji))
|
||||
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),
|
||||
))
|
||||
} else if rpsVictoryMap[botChoice] == playerChoice {
|
||||
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf("%s v %s: bot wins", botEmoji, playerEmoji))
|
||||
return nil
|
||||
}
|
||||
|
||||
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf(
|
||||
"%s v %s: %s wins", be, pe, lib.MapKey(rpsChoiceMap, pc),
|
||||
))
|
||||
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf("%s v %s: you win", botEmoji, playerEmoji))
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
82
bot/rpsls.go
82
bot/rpsls.go
@ -1,82 +0,0 @@
|
||||
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
|
||||
}
|
||||
}
|
@ -141,21 +141,3 @@ func SplitArgs(s string, n int) (args []string) {
|
||||
}
|
||||
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
|
||||
}
|
||||
|
@ -123,37 +123,3 @@ 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
12
lib/rand.go
12
lib/rand.go
@ -37,15 +37,3 @@ func SeedMathRand() error {
|
||||
func RandInt(min int, max int) int {
|
||||
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")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user