-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy patharray_grid.ail
More file actions
61 lines (53 loc) · 1.82 KB
/
Copy patharray_grid.ail
File metadata and controls
61 lines (53 loc) · 1.82 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
-- Array Grid Example
-- Demonstrates using arrays for 2D grid operations (game use case)
-- Uses flat array with row-major indexing for O(1) cell access
module examples/runnable/array_grid
import std/io (print)
import std/array as A
-- Grid dimensions
let WIDTH = 5
let HEIGHT = 3
-- Helper: get index from (x, y) coordinates
pure func idx(x: int, y: int) -> int {
y * WIDTH + x
}
-- Get cell value
pure func getCell(grid: Array[int], x: int, y: int) -> int {
A.get(grid, idx(x, y))
}
-- Set cell value (returns new grid)
pure func setCell(grid: Array[int], x: int, y: int, value: int) -> Array[int] {
A.set(grid, idx(x, y), value)
}
-- Print row helper
pure func rowToString(grid: Array[int], y: int, x: int) -> string {
if x >= WIDTH then ""
else "${show(getCell(grid, x, y))} ${rowToString(grid, y, x + 1)}"
}
export func main() -> () ! {IO} =
-- Create a grid (flat array of size WIDTH * HEIGHT)
let emptyGrid = A.make(WIDTH * HEIGHT, 0) in
-- Create a sample grid with some values
let grid1 = setCell(emptyGrid, 0, 0, 1) in
let grid2 = setCell(grid1, 2, 1, 5) in
let grid3 = setCell(grid2, 4, 2, 9) in
-- Read some cells
let topLeft = getCell(grid3, 0, 0) in
let middle = getCell(grid3, 2, 1) in
let bottomRight = getCell(grid3, 4, 2) in
let empty = getCell(grid3, 1, 1) in
{
print("=== 2D Grid Example ===");
print("Grid size: ${show(WIDTH)}x${show(HEIGHT)}");
print("");
print("=== Cell Values ===");
print("Top-left (0,0): ${show(topLeft)}");
print("Middle (2,1): ${show(middle)}");
print("Bottom-right (4,2): ${show(bottomRight)}");
print("Empty cell (1,1): ${show(empty)}");
print("");
print("=== Grid Layout ===");
print("Row 0: ${rowToString(grid3, 0, 0)}");
print("Row 1: ${rowToString(grid3, 1, 0)}");
print("Row 2: ${rowToString(grid3, 2, 0)}")
}