Compare commits
31 Commits
b2e61cd0d2
...
cat-town
Author | SHA1 | Date | |
---|---|---|---|
09ee33a751
|
|||
ee9fb1955c
|
|||
41f53c34c0
|
|||
6c0083e094
|
|||
7f2f579241
|
|||
7d2ac20ce1
|
|||
8f38a3b271
|
|||
96f5f85a27
|
|||
6cf80e09b1
|
|||
30ed115ed0
|
|||
8376608a1e | |||
3de96b813f
|
|||
a73acedc6d
|
|||
92e3d6fe5b
|
|||
395006cdb8
|
|||
622d47071a
|
|||
1452d544bb
|
|||
56c6a8cf09
|
|||
cc29386cee
|
|||
95453bf197
|
|||
a6526af6ff
|
|||
3ea30a997a
|
|||
03edacc8ec
|
|||
d1ffbe25a3
|
|||
dc3a40b19d
|
|||
f2e0de1b66
|
|||
58842504c4
|
|||
1080737931
|
|||
de6e907c60
|
|||
9989801e62
|
|||
8c280c303e
|
1
.gitignore
vendored
1
.gitignore
vendored
@@ -6,3 +6,4 @@
|
||||
**/target/
|
||||
/hello_world/main
|
||||
/aoc*/**/input
|
||||
/arduino/build-*/
|
||||
|
2
arduino/.envrc
Normal file
2
arduino/.envrc
Normal file
@@ -0,0 +1,2 @@
|
||||
export ARDMK_DIR=/usr/share/arduino
|
||||
export ARDMK_VENDOR=archlinux-arduino
|
3
arduino/Makefile
Normal file
3
arduino/Makefile
Normal file
@@ -0,0 +1,3 @@
|
||||
BOARD_TAG = uno
|
||||
|
||||
include $(ARDMK_DIR)/Arduino.mk
|
31
arduino/sos.ino
Normal file
31
arduino/sos.ino
Normal file
@@ -0,0 +1,31 @@
|
||||
#define DIT_DURATION_MS 88
|
||||
#define LETTER_PAUSE_MS 1000
|
||||
|
||||
void setup() {
|
||||
pinMode(13, OUTPUT);
|
||||
}
|
||||
|
||||
void dit() {
|
||||
digitalWrite(13, HIGH);
|
||||
delay(DIT_DURATION_MS);
|
||||
digitalWrite(13, LOW);
|
||||
delay(DIT_DURATION_MS);
|
||||
}
|
||||
|
||||
void dah() {
|
||||
digitalWrite(13, HIGH);
|
||||
delay(DIT_DURATION_MS * 3);
|
||||
digitalWrite(13, LOW);
|
||||
delay(DIT_DURATION_MS);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
dit(); dit(); dit();
|
||||
delay(LETTER_PAUSE_MS);
|
||||
|
||||
dah();
|
||||
delay(LETTER_PAUSE_MS);
|
||||
|
||||
dit(); dit(); dit();
|
||||
delay(LETTER_PAUSE_MS);
|
||||
}
|
@@ -1,4 +0,0 @@
|
||||
# argh command line parser
|
||||
|
||||
> NOTE: much of this is lifted from
|
||||
> https://blog.gopheracademy.com/advent-2014/parsers-lexers/
|
46
argh/argh.go
46
argh/argh.go
@@ -1,46 +0,0 @@
|
||||
package argh
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
var (
|
||||
tracingEnabled = os.Getenv("ARGH_TRACING") == "enabled"
|
||||
traceLogger *log.Logger
|
||||
)
|
||||
|
||||
func init() {
|
||||
if !tracingEnabled {
|
||||
return
|
||||
}
|
||||
|
||||
traceLogger = log.New(os.Stderr, "ARGH TRACING: ", 0)
|
||||
}
|
||||
|
||||
type Argh struct {
|
||||
ParseTree *ParseTree `json:"parse_tree"`
|
||||
}
|
||||
|
||||
func (a *Argh) TypedAST() []TypedNode {
|
||||
return a.ParseTree.typedAST()
|
||||
}
|
||||
|
||||
func (a *Argh) AST() []Node {
|
||||
return a.ParseTree.ast()
|
||||
}
|
||||
|
||||
func tracef(format string, v ...any) {
|
||||
if !tracingEnabled {
|
||||
return
|
||||
}
|
||||
|
||||
if _, file, line, ok := runtime.Caller(2); ok {
|
||||
format = fmt.Sprintf("%v:%v ", filepath.Base(file), line) + format
|
||||
}
|
||||
|
||||
traceLogger.Printf(format, v...)
|
||||
}
|
@@ -1,26 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"git.meatballhat.com/x/box-o-sand/argh"
|
||||
)
|
||||
|
||||
func main() {
|
||||
log.SetFlags(0)
|
||||
|
||||
ast, err := argh.ParseArgs(os.Args, nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
b, err := json.MarshalIndent(ast, "", " ")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println(string(b))
|
||||
}
|
12
argh/go.mod
12
argh/go.mod
@@ -1,12 +0,0 @@
|
||||
module git.meatballhat.com/x/box-o-sand/argh
|
||||
|
||||
go 1.18
|
||||
|
||||
require github.com/pkg/errors v0.9.1
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.0 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/stretchr/testify v1.7.1 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
|
||||
)
|
12
argh/go.sum
12
argh/go.sum
@@ -1,12 +0,0 @@
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
@@ -1,95 +0,0 @@
|
||||
package argh
|
||||
|
||||
import "fmt"
|
||||
|
||||
type ParseTree struct {
|
||||
Nodes []Node `json:"nodes"`
|
||||
}
|
||||
|
||||
func (pt *ParseTree) typedAST() []TypedNode {
|
||||
ret := []TypedNode{}
|
||||
|
||||
for _, node := range pt.Nodes {
|
||||
if _, ok := node.(ArgDelimiter); ok {
|
||||
continue
|
||||
}
|
||||
|
||||
if _, ok := node.(StopFlag); ok {
|
||||
continue
|
||||
}
|
||||
|
||||
ret = append(
|
||||
ret,
|
||||
TypedNode{
|
||||
Type: fmt.Sprintf("%T", node),
|
||||
Node: node,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func (pt *ParseTree) ast() []Node {
|
||||
ret := []Node{}
|
||||
|
||||
for _, node := range pt.Nodes {
|
||||
if _, ok := node.(ArgDelimiter); ok {
|
||||
continue
|
||||
}
|
||||
|
||||
if _, ok := node.(StopFlag); ok {
|
||||
continue
|
||||
}
|
||||
|
||||
if v, ok := node.(Statement); ok {
|
||||
for _, subNode := range v.Nodes {
|
||||
ret = append(ret, subNode)
|
||||
}
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
ret = append(ret, node)
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
type Node interface{}
|
||||
|
||||
type TypedNode struct {
|
||||
Type string `json:"type"`
|
||||
Node Node `json:"node"`
|
||||
}
|
||||
|
||||
type Args struct {
|
||||
Nodes []Node `json:"nodes"`
|
||||
}
|
||||
|
||||
type Statement struct {
|
||||
Nodes []Node `json:"nodes"`
|
||||
}
|
||||
|
||||
type Program struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type Ident struct {
|
||||
Literal string `json:"literal"`
|
||||
}
|
||||
|
||||
type Command struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type Flag struct {
|
||||
Name string `json:"name"`
|
||||
Value *string `json:"value,omitempty"`
|
||||
}
|
||||
|
||||
type StdinFlag struct{}
|
||||
|
||||
type StopFlag struct{}
|
||||
|
||||
type ArgDelimiter struct{}
|
228
argh/parser.go
228
argh/parser.go
@@ -1,228 +0,0 @@
|
||||
package argh
|
||||
|
||||
import (
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
var (
|
||||
errSyntax = errors.New("syntax error")
|
||||
|
||||
DefaultParserConfig = &ParserConfig{
|
||||
Commands: []string{},
|
||||
ValueFlags: []string{},
|
||||
ScannerConfig: DefaultScannerConfig,
|
||||
}
|
||||
)
|
||||
|
||||
func ParseArgs(args []string, pCfg *ParserConfig) (*Argh, error) {
|
||||
reEncoded := strings.Join(args, string(nul))
|
||||
|
||||
return NewParser(
|
||||
strings.NewReader(reEncoded),
|
||||
pCfg,
|
||||
).Parse()
|
||||
}
|
||||
|
||||
type Parser struct {
|
||||
s *Scanner
|
||||
buf ParserBuffer
|
||||
|
||||
commands map[string]struct{}
|
||||
valueFlags map[string]struct{}
|
||||
|
||||
nodes []Node
|
||||
stopSeen bool
|
||||
}
|
||||
|
||||
type ParserBuffer struct {
|
||||
tok Token
|
||||
lit string
|
||||
pos int
|
||||
n int
|
||||
}
|
||||
|
||||
type ParserConfig struct {
|
||||
Commands []string
|
||||
ValueFlags []string
|
||||
ScannerConfig *ScannerConfig
|
||||
}
|
||||
|
||||
type parseDirective struct {
|
||||
Break bool
|
||||
}
|
||||
|
||||
func NewParser(r io.Reader, pCfg *ParserConfig) *Parser {
|
||||
if pCfg == nil {
|
||||
pCfg = DefaultParserConfig
|
||||
}
|
||||
|
||||
parser := &Parser{
|
||||
s: NewScanner(r, pCfg.ScannerConfig),
|
||||
commands: map[string]struct{}{},
|
||||
valueFlags: map[string]struct{}{},
|
||||
}
|
||||
|
||||
for _, command := range pCfg.Commands {
|
||||
parser.commands[command] = struct{}{}
|
||||
}
|
||||
|
||||
for _, valueFlag := range pCfg.ValueFlags {
|
||||
parser.valueFlags[valueFlag] = struct{}{}
|
||||
}
|
||||
|
||||
tracef("NewParser parser=%+#v", parser)
|
||||
tracef("NewParser pCfg=%+#v", pCfg)
|
||||
|
||||
return parser
|
||||
}
|
||||
|
||||
func (p *Parser) Parse() (*Argh, error) {
|
||||
p.nodes = []Node{}
|
||||
|
||||
for {
|
||||
pd, err := p.parseArg()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if pd != nil && pd.Break {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return &Argh{ParseTree: &ParseTree{Nodes: p.nodes}}, nil
|
||||
}
|
||||
|
||||
func (p *Parser) parseArg() (*parseDirective, error) {
|
||||
tok, lit, pos := p.scan()
|
||||
if tok == ILLEGAL {
|
||||
return nil, errors.Wrapf(errSyntax, "illegal value %q at pos=%v", lit, pos)
|
||||
}
|
||||
|
||||
if tok == EOL {
|
||||
return &parseDirective{Break: true}, nil
|
||||
}
|
||||
|
||||
p.unscan()
|
||||
|
||||
node, err := p.nodify()
|
||||
|
||||
tracef("parseArg node=%+#v err=%+#v", node, err)
|
||||
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "value %q at pos=%v", lit, pos)
|
||||
}
|
||||
|
||||
if node != nil {
|
||||
p.nodes = append(p.nodes, node)
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (p *Parser) nodify() (Node, error) {
|
||||
tok, lit, pos := p.scan()
|
||||
|
||||
tracef("nodify tok=%s lit=%q pos=%v", tok, lit, pos)
|
||||
|
||||
switch tok {
|
||||
case IDENT:
|
||||
if len(p.nodes) == 0 {
|
||||
return Program{Name: lit}, nil
|
||||
}
|
||||
|
||||
if _, ok := p.commands[lit]; ok {
|
||||
return Command{Name: lit}, nil
|
||||
}
|
||||
|
||||
return Ident{Literal: lit}, nil
|
||||
case ARG_DELIMITER:
|
||||
return ArgDelimiter{}, nil
|
||||
case COMPOUND_SHORT_FLAG:
|
||||
flagNodes := []Node{}
|
||||
|
||||
for _, r := range lit[1:] {
|
||||
flagNodes = append(
|
||||
flagNodes,
|
||||
Flag{
|
||||
Name: string(r),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
return Statement{Nodes: flagNodes}, nil
|
||||
case SHORT_FLAG:
|
||||
flagName := string(lit[1:])
|
||||
if _, ok := p.valueFlags[flagName]; ok {
|
||||
return p.scanValueFlag(flagName, pos)
|
||||
}
|
||||
|
||||
return Flag{Name: flagName}, nil
|
||||
case LONG_FLAG:
|
||||
flagName := string(lit[2:])
|
||||
if _, ok := p.valueFlags[flagName]; ok {
|
||||
return p.scanValueFlag(flagName, pos)
|
||||
}
|
||||
|
||||
return Flag{Name: flagName}, nil
|
||||
default:
|
||||
}
|
||||
|
||||
return Ident{Literal: lit}, nil
|
||||
}
|
||||
|
||||
func (p *Parser) scanValueFlag(flagName string, pos int) (Node, error) {
|
||||
tracef("scanValueFlag flagName=%q pos=%v", flagName, pos)
|
||||
|
||||
lit, err := p.scanIdent()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return Flag{Name: flagName, Value: ptr(lit)}, nil
|
||||
}
|
||||
|
||||
func (p *Parser) scanIdent() (string, error) {
|
||||
tok, lit, pos := p.scan()
|
||||
|
||||
nUnscan := 0
|
||||
|
||||
if tok == ASSIGN || tok == ARG_DELIMITER {
|
||||
nUnscan++
|
||||
tok, lit, pos = p.scan()
|
||||
}
|
||||
|
||||
if tok == IDENT {
|
||||
return lit, nil
|
||||
}
|
||||
|
||||
for i := 0; i < nUnscan; i++ {
|
||||
p.unscan()
|
||||
}
|
||||
|
||||
return "", errors.Wrapf(errSyntax, "expected ident at pos=%v but got %s (%q)", pos, tok, lit)
|
||||
}
|
||||
|
||||
func (p *Parser) scan() (Token, string, int) {
|
||||
if p.buf.n != 0 {
|
||||
p.buf.n = 0
|
||||
return p.buf.tok, p.buf.lit, p.buf.pos
|
||||
}
|
||||
|
||||
tok, lit, pos := p.s.Scan()
|
||||
|
||||
p.buf.tok, p.buf.lit, p.buf.pos = tok, lit, pos
|
||||
|
||||
return tok, lit, pos
|
||||
}
|
||||
|
||||
func (p *Parser) unscan() {
|
||||
p.buf.n = 1
|
||||
}
|
||||
|
||||
func ptr[T any](v T) *T {
|
||||
return &v
|
||||
}
|
@@ -1,207 +0,0 @@
|
||||
package argh_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.meatballhat.com/x/box-o-sand/argh"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func ptr[T any](v T) *T {
|
||||
return &v
|
||||
}
|
||||
|
||||
func TestParser(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
args []string
|
||||
cfg *argh.ParserConfig
|
||||
expPT []argh.Node
|
||||
expAST []argh.Node
|
||||
expErr error
|
||||
skip bool
|
||||
}{
|
||||
{
|
||||
name: "bare",
|
||||
args: []string{"pizzas"},
|
||||
expPT: []argh.Node{
|
||||
argh.Program{Name: "pizzas"},
|
||||
},
|
||||
expAST: []argh.Node{
|
||||
argh.Program{Name: "pizzas"},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "long value-less flags",
|
||||
args: []string{"pizzas", "--tasty", "--fresh", "--super-hot-right-now"},
|
||||
expPT: []argh.Node{
|
||||
argh.Program{Name: "pizzas"},
|
||||
argh.ArgDelimiter{},
|
||||
argh.Flag{Name: "tasty"},
|
||||
argh.ArgDelimiter{},
|
||||
argh.Flag{Name: "fresh"},
|
||||
argh.ArgDelimiter{},
|
||||
argh.Flag{Name: "super-hot-right-now"},
|
||||
},
|
||||
expAST: []argh.Node{
|
||||
argh.Program{Name: "pizzas"},
|
||||
argh.Flag{Name: "tasty"},
|
||||
argh.Flag{Name: "fresh"},
|
||||
argh.Flag{Name: "super-hot-right-now"},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "long flags mixed",
|
||||
args: []string{"pizzas", "--tasty", "--fresh", "soon", "--super-hot-right-now"},
|
||||
cfg: &argh.ParserConfig{
|
||||
Commands: []string{},
|
||||
ValueFlags: []string{"fresh"},
|
||||
},
|
||||
expPT: []argh.Node{
|
||||
argh.Program{Name: "pizzas"},
|
||||
argh.ArgDelimiter{},
|
||||
argh.Flag{Name: "tasty"},
|
||||
argh.ArgDelimiter{},
|
||||
argh.Flag{Name: "fresh", Value: ptr("soon")},
|
||||
argh.ArgDelimiter{},
|
||||
argh.Flag{Name: "super-hot-right-now"},
|
||||
},
|
||||
expAST: []argh.Node{
|
||||
argh.Program{Name: "pizzas"},
|
||||
argh.Flag{Name: "tasty"},
|
||||
argh.Flag{Name: "fresh", Value: ptr("soon")},
|
||||
argh.Flag{Name: "super-hot-right-now"},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "short value-less flags",
|
||||
args: []string{"pizzas", "-t", "-f", "-s"},
|
||||
expPT: []argh.Node{
|
||||
argh.Program{Name: "pizzas"},
|
||||
argh.ArgDelimiter{},
|
||||
argh.Flag{Name: "t"},
|
||||
argh.ArgDelimiter{},
|
||||
argh.Flag{Name: "f"},
|
||||
argh.ArgDelimiter{},
|
||||
argh.Flag{Name: "s"},
|
||||
},
|
||||
expAST: []argh.Node{
|
||||
argh.Program{Name: "pizzas"},
|
||||
argh.Flag{Name: "t"},
|
||||
argh.Flag{Name: "f"},
|
||||
argh.Flag{Name: "s"},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "compound short flags",
|
||||
args: []string{"pizzas", "-aca", "-blol"},
|
||||
expPT: []argh.Node{
|
||||
argh.Program{Name: "pizzas"},
|
||||
argh.ArgDelimiter{},
|
||||
argh.Statement{
|
||||
Nodes: []argh.Node{
|
||||
argh.Flag{Name: "a"},
|
||||
argh.Flag{Name: "c"},
|
||||
argh.Flag{Name: "a"},
|
||||
},
|
||||
},
|
||||
argh.ArgDelimiter{},
|
||||
argh.Statement{
|
||||
Nodes: []argh.Node{
|
||||
argh.Flag{Name: "b"},
|
||||
argh.Flag{Name: "l"},
|
||||
argh.Flag{Name: "o"},
|
||||
argh.Flag{Name: "l"},
|
||||
},
|
||||
},
|
||||
},
|
||||
expAST: []argh.Node{
|
||||
argh.Program{Name: "pizzas"},
|
||||
argh.Flag{Name: "a"},
|
||||
argh.Flag{Name: "c"},
|
||||
argh.Flag{Name: "a"},
|
||||
argh.Flag{Name: "b"},
|
||||
argh.Flag{Name: "l"},
|
||||
argh.Flag{Name: "o"},
|
||||
argh.Flag{Name: "l"},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "mixed long short value flags",
|
||||
args: []string{"pizzas", "-a", "--ca", "-b", "1312", "-lol"},
|
||||
cfg: &argh.ParserConfig{
|
||||
Commands: []string{},
|
||||
ValueFlags: []string{"b"},
|
||||
},
|
||||
expPT: []argh.Node{
|
||||
argh.Program{Name: "pizzas"},
|
||||
argh.ArgDelimiter{},
|
||||
argh.Flag{Name: "a"},
|
||||
argh.ArgDelimiter{},
|
||||
argh.Flag{Name: "ca"},
|
||||
argh.ArgDelimiter{},
|
||||
argh.Flag{Name: "b", Value: ptr("1312")},
|
||||
argh.ArgDelimiter{},
|
||||
argh.Statement{
|
||||
Nodes: []argh.Node{
|
||||
argh.Flag{Name: "l"},
|
||||
argh.Flag{Name: "o"},
|
||||
argh.Flag{Name: "l"},
|
||||
},
|
||||
},
|
||||
},
|
||||
expAST: []argh.Node{
|
||||
argh.Program{Name: "pizzas"},
|
||||
argh.Flag{Name: "a"},
|
||||
argh.Flag{Name: "ca"},
|
||||
argh.Flag{Name: "b", Value: ptr("1312")},
|
||||
argh.Flag{Name: "l"},
|
||||
argh.Flag{Name: "o"},
|
||||
argh.Flag{Name: "l"},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "commands",
|
||||
args: []string{"pizzas", "fly", "fry"},
|
||||
cfg: &argh.ParserConfig{
|
||||
Commands: []string{"fly", "fry"},
|
||||
ValueFlags: []string{},
|
||||
},
|
||||
expPT: []argh.Node{
|
||||
argh.Program{Name: "pizzas"},
|
||||
argh.ArgDelimiter{},
|
||||
argh.Command{Name: "fly"},
|
||||
argh.ArgDelimiter{},
|
||||
argh.Command{Name: "fry"},
|
||||
},
|
||||
},
|
||||
} {
|
||||
if tc.skip {
|
||||
continue
|
||||
}
|
||||
|
||||
if tc.expPT != nil {
|
||||
t.Run(tc.name+" parse tree", func(ct *testing.T) {
|
||||
actual, err := argh.ParseArgs(tc.args, tc.cfg)
|
||||
if err != nil {
|
||||
assert.ErrorIs(ct, err, tc.expErr)
|
||||
return
|
||||
}
|
||||
|
||||
assert.Equal(ct, tc.expPT, actual.ParseTree.Nodes)
|
||||
})
|
||||
}
|
||||
|
||||
if tc.expAST != nil {
|
||||
t.Run(tc.name+" ast", func(ct *testing.T) {
|
||||
actual, err := argh.ParseArgs(tc.args, tc.cfg)
|
||||
if err != nil {
|
||||
assert.ErrorIs(ct, err, tc.expErr)
|
||||
return
|
||||
}
|
||||
|
||||
assert.Equal(ct, tc.expAST, actual.AST())
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
194
argh/scanner.go
194
argh/scanner.go
@@ -1,194 +0,0 @@
|
||||
package argh
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"errors"
|
||||
"io"
|
||||
"log"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
const (
|
||||
nul = rune(0)
|
||||
eol = rune(-1)
|
||||
)
|
||||
|
||||
var (
|
||||
DefaultScannerConfig = &ScannerConfig{
|
||||
AssignmentOperator: '=',
|
||||
FlagPrefix: '-',
|
||||
MultiValueDelim: ',',
|
||||
}
|
||||
)
|
||||
|
||||
type Scanner struct {
|
||||
r *bufio.Reader
|
||||
i int
|
||||
cfg *ScannerConfig
|
||||
}
|
||||
|
||||
type ScannerConfig struct {
|
||||
AssignmentOperator rune
|
||||
FlagPrefix rune
|
||||
MultiValueDelim rune
|
||||
}
|
||||
|
||||
func NewScanner(r io.Reader, cfg *ScannerConfig) *Scanner {
|
||||
if cfg == nil {
|
||||
cfg = DefaultScannerConfig
|
||||
}
|
||||
|
||||
return &Scanner{
|
||||
r: bufio.NewReader(r),
|
||||
cfg: cfg,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Scanner) Scan() (Token, string, int) {
|
||||
ch, pos := s.read()
|
||||
|
||||
if s.isBlankspace(ch) {
|
||||
_ = s.unread()
|
||||
return s.scanBlankspace()
|
||||
}
|
||||
|
||||
if s.isAssignmentOperator(ch) {
|
||||
return ASSIGN, string(ch), pos
|
||||
}
|
||||
|
||||
if s.isMultiValueDelim(ch) {
|
||||
return MULTI_VALUE_DELIMITER, string(ch), pos
|
||||
}
|
||||
|
||||
if ch == eol {
|
||||
return EOL, "", pos
|
||||
}
|
||||
|
||||
if ch == nul {
|
||||
return ARG_DELIMITER, string(ch), pos
|
||||
}
|
||||
|
||||
if unicode.IsGraphic(ch) {
|
||||
_ = s.unread()
|
||||
return s.scanArg()
|
||||
}
|
||||
|
||||
return ILLEGAL, string(ch), pos
|
||||
}
|
||||
|
||||
func (s *Scanner) read() (rune, int) {
|
||||
ch, _, err := s.r.ReadRune()
|
||||
s.i++
|
||||
|
||||
if errors.Is(err, io.EOF) {
|
||||
return eol, s.i
|
||||
} else if err != nil {
|
||||
log.Printf("unknown scanner error=%+v", err)
|
||||
return eol, s.i
|
||||
}
|
||||
|
||||
return ch, s.i
|
||||
}
|
||||
|
||||
func (s *Scanner) unread() int {
|
||||
_ = s.r.UnreadRune()
|
||||
s.i--
|
||||
return s.i
|
||||
}
|
||||
|
||||
func (s *Scanner) isBlankspace(ch rune) bool {
|
||||
return ch == ' ' || ch == '\t' || ch == '\n'
|
||||
}
|
||||
|
||||
func (s *Scanner) isUnderscore(ch rune) bool {
|
||||
return ch == '_'
|
||||
}
|
||||
|
||||
func (s *Scanner) isFlagPrefix(ch rune) bool {
|
||||
return ch == s.cfg.FlagPrefix
|
||||
}
|
||||
|
||||
func (s *Scanner) isMultiValueDelim(ch rune) bool {
|
||||
return ch == s.cfg.MultiValueDelim
|
||||
}
|
||||
|
||||
func (s *Scanner) isAssignmentOperator(ch rune) bool {
|
||||
return ch == s.cfg.AssignmentOperator
|
||||
}
|
||||
|
||||
func (s *Scanner) scanBlankspace() (Token, string, int) {
|
||||
buf := &bytes.Buffer{}
|
||||
ch, pos := s.read()
|
||||
buf.WriteRune(ch)
|
||||
|
||||
for {
|
||||
ch, pos = s.read()
|
||||
|
||||
if ch == eol {
|
||||
break
|
||||
} else if !s.isBlankspace(ch) {
|
||||
pos = s.unread()
|
||||
break
|
||||
} else {
|
||||
_, _ = buf.WriteRune(ch)
|
||||
}
|
||||
}
|
||||
|
||||
return BS, buf.String(), pos
|
||||
}
|
||||
|
||||
func (s *Scanner) scanArg() (Token, string, int) {
|
||||
buf := &bytes.Buffer{}
|
||||
ch, pos := s.read()
|
||||
buf.WriteRune(ch)
|
||||
|
||||
for {
|
||||
ch, pos = s.read()
|
||||
|
||||
if ch == eol || ch == nul || s.isAssignmentOperator(ch) || s.isMultiValueDelim(ch) {
|
||||
pos = s.unread()
|
||||
break
|
||||
}
|
||||
|
||||
_, _ = buf.WriteRune(ch)
|
||||
}
|
||||
|
||||
str := buf.String()
|
||||
|
||||
if len(str) == 0 {
|
||||
return EMPTY, str, pos
|
||||
}
|
||||
|
||||
ch0 := rune(str[0])
|
||||
|
||||
if len(str) == 1 {
|
||||
if s.isFlagPrefix(ch0) {
|
||||
return STDIN_FLAG, str, pos
|
||||
}
|
||||
|
||||
return IDENT, str, pos
|
||||
}
|
||||
|
||||
ch1 := rune(str[1])
|
||||
|
||||
if len(str) == 2 {
|
||||
if str == string(s.cfg.FlagPrefix)+string(s.cfg.FlagPrefix) {
|
||||
return STOP_FLAG, str, pos
|
||||
}
|
||||
|
||||
if s.isFlagPrefix(ch0) {
|
||||
return SHORT_FLAG, str, pos
|
||||
}
|
||||
}
|
||||
|
||||
if s.isFlagPrefix(ch0) {
|
||||
if s.isFlagPrefix(ch1) {
|
||||
return LONG_FLAG, str, pos
|
||||
}
|
||||
|
||||
return COMPOUND_SHORT_FLAG, str, pos
|
||||
}
|
||||
|
||||
return IDENT, str, pos
|
||||
}
|
@@ -1,22 +0,0 @@
|
||||
//go:generate stringer -type Token
|
||||
|
||||
package argh
|
||||
|
||||
const (
|
||||
ILLEGAL Token = iota
|
||||
EOL
|
||||
EMPTY
|
||||
BS
|
||||
IDENT
|
||||
ARG_DELIMITER
|
||||
COMMAND
|
||||
ASSIGN
|
||||
MULTI_VALUE_DELIMITER
|
||||
LONG_FLAG
|
||||
SHORT_FLAG
|
||||
COMPOUND_SHORT_FLAG
|
||||
STDIN_FLAG
|
||||
STOP_FLAG
|
||||
)
|
||||
|
||||
type Token int
|
@@ -1,36 +0,0 @@
|
||||
// Code generated by "stringer -type Token"; DO NOT EDIT.
|
||||
|
||||
package argh
|
||||
|
||||
import "strconv"
|
||||
|
||||
func _() {
|
||||
// An "invalid array index" compiler error signifies that the constant values have changed.
|
||||
// Re-run the stringer command to generate them again.
|
||||
var x [1]struct{}
|
||||
_ = x[ILLEGAL-0]
|
||||
_ = x[EOL-1]
|
||||
_ = x[EMPTY-2]
|
||||
_ = x[BS-3]
|
||||
_ = x[IDENT-4]
|
||||
_ = x[ARG_DELIMITER-5]
|
||||
_ = x[COMMAND-6]
|
||||
_ = x[ASSIGN-7]
|
||||
_ = x[MULTI_VALUE_DELIMITER-8]
|
||||
_ = x[LONG_FLAG-9]
|
||||
_ = x[SHORT_FLAG-10]
|
||||
_ = x[COMPOUND_SHORT_FLAG-11]
|
||||
_ = x[STDIN_FLAG-12]
|
||||
_ = x[STOP_FLAG-13]
|
||||
}
|
||||
|
||||
const _Token_name = "ILLEGALEOLEMPTYBSIDENTARG_DELIMITERCOMMANDASSIGNMULTI_VALUE_DELIMITERLONG_FLAGSHORT_FLAGCOMPOUND_SHORT_FLAGSTDIN_FLAGSTOP_FLAG"
|
||||
|
||||
var _Token_index = [...]uint8{0, 7, 10, 15, 17, 22, 35, 42, 48, 69, 78, 88, 107, 117, 126}
|
||||
|
||||
func (i Token) String() string {
|
||||
if i < 0 || i >= Token(len(_Token_index)-1) {
|
||||
return "Token(" + strconv.FormatInt(int64(i), 10) + ")"
|
||||
}
|
||||
return _Token_name[_Token_index[i]:_Token_index[i+1]]
|
||||
}
|
3
cat-town/.gitignore
vendored
Normal file
3
cat-town/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
/node_modules/
|
||||
/src/*.js
|
||||
/dist/
|
4838
cat-town/package-lock.json
generated
Normal file
4838
cat-town/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
30
cat-town/package.json
Normal file
30
cat-town/package.json
Normal file
@@ -0,0 +1,30 @@
|
||||
{
|
||||
"name": "cat-town",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"webpack": "webpack",
|
||||
"dev": "webpack serve --config webpack.development.js",
|
||||
"start": "npm run dev",
|
||||
"build:dev": "webpack --config webpack.development.js",
|
||||
"build:prod": "webpack --config webpack.production.js"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@excaliburjs/testing": "^0.25.1",
|
||||
"clean-webpack-plugin": "^3.0.0",
|
||||
"compression-webpack-plugin": "^7.1.2",
|
||||
"html-webpack-plugin": "^5.5.0",
|
||||
"source-map-loader": "^2.0.2",
|
||||
"terser-webpack-plugin": "^5.3.6",
|
||||
"ts-loader": "^9.4.2",
|
||||
"typescript": "^4.9.4",
|
||||
"webpack": "^5.75.0",
|
||||
"webpack-cli": "^4.10.0",
|
||||
"webpack-dev-server": "^4.11.1",
|
||||
"webpack-merge": "^5.8.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"excalibur": "^0.27.0"
|
||||
}
|
||||
}
|
17
cat-town/src/actors/player.ts
Normal file
17
cat-town/src/actors/player.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { Actor, Color, vec } from 'excalibur'
|
||||
import { Resources } from '../resources'
|
||||
|
||||
export class Player extends Actor {
|
||||
constructor() {
|
||||
super({
|
||||
pos: vec(150, 150),
|
||||
width: 25,
|
||||
height: 25,
|
||||
color: new Color(255, 100, 100)
|
||||
})
|
||||
}
|
||||
|
||||
onInitialize() {
|
||||
this.graphics.use(Resources.Cat.toSprite())
|
||||
}
|
||||
}
|
16
cat-town/src/cat.ts
Normal file
16
cat-town/src/cat.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
// type Cat = {
|
||||
// name string;
|
||||
// years int;
|
||||
// centimeters int;
|
||||
// kilograms int;
|
||||
// coloring Coloring;
|
||||
// pattern Pattern;
|
||||
// mood Mood;
|
||||
// };
|
||||
//
|
||||
// type Coloring = 'orange' | 'black' | 'brown' | 'blue';
|
||||
//
|
||||
// type Pattern = 'plain' | 'striped' | 'spotted';
|
||||
//
|
||||
// type Mood = 'happy' | 'sad' | 'purring' |
|
||||
// 'screaming' | 'mad' | 'scratchy' | 'curious' | 'concerned';
|
BIN
cat-town/src/images/cat.png
Normal file
BIN
cat-town/src/images/cat.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 8.8 KiB |
BIN
cat-town/src/images/cat.png-autosave.kra
Normal file
BIN
cat-town/src/images/cat.png-autosave.kra
Normal file
Binary file not shown.
30
cat-town/src/index.ts
Normal file
30
cat-town/src/index.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { Engine, Loader, DisplayMode } from 'excalibur'
|
||||
import { Beginning } from './scenes/beginning'
|
||||
import { Player } from './actors/player'
|
||||
import { Resources } from './resources'
|
||||
|
||||
class Game extends Engine {
|
||||
private player: Player
|
||||
private beginning: Beginning
|
||||
|
||||
constructor() {
|
||||
super({ displayMode: DisplayMode.FitScreen })
|
||||
}
|
||||
|
||||
public start() {
|
||||
this.beginning = new Beginning()
|
||||
this.player = new Player()
|
||||
this.beginning.add(this.player)
|
||||
|
||||
game.add('beginning', this.beginning)
|
||||
|
||||
const loader = new Loader(Object.values(Resources))
|
||||
|
||||
return super.start(loader)
|
||||
}
|
||||
}
|
||||
|
||||
const game = new Game()
|
||||
game.start().then(() => {
|
||||
game.goToScene('beginning')
|
||||
})
|
8
cat-town/src/resources.ts
Normal file
8
cat-town/src/resources.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { ImageSource } from 'excalibur'
|
||||
import catImage from './images/cat.png'
|
||||
|
||||
const Resources = {
|
||||
Cat: new ImageSource(catImage)
|
||||
}
|
||||
|
||||
export { Resources }
|
7
cat-town/src/scenes/beginning.ts
Normal file
7
cat-town/src/scenes/beginning.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { Engine, Scene } from 'excalibur'
|
||||
|
||||
export class Beginning extends Scene {
|
||||
public onInitialize(engine: Engine) {}
|
||||
public onActivate() {}
|
||||
public onDeactivate() {}
|
||||
}
|
22
cat-town/src/town.ts
Normal file
22
cat-town/src/town.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
// type Town = {
|
||||
// grid Grid;
|
||||
// };
|
||||
//
|
||||
// type Grid = {
|
||||
// squares Map<Coords, Square>;
|
||||
// };
|
||||
//
|
||||
// type Coords = {
|
||||
// x int;
|
||||
// y int;
|
||||
// };
|
||||
//
|
||||
// type Square = {
|
||||
// description string;
|
||||
// things Map<string, Thing>;
|
||||
// };
|
||||
//
|
||||
// type Thing = {
|
||||
// name string;
|
||||
// description string;
|
||||
// };
|
14
cat-town/tsconfig.json
Normal file
14
cat-town/tsconfig.json
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"moduleResolution": "node",
|
||||
"sourceMap": true,
|
||||
"target": "es2017",
|
||||
"module": "es6",
|
||||
"types": ["excalibur"],
|
||||
"outDir": "./dist/",
|
||||
"esModuleInterop": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"skipLibCheck": true
|
||||
}
|
||||
}
|
47
cat-town/webpack.common.js
Normal file
47
cat-town/webpack.common.js
Normal file
@@ -0,0 +1,47 @@
|
||||
const path = require("path");
|
||||
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
|
||||
const HtmlWebPackPlugin = require("html-webpack-plugin");
|
||||
|
||||
module.exports = {
|
||||
entry: "./src/index.ts",
|
||||
target: "web",
|
||||
output: {
|
||||
filename: '[name].js',
|
||||
sourceMapFilename: "[file].map",
|
||||
path: path.resolve(__dirname, "dist"),
|
||||
},
|
||||
devtool: "source-map",
|
||||
module: {
|
||||
rules: [
|
||||
{
|
||||
test: /\.(png|svg|jpg|jpeg|gif)$/i,
|
||||
type: "asset/resource"
|
||||
},
|
||||
{
|
||||
test: /\.js$/,
|
||||
use: ["source-map-loader"],
|
||||
exclude: [path.resolve(__dirname, "node_modules/excalibur")],
|
||||
enforce: "pre",
|
||||
},
|
||||
{
|
||||
test: /\.tsx?$/,
|
||||
use: "ts-loader",
|
||||
exclude: /node_modules/,
|
||||
},
|
||||
],
|
||||
},
|
||||
resolve: {
|
||||
extensions: [".tsx", ".ts", ".js"],
|
||||
},
|
||||
optimization: {
|
||||
splitChunks: {
|
||||
chunks: "all",
|
||||
},
|
||||
},
|
||||
plugins: [
|
||||
new CleanWebpackPlugin(),
|
||||
new HtmlWebPackPlugin({
|
||||
title: "Cat Town",
|
||||
}),
|
||||
],
|
||||
};
|
12
cat-town/webpack.development.js
Normal file
12
cat-town/webpack.development.js
Normal file
@@ -0,0 +1,12 @@
|
||||
const { merge } = require("webpack-merge");
|
||||
const common = require("./webpack.common");
|
||||
|
||||
module.exports = merge(common, {
|
||||
mode: "development",
|
||||
devtool: "inline-source-map",
|
||||
devServer: {
|
||||
static: {
|
||||
directory: "./dist",
|
||||
},
|
||||
},
|
||||
});
|
13
cat-town/webpack.production.js
Normal file
13
cat-town/webpack.production.js
Normal file
@@ -0,0 +1,13 @@
|
||||
const { merge } = require("webpack-merge");
|
||||
const CompressionWebpackPlugin = require("compression-webpack-plugin");
|
||||
const TerserPlugin = require("terser-webpack-plugin");
|
||||
const common = require("./webpack.common");
|
||||
|
||||
module.exports = merge(common, {
|
||||
mode: "production",
|
||||
optimization: {
|
||||
minimize: true,
|
||||
minimizer: [new TerserPlugin()],
|
||||
},
|
||||
plugins: [new CompressionWebpackPlugin()],
|
||||
});
|
1876
piston-tutorials/getting-started/Cargo.lock
generated
Normal file
1876
piston-tutorials/getting-started/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
17
piston-tutorials/getting-started/Cargo.toml
Normal file
17
piston-tutorials/getting-started/Cargo.toml
Normal file
@@ -0,0 +1,17 @@
|
||||
[package]
|
||||
name = "spinning-square"
|
||||
version = "0.1.0"
|
||||
authors = [
|
||||
"Dan Buch <dan@meatballhat.com>",
|
||||
"TyOverby <ty@pre-alpha.com>",
|
||||
"Nikita Pekin <contact@nikitapek.in>"
|
||||
]
|
||||
|
||||
[[bin]]
|
||||
name = "spinning-square"
|
||||
|
||||
[dependencies]
|
||||
piston = "0.53.0"
|
||||
piston2d-graphics = "0.43.0"
|
||||
pistoncore-glutin_window = "0.70.1"
|
||||
piston2d-opengl_graphics = "0.82.0"
|
70
piston-tutorials/getting-started/src/main.rs
Normal file
70
piston-tutorials/getting-started/src/main.rs
Normal file
@@ -0,0 +1,70 @@
|
||||
extern crate glutin_window;
|
||||
extern crate graphics;
|
||||
extern crate opengl_graphics;
|
||||
extern crate piston;
|
||||
|
||||
use glutin_window::GlutinWindow as Window;
|
||||
use opengl_graphics::{GlGraphics, OpenGL};
|
||||
use piston::event_loop::{EventSettings, Events};
|
||||
use piston::input::{RenderArgs, RenderEvent, UpdateArgs, UpdateEvent};
|
||||
use piston::window::WindowSettings;
|
||||
|
||||
pub struct App {
|
||||
gl: GlGraphics,
|
||||
rotation: f64,
|
||||
}
|
||||
|
||||
impl App {
|
||||
fn render(&mut self, args: &RenderArgs) {
|
||||
use graphics::*;
|
||||
|
||||
const GREEN: [f32; 4] = [0.0, 1.0, 0.0, 1.0];
|
||||
const RED: [f32; 4] = [1.0, 0.0, 0.0, 1.0];
|
||||
|
||||
let square = rectangle::square(0.0, 0.0, 50.0);
|
||||
let rotation = self.rotation;
|
||||
let (x, y) = (args.window_size[0] / 2.0, args.window_size[1] / 2.0);
|
||||
|
||||
self.gl.draw(args.viewport(), |c, gl| {
|
||||
clear(GREEN, gl);
|
||||
|
||||
let transform = c
|
||||
.transform
|
||||
.trans(x, y)
|
||||
.rot_rad(rotation)
|
||||
.trans(-25.0, -25.0);
|
||||
|
||||
rectangle(RED, square, transform, gl);
|
||||
})
|
||||
}
|
||||
|
||||
fn update(&mut self, args: &UpdateArgs) {
|
||||
self.rotation += 2.0 * args.dt;
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let opengl = OpenGL::V3_2;
|
||||
|
||||
let mut window: Window = WindowSettings::new("spinning-square", [200, 200])
|
||||
.graphics_api(opengl)
|
||||
.exit_on_esc(true)
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
let mut app = App {
|
||||
gl: GlGraphics::new(opengl),
|
||||
rotation: 0.0,
|
||||
};
|
||||
|
||||
let mut events = Events::new(EventSettings::new());
|
||||
while let Some(e) = events.next(&mut window) {
|
||||
if let Some(args) = e.render_args() {
|
||||
app.render(&args);
|
||||
}
|
||||
|
||||
if let Some(args) = e.update_args() {
|
||||
app.update(&args);
|
||||
}
|
||||
}
|
||||
}
|
1876
piston-tutorials/sudoku/Cargo.lock
generated
Normal file
1876
piston-tutorials/sudoku/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
12
piston-tutorials/sudoku/Cargo.toml
Normal file
12
piston-tutorials/sudoku/Cargo.toml
Normal file
@@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "sudoku"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
piston = "0.53.2"
|
||||
piston2d-graphics = "0.43.0"
|
||||
piston2d-opengl_graphics = "0.82.0"
|
||||
pistoncore-glutin_window = "0.70.1"
|
BIN
piston-tutorials/sudoku/assets/FiraSans-Regular.ttf
Normal file
BIN
piston-tutorials/sudoku/assets/FiraSans-Regular.ttf
Normal file
Binary file not shown.
99
piston-tutorials/sudoku/assets/LICENSE
Normal file
99
piston-tutorials/sudoku/assets/LICENSE
Normal file
@@ -0,0 +1,99 @@
|
||||
Copyright (c) 2014, Mozilla Foundation https://mozilla.org/
|
||||
with Reserved Font Name Fira Sans.
|
||||
|
||||
Copyright (c) 2014, Mozilla Foundation https://mozilla.org/
|
||||
with Reserved Font Name Fira Mono.
|
||||
|
||||
Copyright (c) 2014, Telefonica S.A.
|
||||
|
||||
This Font Software is licensed under the SIL Open Font License, Version 1.1.
|
||||
This license is copied below, and is also available with a FAQ at:
|
||||
http://scripts.sil.org/OFL
|
||||
|
||||
|
||||
-----------------------------------------------------------
|
||||
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
|
||||
-----------------------------------------------------------
|
||||
|
||||
PREAMBLE
|
||||
The goals of the Open Font License (OFL) are to stimulate worldwide
|
||||
development of collaborative font projects, to support the font creation
|
||||
efforts of academic and linguistic communities, and to provide a free and
|
||||
open framework in which fonts may be shared and improved in partnership
|
||||
with others.
|
||||
|
||||
The OFL allows the licensed fonts to be used, studied, modified and
|
||||
redistributed freely as long as they are not sold by themselves. The
|
||||
fonts, including any derivative works, can be bundled, embedded,
|
||||
redistributed and/or sold with any software provided that any reserved
|
||||
names are not used by derivative works. The fonts and derivatives,
|
||||
however, cannot be released under any other type of license. The
|
||||
requirement for fonts to remain under this license does not apply
|
||||
to any document created using the fonts or their derivatives.
|
||||
|
||||
DEFINITIONS
|
||||
"Font Software" refers to the set of files released by the Copyright
|
||||
Holder(s) under this license and clearly marked as such. This may
|
||||
include source files, build scripts and documentation.
|
||||
|
||||
"Reserved Font Name" refers to any names specified as such after the
|
||||
copyright statement(s).
|
||||
|
||||
"Original Version" refers to the collection of Font Software components as
|
||||
distributed by the Copyright Holder(s).
|
||||
|
||||
"Modified Version" refers to any derivative made by adding to, deleting,
|
||||
or substituting -- in part or in whole -- any of the components of the
|
||||
Original Version, by changing formats or by porting the Font Software to a
|
||||
new environment.
|
||||
|
||||
"Author" refers to any designer, engineer, programmer, technical
|
||||
writer or other person who contributed to the Font Software.
|
||||
|
||||
PERMISSION & CONDITIONS
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of the Font Software, to use, study, copy, merge, embed, modify,
|
||||
redistribute, and sell modified and unmodified copies of the Font
|
||||
Software, subject to the following conditions:
|
||||
|
||||
1) Neither the Font Software nor any of its individual components,
|
||||
in Original or Modified Versions, may be sold by itself.
|
||||
|
||||
2) Original or Modified Versions of the Font Software may be bundled,
|
||||
redistributed and/or sold with any software, provided that each copy
|
||||
contains the above copyright notice and this license. These can be
|
||||
included either as stand-alone text files, human-readable headers or
|
||||
in the appropriate machine-readable metadata fields within text or
|
||||
binary files as long as those fields can be easily viewed by the user.
|
||||
|
||||
3) No Modified Version of the Font Software may use the Reserved Font
|
||||
Name(s) unless explicit written permission is granted by the corresponding
|
||||
Copyright Holder. This restriction only applies to the primary font name as
|
||||
presented to the users.
|
||||
|
||||
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
|
||||
Software shall not be used to promote, endorse or advertise any
|
||||
Modified Version, except to acknowledge the contribution(s) of the
|
||||
Copyright Holder(s) and the Author(s) or with their explicit written
|
||||
permission.
|
||||
|
||||
5) The Font Software, modified or unmodified, in part or in whole,
|
||||
must be distributed entirely under this license, and must not be
|
||||
distributed under any other license. The requirement for fonts to
|
||||
remain under this license does not apply to any document created
|
||||
using the Font Software.
|
||||
|
||||
TERMINATION
|
||||
This license becomes null and void if any of the above conditions are
|
||||
not met.
|
||||
|
||||
DISCLAIMER
|
||||
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
|
||||
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
|
||||
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
|
||||
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
|
||||
OTHER DEALINGS IN THE FONT SOFTWARE.
|
155
piston-tutorials/sudoku/src/gameboard.rs
Normal file
155
piston-tutorials/sudoku/src/gameboard.rs
Normal file
@@ -0,0 +1,155 @@
|
||||
//! Game board logic.
|
||||
|
||||
use std::fs::read_to_string;
|
||||
|
||||
const SIZE: usize = 9;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq)]
|
||||
pub struct Cell {
|
||||
pub value: u8,
|
||||
pub loaded: bool,
|
||||
pub invalid: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct Gameboard {
|
||||
pub cells: [[Cell; SIZE]; SIZE],
|
||||
pub completed: bool,
|
||||
}
|
||||
|
||||
impl Gameboard {
|
||||
pub fn new() -> Gameboard {
|
||||
Gameboard {
|
||||
cells: [[Cell::default(); SIZE]; SIZE],
|
||||
completed: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_cells(cells: [[u8; SIZE]; SIZE]) -> Gameboard {
|
||||
let mut ret = Gameboard::new();
|
||||
for (i, row) in cells.iter().enumerate() {
|
||||
for (j, &col) in row.iter().enumerate() {
|
||||
ret.cells[i][j] = Cell {
|
||||
value: col,
|
||||
loaded: col != 0,
|
||||
invalid: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
ret
|
||||
}
|
||||
|
||||
pub fn char(&self, ind: [usize; 2]) -> Option<char> {
|
||||
Some(match self.cells[ind[1]][ind[0]].value {
|
||||
1 => '1',
|
||||
2 => '2',
|
||||
3 => '3',
|
||||
4 => '4',
|
||||
5 => '5',
|
||||
6 => '6',
|
||||
7 => '7',
|
||||
8 => '8',
|
||||
9 => '9',
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn set(&mut self, ind: [usize; 2], val: u8) {
|
||||
if !self.cells[ind[1]][ind[0]].loaded {
|
||||
self.validate(ind, val);
|
||||
self.cells[ind[1]][ind[0]].value = val;
|
||||
}
|
||||
|
||||
self.completed = self
|
||||
.cells
|
||||
.iter()
|
||||
.flatten()
|
||||
.all(|cell| !cell.invalid && cell.value != 0);
|
||||
}
|
||||
|
||||
pub fn load_sdm(filename: &str) -> Self {
|
||||
let data = read_to_string(filename).expect("failed to read SDM file");
|
||||
let mut cells = [[Cell::default(); SIZE]; SIZE];
|
||||
let mut row = 0;
|
||||
let mut col = 0;
|
||||
for c in data.chars() {
|
||||
if col == SIZE {
|
||||
col = 0;
|
||||
row += 1;
|
||||
}
|
||||
if let Some(v) = c.to_digit(10) {
|
||||
let value = v as u8;
|
||||
cells[row][col] = Cell {
|
||||
value,
|
||||
loaded: value != 0,
|
||||
invalid: false,
|
||||
};
|
||||
col += 1;
|
||||
}
|
||||
}
|
||||
|
||||
Self {
|
||||
cells,
|
||||
completed: false,
|
||||
}
|
||||
}
|
||||
|
||||
fn validate(&mut self, ind: [usize; 2], val: u8) {
|
||||
let [b, a] = ind;
|
||||
for i in 0..SIZE {
|
||||
if i == a {
|
||||
continue;
|
||||
}
|
||||
if self.cells[a][i].value == val {
|
||||
self.cells[a][b].invalid = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
for i in 0..SIZE {
|
||||
if i == b {
|
||||
continue;
|
||||
}
|
||||
if self.cells[i][b].value == val {
|
||||
self.cells[a][b].invalid = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
let (row, col) = (a / 3, b / 3);
|
||||
for i in 3 * row..3 * row + 3 {
|
||||
for j in 3 * col..3 * col + 3 {
|
||||
if i == a && j == b {
|
||||
continue;
|
||||
}
|
||||
if self.cells[i][j].value == val {
|
||||
self.cells[a][b].invalid = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
self.cells[a][b].invalid = false;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn load_sdm() {
|
||||
let got = Gameboard::load_sdm("static/puzzle.sdm");
|
||||
let want = Gameboard::from_cells([
|
||||
[0, 1, 6, 4, 0, 0, 0, 0, 0],
|
||||
[2, 0, 0, 0, 0, 9, 0, 0, 0],
|
||||
[4, 0, 0, 0, 0, 0, 0, 6, 2],
|
||||
[0, 7, 0, 2, 3, 0, 1, 0, 0],
|
||||
[1, 0, 0, 0, 0, 0, 0, 0, 3],
|
||||
[0, 0, 3, 0, 8, 7, 0, 4, 0],
|
||||
[9, 6, 0, 0, 0, 0, 0, 0, 5],
|
||||
[0, 0, 0, 8, 0, 0, 0, 0, 7],
|
||||
[0, 0, 0, 0, 0, 6, 8, 2, 0],
|
||||
]);
|
||||
assert_eq!(got, want);
|
||||
}
|
||||
}
|
57
piston-tutorials/sudoku/src/gameboard_controller.rs
Normal file
57
piston-tutorials/sudoku/src/gameboard_controller.rs
Normal file
@@ -0,0 +1,57 @@
|
||||
//! Gameboard controller
|
||||
|
||||
use piston::GenericEvent;
|
||||
|
||||
use crate::Gameboard;
|
||||
|
||||
pub struct GameboardController {
|
||||
pub gameboard: Gameboard,
|
||||
pub selected_cell: Option<[usize; 2]>,
|
||||
cursor_pos: [f64; 2],
|
||||
}
|
||||
|
||||
impl GameboardController {
|
||||
pub fn new(gameboard: Gameboard) -> GameboardController {
|
||||
GameboardController {
|
||||
gameboard: gameboard,
|
||||
selected_cell: None,
|
||||
cursor_pos: [0.0; 2],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn event<E: GenericEvent>(&mut self, pos: [f64; 2], size: f64, e: &E) {
|
||||
use piston::input::{Button, Key, MouseButton};
|
||||
|
||||
if let Some(pos) = e.mouse_cursor_args() {
|
||||
self.cursor_pos = pos;
|
||||
}
|
||||
|
||||
if let Some(Button::Mouse(MouseButton::Left)) = e.press_args() {
|
||||
let x = self.cursor_pos[0] - pos[0];
|
||||
let y = self.cursor_pos[1] - pos[1];
|
||||
|
||||
if x >= 0.0 && x < size && y >= 0.0 && y < size {
|
||||
let cell_x = (x / size * 9.0) as usize;
|
||||
let cell_y = (y / size * 9.0) as usize;
|
||||
self.selected_cell = Some([cell_x, cell_y]);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(Button::Keyboard(key)) = e.press_args() {
|
||||
if let Some(ind) = self.selected_cell {
|
||||
match key {
|
||||
Key::D1 => self.gameboard.set(ind, 1),
|
||||
Key::D2 => self.gameboard.set(ind, 2),
|
||||
Key::D3 => self.gameboard.set(ind, 3),
|
||||
Key::D4 => self.gameboard.set(ind, 4),
|
||||
Key::D5 => self.gameboard.set(ind, 5),
|
||||
Key::D6 => self.gameboard.set(ind, 6),
|
||||
Key::D7 => self.gameboard.set(ind, 7),
|
||||
Key::D8 => self.gameboard.set(ind, 8),
|
||||
Key::D9 => self.gameboard.set(ind, 9),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
210
piston-tutorials/sudoku/src/gameboard_view.rs
Normal file
210
piston-tutorials/sudoku/src/gameboard_view.rs
Normal file
@@ -0,0 +1,210 @@
|
||||
//! Gameboard view.
|
||||
|
||||
use graphics::character::CharacterCache;
|
||||
use graphics::types::Color;
|
||||
use graphics::{Context, Graphics};
|
||||
|
||||
use crate::gameboard_controller::GameboardController;
|
||||
|
||||
pub struct GameboardViewSettings {
|
||||
pub position: [f64; 2],
|
||||
pub size: f64,
|
||||
pub background_color: Color,
|
||||
pub border_color: Color,
|
||||
pub board_edge_color: Color,
|
||||
pub section_edge_color: Color,
|
||||
pub cell_edge_color: Color,
|
||||
pub board_edge_radius: f64,
|
||||
pub section_edge_radius: f64,
|
||||
pub cell_edge_radius: f64,
|
||||
pub selected_cell_background_color: Color,
|
||||
pub text_color: Color,
|
||||
pub loaded_cell_background_color: Color,
|
||||
pub invalid_cell_background_color: Color,
|
||||
pub invalid_selected_cell_background_color: Color,
|
||||
pub completed_background_color: Color,
|
||||
}
|
||||
|
||||
impl GameboardViewSettings {
|
||||
pub fn new() -> GameboardViewSettings {
|
||||
GameboardViewSettings {
|
||||
position: [10.0; 2],
|
||||
size: 400.0,
|
||||
background_color: [0.8, 0.8, 1.0, 1.0],
|
||||
border_color: [0.0, 0.0, 0.2, 1.0],
|
||||
board_edge_color: [0.0, 0.0, 0.2, 1.0],
|
||||
section_edge_color: [0.0, 0.0, 0.2, 1.0],
|
||||
cell_edge_color: [0.0, 0.0, 0.2, 1.0],
|
||||
board_edge_radius: 3.0,
|
||||
section_edge_radius: 2.0,
|
||||
cell_edge_radius: 1.0,
|
||||
selected_cell_background_color: [0.9, 0.9, 1.0, 1.0],
|
||||
text_color: [0.0, 0.0, 0.1, 1.0],
|
||||
loaded_cell_background_color: [1.0, 1.0, 1.0, 1.0],
|
||||
invalid_cell_background_color: [1.0, 0.0, 0.0, 1.0],
|
||||
invalid_selected_cell_background_color: [1.0, 0.0, 0.5, 1.0],
|
||||
completed_background_color: [0.0, 1.0, 0.0, 1.0],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct GameboardView {
|
||||
pub settings: GameboardViewSettings,
|
||||
}
|
||||
|
||||
impl GameboardView {
|
||||
pub fn new(settings: GameboardViewSettings) -> GameboardView {
|
||||
GameboardView { settings: settings }
|
||||
}
|
||||
|
||||
pub fn draw<G: Graphics, C>(
|
||||
&self,
|
||||
controller: &GameboardController,
|
||||
glyphs: &mut C,
|
||||
c: &Context,
|
||||
g: &mut G,
|
||||
) where
|
||||
C: CharacterCache<Texture = G::Texture>,
|
||||
{
|
||||
use graphics::{Image, Line, Rectangle, Transformed};
|
||||
|
||||
let ref settings = self.settings;
|
||||
let board_rect = [
|
||||
settings.position[0],
|
||||
settings.position[1],
|
||||
settings.size,
|
||||
settings.size,
|
||||
];
|
||||
|
||||
if controller.gameboard.completed {
|
||||
Rectangle::new(settings.completed_background_color).draw(
|
||||
board_rect,
|
||||
&c.draw_state,
|
||||
c.transform,
|
||||
g,
|
||||
);
|
||||
} else {
|
||||
Rectangle::new(settings.background_color).draw(
|
||||
board_rect,
|
||||
&c.draw_state,
|
||||
c.transform,
|
||||
g,
|
||||
);
|
||||
|
||||
for i in 0..9 {
|
||||
for j in 0..9 {
|
||||
if controller.gameboard.cells[i][j].loaded {
|
||||
color_cell(
|
||||
settings,
|
||||
[j, i],
|
||||
settings.loaded_cell_background_color,
|
||||
c,
|
||||
g,
|
||||
);
|
||||
} else if controller.gameboard.cells[i][j].invalid {
|
||||
color_cell(
|
||||
settings,
|
||||
[j, i],
|
||||
settings.invalid_cell_background_color,
|
||||
c,
|
||||
g,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(ind) = controller.selected_cell {
|
||||
let cell = controller.gameboard.cells[ind[1]][ind[0]];
|
||||
let color = if !cell.loaded {
|
||||
if !cell.invalid {
|
||||
settings.selected_cell_background_color
|
||||
} else {
|
||||
settings.invalid_selected_cell_background_color
|
||||
}
|
||||
} else {
|
||||
settings.loaded_cell_background_color
|
||||
};
|
||||
color_cell(settings, ind, color, c, g);
|
||||
}
|
||||
}
|
||||
|
||||
let text_image = Image::new_color(settings.text_color);
|
||||
let cell_size = settings.size / 9.0;
|
||||
|
||||
for j in 0..9 {
|
||||
for i in 0..9 {
|
||||
if let Some(ch) = controller.gameboard.char([i, j]) {
|
||||
let pos = [
|
||||
settings.position[0] + i as f64 * cell_size + 15.0,
|
||||
settings.position[1] + j as f64 * cell_size + 34.0,
|
||||
];
|
||||
|
||||
if let Ok(character) = glyphs.character(34, ch) {
|
||||
let ch_x = pos[0] + character.left();
|
||||
let ch_y = pos[1] - character.top();
|
||||
let text_image = text_image.src_rect([
|
||||
character.atlas_offset[0],
|
||||
character.atlas_offset[1],
|
||||
character.atlas_size[0],
|
||||
character.atlas_size[1],
|
||||
]);
|
||||
text_image.draw(
|
||||
character.texture,
|
||||
&c.draw_state,
|
||||
c.transform.trans(ch_x, ch_y),
|
||||
g,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let cell_edge = Line::new(settings.cell_edge_color, settings.cell_edge_radius);
|
||||
let section_edge = Line::new(settings.section_edge_color, settings.section_edge_radius);
|
||||
|
||||
for i in 0..9 {
|
||||
let x = settings.position[0] + i as f64 / 9.0 * settings.size;
|
||||
let y = settings.position[1] + i as f64 / 9.0 * settings.size;
|
||||
let x2 = settings.position[0] + settings.size;
|
||||
let y2 = settings.position[1] + settings.size;
|
||||
|
||||
let vline = [x, settings.position[1], x, y2];
|
||||
let hline = [settings.position[0], y, x2, y];
|
||||
|
||||
if (i % 3) == 0 {
|
||||
section_edge.draw(vline, &c.draw_state, c.transform, g);
|
||||
section_edge.draw(hline, &c.draw_state, c.transform, g);
|
||||
} else {
|
||||
cell_edge.draw(vline, &c.draw_state, c.transform, g);
|
||||
cell_edge.draw(hline, &c.draw_state, c.transform, g);
|
||||
}
|
||||
}
|
||||
|
||||
Rectangle::new_border(settings.board_edge_color, settings.board_edge_radius).draw(
|
||||
board_rect,
|
||||
&c.draw_state,
|
||||
c.transform,
|
||||
g,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn color_cell<G: Graphics>(
|
||||
settings: &GameboardViewSettings,
|
||||
ind: [usize; 2],
|
||||
color: [f32; 4],
|
||||
c: &Context,
|
||||
g: &mut G,
|
||||
) {
|
||||
use graphics::Rectangle;
|
||||
|
||||
let cell_size = settings.size / 9.0;
|
||||
let pos = [ind[0] as f64 * cell_size, ind[1] as f64 * cell_size];
|
||||
let cell_rect = [
|
||||
settings.position[0] + pos[0],
|
||||
settings.position[1] + pos[1],
|
||||
cell_size,
|
||||
cell_size,
|
||||
];
|
||||
Rectangle::new(color).draw(cell_rect, &c.draw_state, c.transform, g);
|
||||
}
|
57
piston-tutorials/sudoku/src/main.rs
Normal file
57
piston-tutorials/sudoku/src/main.rs
Normal file
@@ -0,0 +1,57 @@
|
||||
//////#![deny(missing_docs)]
|
||||
|
||||
//! An Sudoko please.
|
||||
|
||||
extern crate glutin_window;
|
||||
|
||||
use glutin_window::GlutinWindow;
|
||||
use opengl_graphics::{Filter, GlGraphics, GlyphCache, OpenGL, TextureSettings};
|
||||
use piston::event_loop::{EventSettings, Events};
|
||||
use piston::{EventLoop, RenderEvent, WindowSettings};
|
||||
|
||||
pub use crate::gameboard::Gameboard;
|
||||
pub use crate::gameboard_controller::GameboardController;
|
||||
pub use crate::gameboard_view::{GameboardView, GameboardViewSettings};
|
||||
|
||||
mod gameboard;
|
||||
mod gameboard_controller;
|
||||
mod gameboard_view;
|
||||
|
||||
fn main() {
|
||||
let opengl = OpenGL::V3_2;
|
||||
let settings = WindowSettings::new("Sudoku", (640, 480))
|
||||
.exit_on_esc(true)
|
||||
.graphics_api(opengl)
|
||||
.vsync(true);
|
||||
let mut window: GlutinWindow = settings.build().expect("could not create window");
|
||||
let mut events = Events::new(EventSettings::new().lazy(true));
|
||||
let mut gl = GlGraphics::new(opengl);
|
||||
|
||||
let args: Vec<_> = std::env::args().collect();
|
||||
let infile = args.get(1).expect("usage: sudoku <sdm-file>");
|
||||
|
||||
let gameboard = Gameboard::load_sdm(infile);
|
||||
let mut gameboard_controller = GameboardController::new(gameboard);
|
||||
let gameboard_view_settings = GameboardViewSettings::new();
|
||||
let gameboard_view = GameboardView::new(gameboard_view_settings);
|
||||
|
||||
let texture_settings = TextureSettings::new().filter(Filter::Nearest);
|
||||
let ref mut glyphs = GlyphCache::new("assets/FiraSans-Regular.ttf", (), texture_settings)
|
||||
.expect("Could not load font");
|
||||
|
||||
while let Some(e) = events.next(&mut window) {
|
||||
gameboard_controller.event(
|
||||
gameboard_view.settings.position,
|
||||
gameboard_view.settings.size,
|
||||
&e,
|
||||
);
|
||||
if let Some(args) = e.render_args() {
|
||||
gl.draw(args.viewport(), |c, g| {
|
||||
use graphics::clear;
|
||||
|
||||
clear([1.0; 4], g);
|
||||
gameboard_view.draw(&gameboard_controller, glyphs, &c, g);
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
1
piston-tutorials/sudoku/static/puzzle-almost-solved.sdm
Normal file
1
piston-tutorials/sudoku/static/puzzle-almost-solved.sdm
Normal file
@@ -0,0 +1 @@
|
||||
517962483236847915498351762371695248654218397829734156765129834142583679983476520
|
1
piston-tutorials/sudoku/static/puzzle.sdm
Normal file
1
piston-tutorials/sudoku/static/puzzle.sdm
Normal file
@@ -0,0 +1 @@
|
||||
016400000200009000400000062070230100100000003003087040960000005000800007000006820
|
Reference in New Issue
Block a user