Skip to content

Commit

Permalink
Merge pull request #60 from rokostik/ebitengine-game-of-life
Browse files Browse the repository at this point in the history
Add ebitengine game of life example
  • Loading branch information
refaktor authored Dec 13, 2023
2 parents bb03052 + 1404121 commit d13d6e4
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
45 changes: 45 additions & 0 deletions evaldo/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"math"
"math/rand"
"os"
"os/exec"
"reflect"
Expand Down Expand Up @@ -1313,6 +1314,20 @@ var builtins = map[string]*env.Builtin{
},
}, */

"random-integer": {
Argsn: 1,
Doc: "Accepts an integer n and eturns a random integer between 0 and n in the half-open interval [0,n).",
Pure: true,
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch arg := arg0.(type) {
case env.Integer:
return *env.NewInteger(rand.Int63n(arg.Value))
default:
return MakeArgError(ps, 1, []env.Type{env.IntegerType}, "random")
}
},
},

"load": { // **
Argsn: 1,
Doc: "Loads a string into Rye values.",
Expand Down Expand Up @@ -5598,6 +5613,36 @@ var builtins = map[string]*env.Builtin{
}
},
},

"change\\nth!": { // **
Argsn: 3,
Doc: "Accepts a Block or List, Integer n and a value. Changes the n-th value in the Block in place. Also returns the new series.",
Pure: false,
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch num := arg1.(type) {
case env.Integer:
switch s1 := arg0.(type) {
case env.Block:
if num.Value > int64(s1.Series.Len()) {
return MakeBuiltinError(ps, fmt.Sprintf("Block has less than %d elements.", num.Value), "change\\nth!")
}
s1.Series.S[num.Value-1] = arg2
return s1
case env.List:
if num.Value > int64(len(s1.Data)) {
return MakeBuiltinError(ps, fmt.Sprintf("List has less than %d elements.", num.Value), "change\\nth!")
}
s1.Data[num.Value-1] = env.RyeToRaw(arg2)
return s1
default:
return MakeArgError(ps, 1, []env.Type{env.BlockType}, "change\\nth!")
}
default:
return MakeArgError(ps, 2, []env.Type{env.IntegerType}, "change\\nth!")
}
},
},

// FUNCTIONALITY AROUND GENERIC METHODS
// generic <integer> <add> fn [ a b ] [ a + b ] // tagwords are temporary here
"generic": {
Expand Down
86 changes: 86 additions & 0 deletions examples/ebitengine/life.rye
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
w: 320
h: 240

maxInitAliveCells: w * h / 10

world: list produce w * h { } { .concat false }

loop maxInitAliveCells {
x: random-integer w
y: random-integer h
change\nth! world 1 + x + y * w true
}

pixels: list produce w * h * 4 { } { .concat 0 }


neighbourCount: fn { x y } {
c: 0
neg: 0 - 1 ; TODO loading literal -1 doesn't work
ixs: vals { neg 0 1 }
for ixs { :i
for ixs { :j
if any { not i = 0 not j = 0 } {
x2: x + i
y2: y + j
if all { not x2 < 0 not y2 < 0 not w <= x2 not h <= y2 } {
if world .nth 1 + x2 + y2 * w {
inc! 'c
}
}
}
}
}
return c
}

on-update {
print "on-update"
next: list produce w * h { } { .concat false }

loop w { :x1
x: x1 - 1
loop h { :y1
y: y1 - 1
pop: neighbourCount x y
ix: 1 + x + y * w
either pop < 2 {
change\nth! next ix false
} {
either all { any { pop = 2 pop = 3 } world .nth ix } {
change\nth! next ix true
} {
either pop > 3 {
change\nth! next ix false
} { if pop = 3 {
change\nth! next ix true }
}
}
}
}
}
world: next
}

on-draw { :screen
print "on-draw"
loop w * h { :i1
i: i1 - 1
ix: 1 + i * 4
either world .nth 1 + i {
change\nth! pixels ix 255
change\nth! pixels ix + 1 255
change\nth! pixels ix + 2 255
change\nth! pixels ix + 3 255
} {
change\nth! pixels ix 0
change\nth! pixels ix + 1 0
change\nth! pixels ix + 2 0
change\nth! pixels ix + 3 0
}
}

screen .write-pixels pixels
}

ebitengine-run w h
13 changes: 13 additions & 0 deletions tests/builtins.rye
Original file line number Diff line number Diff line change
Expand Up @@ -1235,6 +1235,19 @@ section "Functions that change values in-place"
equal { b: { 1 2 3 } , append! { 4 5 } 'b , b } { 1 2 3 { 4 5 } }
}

group "change\ nth!"
mold\nowrap ?change\nth!
{ { word } { object } }
{
equal { b: { 1 2 3 } , change\nth! b 2 4 } { 1 4 3 }
equal { b: { 1 2 3 } , change\nth! b 2 { 4 5 } } { 1 { 4 5 } 3 }
equal { b: list { 1 2 3 } , change\nth! b 2 4 } list { 1 4 3 }
equal { b: list { 1 2 3 } , change\nth! b 2 list { 4 5 } } list vals { 1 list { 4 5 } 3 }
equal { try { b: { 1 2 3 } , change\nth! b 4 0 } |type? } 'error
equal { try { b: list { 1 2 3 } , change\nth! b 4 0 } |type? } 'error
}


group "sort!"
mold\nowrap ?append!
{ { word } { object } }
Expand Down

0 comments on commit d13d6e4

Please sign in to comment.