From 9a5ff5f233e1874860e4ce834e9a8a58ad7eda85 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Thu, 30 Sep 2021 21:41:27 +0200 Subject: Began implementing the funge field --- src/field.nim | 37 +++++++++++++++++++++++++++++++++++++ src/nimfunge98.nim | 5 +++++ 2 files changed, 42 insertions(+) create mode 100644 src/field.nim create mode 100644 src/nimfunge98.nim (limited to 'src') diff --git a/src/field.nim b/src/field.nim new file mode 100644 index 0000000..a9d26fc --- /dev/null +++ b/src/field.nim @@ -0,0 +1,37 @@ +type + Line = ref object + x, l: int + columns: seq[int] + + Field* = ref object + x, y: int + lx, ly: int + lines: seq[Line] + +proc get*(f: Field, x, y: int): int = + if y >= f.y and y < f.y + f.ly: + let l = f.lines[y-f.y] + if x >= l.x and x < l.x + l.l: + return l.columns[x-l.x] + return int(' ') + +proc isIn*(f: Field, x, y: int): bool = + return x >= f.x and y >= f.y and x < f.x+f.lx and y < f.y+f.ly + +when defined(unitTesting): + let minimal = Field( + x: 0, + y: 0, + lx: 1, + ly: 1, + lines: @[ + Line(x: 0, l: 1, columns: @[int('@')]) + ] + ) + suite "Field": + test "Field.get": + check minimal.get(0,0) == int('@') + check minimal.get(1,0) == int(' ') + test "Field.isIn": + check minimal.isIn(0, 0) == true + check minimal.isIn(1, 0) == false diff --git a/src/nimfunge98.nim b/src/nimfunge98.nim new file mode 100644 index 0000000..e8f1ce4 --- /dev/null +++ b/src/nimfunge98.nim @@ -0,0 +1,5 @@ +import nimfunge98/field/field + +let f:Field + +echo f.get(3,2) \ No newline at end of file -- cgit v1.2.3