From f6bd9fa1a8e541c4aaae7272f38a6e73e2b3e233 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Tue, 5 Oct 2021 22:46:37 +0200 Subject: Implemented steps over a funge space field --- src/field.nim | 15 +++++++++++++++ tests/field.nim | 11 +++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/field.nim b/src/field.nim index e15d7b4..a9d706e 100644 --- a/src/field.nim +++ b/src/field.nim @@ -198,3 +198,18 @@ func Set*(f: var Field, x, y, v: int) = f.x = x if f.lx < x-f.x+1: f.lx = x-f.x+1 + +func Step*(f: Field, v: tuple[x, y: int], d: tuple[x, y: int]): (int, int) = + var x = v.x + d.x + var y = v.y + d.y + if f.IsIn(x, y): + return (x, y) + # We are stepping outside, we need to wrap the Lahey-space + x = v.x + y = v.y + while true: + let x2 = x - d.x + let y2 = y - d.y + if not f.IsIn(x2, y2): + return (x, y) + x = x2; y = y2 diff --git a/tests/field.nim b/tests/field.nim index dcb7530..e60887a 100644 --- a/tests/field.nim +++ b/tests/field.nim @@ -205,3 +205,14 @@ suite "Field": ]) f.Set(-7, 4, int('y')) check f == xprependyappend + test "Step": + var minimal = Load("examples/minimal.b98") + var hello = Load("examples/hello.b98") + var dna = Load("examples/dna.b98") + check minimal[].Step((0, 0), (0, 0)) == (0, 0) + check minimal[].Step((0, 0), (1, 0)) == (0, 0) + check hello[].Step((3, 0), (0, 0)) == (3, 0) + check hello[].Step((3, 0), (1, 0)) == (4, 0) + check dna[].Step((1, 2), (3, 5)) == (4, 7) + check dna[].Step((6, 1), (1, 1)) == (5, 0) + check dna[].Step((1, 4), (-2, 2)) == (5, 0) -- cgit v1.2.3