Playground
gnomod.toml
module = "gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/daily/rps/v0" gno = "0.9" [addpkg] creator = "g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv" height = 751709 max_deposit = "5000000ugnot"
rps.gno
// Package rps is the rock-paper-scissors move: parsing it, naming it, and
// deciding a throw. It holds no state, so every realm that plays the game
// agrees on what "r" means and who wins.
//
// Imported by [r/moul/x/daily/rps](/r/moul/x/daily/rps/v1),
// [r/moul/x/daily/rpsmatch](/r/moul/x/daily/rpsmatch/v1),
// [r/moul/x/daily/rpsoracle](/r/moul/x/daily/rpsoracle/v1) and
// [r/moul/x/daily/rpsduel](/r/moul/x/daily/rpsduel/v1).
package rps
import "strings"
// Move is one of rock, paper, or scissors, ordered so that
// (winner - loser + 3) % 3 == 1 for every winning pair.
type Move int
const (
Rock Move = iota
Paper
Scissors
)
// String returns "rock", "paper" or "scissors", and "?" for anything else.
func (m Move) String() string {
switch m {
case Rock:
return "rock"
case Paper:
return "paper"
case Scissors:
return "scissors"
default:
return "?"
}
}
// Parse reads a move, case-insensitively and ignoring surrounding spaces.
// The full name and its first letter are both accepted.
func Parse(s string) (Move, bool) {
switch strings.ToLower(strings.TrimSpace(s)) {
case "rock", "r":
return Rock, true
case "paper", "p":
return Paper, true
case "scissors", "s":
return Scissors, true
default:
return 0, false
}
}
// Beats decides a throw: 0 for a tie, 1 if a beats b, 2 if b beats a.
// The three values are (a - b) mod 3, which is why Move is ordered as it is.
func Beats(a, b Move) int {
return (int(a) - int(b) + 3) % 3
}
rps_test.gno
package rps
import (
"testing"
"gno.land/p/nt/uassert/v0"
)
func TestParse(t *testing.T) {
cases := []struct {
in string
want Move
ok bool
}{
{"rock", Rock, true},
{"Rock", Rock, true},
{" r ", Rock, true},
{"PAPER", Paper, true},
{"p", Paper, true},
{" scissors ", Scissors, true},
{"S", Scissors, true},
{"lizard", 0, false},
{"", 0, false},
{"ro", 0, false},
}
for _, c := range cases {
got, ok := Parse(c.in)
uassert.Equal(t, c.ok, ok, c.in)
uassert.Equal(t, int(c.want), int(got), c.in)
}
}
func TestString(t *testing.T) {
uassert.Equal(t, "rock", Rock.String())
uassert.Equal(t, "paper", Paper.String())
uassert.Equal(t, "scissors", Scissors.String())
uassert.Equal(t, "?", Move(3).String())
uassert.Equal(t, "?", Move(-1).String())
}
func TestBeats(t *testing.T) {
cases := []struct {
a, b Move
want int
}{
{Rock, Rock, 0},
{Paper, Paper, 0},
{Scissors, Scissors, 0},
{Paper, Rock, 1},
{Scissors, Paper, 1},
{Rock, Scissors, 1},
{Rock, Paper, 2},
{Paper, Scissors, 2},
{Scissors, Rock, 2},
}
for _, c := range cases {
uassert.Equal(t, c.want, Beats(c.a, c.b), c.a.String()+" vs "+c.b.String())
}
}
// Every move parses back from its own name.
func TestRoundTrip(t *testing.T) {
for _, m := range []Move{Rock, Paper, Scissors} {
got, ok := Parse(m.String())
uassert.True(t, ok)
uassert.Equal(t, int(m), int(got))
}
}