aboutsummaryrefslogtreecommitdiff
path: root/src/field.nim
blob: a9d26fc9b166f02e60aacd3056529c9724f8c252 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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