Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(examples): add r/demo/games/tictactoe 1P-VS-CPU #2554

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions examples/gno.land/p/demo/ternary/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Ternary package

Ternary operator have notoriously been absent from Go
from its inception.

This package proposes ternary functions.

We don't advocate for their systematic use, but
it can often prove useful when realms need to generate
Markdown,

## Usage
```go
import "p/demo/ternary"

func Render(path string) string {
// display appropriate greeting
return "# " + ternary.String(isEarly, "hi", "bye")
}
```

Another example:

`f := ternary.Float64(useGoldenRatio, 1.618, 1.66)`

## List of functions

Most native types got a function.

Note: both branches yes/no get evaluated, contrarily to the C operator.
Please don't use this if your branches are expensive.

Functions:

* func String(cond bool, yes, no string) string
* func Int(cond bool, yes, no int) int
* func Int8(cond bool, yes, no int8) int8
* func Int16(cond bool, yes, no int16) int16
* func Int32(cond bool, yes, no int32) int32
* func Int64(cond bool, yes, no int64) int64
* func Uint(cond bool, yes, no uint) uint
* func Uint8(cond bool, yes, no uint8) uint8
* func Uint16(cond bool, yes, no uint16) uint16
* func Uint32(cond bool, yes, no uint32) uint32
* func Uint64(cond bool, yes, no uint64) uint64
* func Float32(cond bool, yes, no float32) float32
* func Float64(cond bool, yes, no float64) float64
* func Rune(cond bool, yes, no rune) rune
* func Bool(cond bool, yes, no bool) rune
* func Address(cond bool, std.Address, std.Address) std.Address

1 change: 1 addition & 0 deletions examples/gno.land/p/demo/ternary/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/p/demo/ternary
115 changes: 115 additions & 0 deletions examples/gno.land/p/demo/ternary/ternary.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package ternary

import "std"

func String(cond bool, yes, no string) string {
if cond {
return yes
}
return no
}

func Int(cond bool, yes, no int) int {
if cond {
return yes
}
return no
}

func Int8(cond bool, yes, no int8) int8 {
if cond {
return yes
}
return no
}

func Int16(cond bool, yes, no int16) int16 {
if cond {
return yes
}
return no
}

func Int32(cond bool, yes, no int32) int32 {
if cond {
return yes
}
return no
}

func Int64(cond bool, yes, no int64) int64 {
if cond {
return yes
}
return no
}

func Uint(cond bool, yes, no uint) uint {
if cond {
return yes
}
return no
}

func Uint8(cond bool, yes, no uint8) uint8 {
if cond {
return yes
}
return no
}

func Uint16(cond bool, yes, no uint16) uint16 {
if cond {
return yes
}
return no
}

func Uint32(cond bool, yes, no uint32) uint32 {
if cond {
return yes
}
return no
}

func Uint64(cond bool, yes, no uint64) uint64 {
if cond {
return yes
}
return no
}

func Float32(cond bool, yes, no float32) float32 {
if cond {
return yes
}
return no
}

func Float64(cond bool, yes, no float64) float64 {
if cond {
return yes
}
return no
}

func Rune(cond bool, yes, no rune) rune {
if cond {
return yes
}
return no
}

func Bool(cond bool, yes, no bool) bool {
if cond {
return yes
}
return no
}

func Address(cond bool, yes, no std.Address) std.Address {
if cond {
return yes
}
return no
}
48 changes: 48 additions & 0 deletions examples/gno.land/p/demo/ternary/ternary_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package ternary

import (
"std"
"testing"
)

func TestTernary(t *testing.T) {
assert(t, String(true, "a", "b") == "a")
assert(t, String(false, "a", "b") == "b")
assert(t, Int(true, 0, 1) == 0)
assert(t, Int(false, 0, 1) == 1)
assert(t, Int8(true, 0, 1) == 0)
assert(t, Int8(false, 0, 1) == 1)
assert(t, Int16(true, 0, 1) == 0)
assert(t, Int16(false, 0, 1) == 1)
assert(t, Int32(true, 0, 1) == 0)
assert(t, Int32(false, 0, 1) == 1)
assert(t, Int64(true, 0, 1) == 0)
assert(t, Int64(false, 0, 1) == 1)
assert(t, Uint(true, 0, 1) == 0)
assert(t, Uint(false, 0, 1) == 1)
assert(t, Uint8(true, 0, 1) == 0)
assert(t, Uint8(false, 0, 1) == 1)
assert(t, Uint16(true, 0, 1) == 0)
assert(t, Uint16(false, 0, 1) == 1)
assert(t, Uint32(true, 0, 1) == 0)
assert(t, Uint32(false, 0, 1) == 1)
assert(t, Uint64(true, 0, 1) == 0)
assert(t, Uint64(false, 0, 1) == 1)
assert(t, Float32(true, 3.14, 1.618) == 3.14)
assert(t, Float32(false, 3.14, 1.618) == 1.618)
assert(t, Float64(true, 3.14, 1.618) == 3.14)
assert(t, Float64(false, 3.14, 1.618) == 1.618)
assert(t, Rune(true, '是', '否') == '是')
assert(t, Rune(false, '是', '否') == '否')
n := 17
assert(t, !Bool(true, n%2 == 0, n < 10))
assert(t, Address(true, std.Address("g0"), std.Address("g1")).String() == "g0")
assert(t, Address(false, std.Address("g0"), std.Address("g1")).String() == "g1")
}

func assert(t *testing.T, val bool) {
t.Helper()
if !val {
t.Errorf("expected true, got false")
}
}
Loading
Loading