diff options
Diffstat (limited to '')
-rw-r--r-- | src/field.nim | 3 | ||||
-rw-r--r-- | src/stack.nim | 16 | ||||
-rw-r--r-- | src/stackStack.nim | 22 | ||||
-rw-r--r-- | tests/field.nim | 9 | ||||
-rw-r--r-- | tests/stack.nim | 3 | ||||
-rw-r--r-- | tests/stackStack.nim | 39 |
6 files changed, 90 insertions, 2 deletions
diff --git a/src/field.nim b/src/field.nim index a9d706e..0a741c9 100644 --- a/src/field.nim +++ b/src/field.nim @@ -213,3 +213,6 @@ func Step*(f: Field, v: tuple[x, y: int], d: tuple[x, y: int]): (int, int) = if not f.IsIn(x2, y2): return (x, y) x = x2; y = y2 + +func GetSize*(f: Field): (int, int, int, int) = + return (f.x, f.y, f.lx, f.ly) diff --git a/src/stack.nim b/src/stack.nim index f08baf0..4565b5c 100644 --- a/src/stack.nim +++ b/src/stack.nim @@ -80,3 +80,19 @@ func Discard*(s: var Stack, n: int) = func Next*(s: Stack): ref Stack = return s.next + +func GetHeights*(s: Stack): seq[int] = + if s.next != nil: + result = s.next[].GetHeights() + result.add(s.height) + else: + return @[s.height] + +func YCommandPick*(s: var Stack, n, h: int) = + if n > s.height: + s.height = 1 + s.data[0] = 0 + else: + let v = s.data[s.height-n] + s.height = h + s.Push(v) diff --git a/src/stackStack.nim b/src/stackStack.nim index d55d5d9..b2bf29f 100644 --- a/src/stackStack.nim +++ b/src/stackStack.nim @@ -52,8 +52,7 @@ func End*(ss: var StackStack, v: var tuple[x, y: int]): bool = let toss = ss.head let soss = toss[].Next() let n = toss[].Pop() - v.y = soss[].Pop() - v.x = soss[].Pop() + (v.x, v.y) = soss[].PopVector() if n > 0: soss[].Transfert(toss[], n) else: @@ -61,3 +60,22 @@ func End*(ss: var StackStack, v: var tuple[x, y: int]): bool = dec ss.height ss.head = soss return false + +func Under*(ss: var StackStack): bool = + if ss.height == 1: + return true + let soss = ss.head[].Next() + let n = ss.Pop() + if n > 0: + for i in 0..<n: + ss.Push(soss[].Pop()) + else: + for i in 0 ..< -n: + soss[].Push(ss.Pop()) + return false + +func GetHeights*(ss: StackStack): (int, seq[int]) = + return (ss.height, ss.head[].GetHeights) + +func YCommandPick*(ss: var StackStack, n, h: int) = + ss.head[].YCommandPick(n, h) diff --git a/tests/field.nim b/tests/field.nim index e60887a..455d81f 100644 --- a/tests/field.nim +++ b/tests/field.nim @@ -216,3 +216,12 @@ suite "Field": 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) + test "GetSize": + var minimal = Load("examples/minimal.b98") + var hello = Load("examples/hello.b98") + var hello2 = Load("examples/hello2.b98") + var dna = Load("examples/dna.b98") + check minimal[].GetSize() == (0, 0, 1, 1) + check hello[].GetSize() == (0, 0, 24, 1) + check hello2[].GetSize() == (1, 0, 33, 2) + check dna[].GetSize() == (0, 0, 7, 8) diff --git a/tests/stack.nim b/tests/stack.nim index cecdef9..ebbbe5d 100644 --- a/tests/stack.nim +++ b/tests/stack.nim @@ -144,3 +144,6 @@ suite "Stack": check empty[].Next() == nil var some = NewStack(next = empty) check some[].Next() == empty + test "GetHeights": + var empty = NewStack() + check empty[].GetHeights == @[0] diff --git a/tests/stackStack.nim b/tests/stackStack.nim index 5451d79..8ff3e62 100644 --- a/tests/stackStack.nim +++ b/tests/stackStack.nim @@ -110,3 +110,42 @@ suite "StackStack": check empty.height == 1 check empty[].Pop() == 1 check empty[].Pop() == 0 + test "Under": + var empty = NewStackStack() + empty[].Push(18) + check empty[].Under() == true + check empty[].Pop() == 18 + empty[].Push(1) + empty[].Push(2) + empty[].Push(3) + empty[].Push(4) + empty[].Push(5) + empty[].Push(6) + empty[].Push(0) + empty[].Begin((7, 8)) + empty[].Push(9) + empty[].Push(0) + check empty[].Under() == false + check empty[].Pop() == 9 + empty[].Push(9) + empty[].Push(2) + check empty[].Under() == false + check empty[].Pop() == 7 + check empty[].Pop() == 8 + check empty[].Pop() == 9 + empty[].Push(9) + empty[].Push(10) + empty[].Push(-3) + check empty[].Under() == false + check empty[].Pop() == 0 + check empty[].Pop() == 0 + check empty[].Pop() == 0 + var v: tuple[x, y: int] + check empty[].End(v) == false + check v.x == 9 + check v.y == 0 + check empty[].Pop() == 10 + check empty[].Pop() == 6 + test "GetHeights": + var empty = NewStackStack() + check empty[].GetHeights == (1, @[0]) |