Reference implementation for the Phoenix Architecture. Work in progress.
aicoding.leaflet.pub/
ai
coding
crazy
1export interface GridCell {
2 isEmpty: boolean;
3 value?: any;
4}
5
6export interface GridPosition {
7 row: number;
8 col: number;
9}
10
11export interface GridDimensions {
12 rows: number;
13 cols: number;
14}
15
16export class Grid {
17 private cells: GridCell[][];
18 private dimensions: GridDimensions;
19
20 constructor(rows: number = 10, cols: number = 10) {
21 this.dimensions = { rows, cols };
22 this.cells = this.initializeEmptyCells();
23 }
24
25 private initializeEmptyCells(): GridCell[][] {
26 const cells: GridCell[][] = [];
27 for (let row = 0; row < this.dimensions.rows; row++) {
28 cells[row] = [];
29 for (let col = 0; col < this.dimensions.cols; col++) {
30 cells[row][col] = { isEmpty: true };
31 }
32 }
33 return cells;
34 }
35
36 public getCell(position: GridPosition): GridCell | null {
37 if (!this.isValidPosition(position)) {
38 return null;
39 }
40 return this.cells[position.row][position.col];
41 }
42
43 public setCell(position: GridPosition, value: any): boolean {
44 if (!this.isValidPosition(position)) {
45 return false;
46 }
47 this.cells[position.row][position.col] = {
48 isEmpty: false,
49 value: value
50 };
51 return true;
52 }
53
54 public clearCell(position: GridPosition): boolean {
55 if (!this.isValidPosition(position)) {
56 return false;
57 }
58 this.cells[position.row][position.col] = { isEmpty: true };
59 return true;
60 }
61
62 public isCellEmpty(position: GridPosition): boolean {
63 const cell = this.getCell(position);
64 return cell ? cell.isEmpty : false;
65 }
66
67 public getDimensions(): GridDimensions {
68 return { ...this.dimensions };
69 }
70
71 public getAllCells(): GridCell[][] {
72 return this.cells.map(row => row.map(cell => ({ ...cell })));
73 }
74
75 public clearAll(): void {
76 this.cells = this.initializeEmptyCells();
77 }
78
79 public getEmptyCells(): GridPosition[] {
80 const emptyCells: GridPosition[] = [];
81 for (let row = 0; row < this.dimensions.rows; row++) {
82 for (let col = 0; col < this.dimensions.cols; col++) {
83 if (this.cells[row][col].isEmpty) {
84 emptyCells.push({ row, col });
85 }
86 }
87 }
88 return emptyCells;
89 }
90
91 public getOccupiedCells(): GridPosition[] {
92 const occupiedCells: GridPosition[] = [];
93 for (let row = 0; row < this.dimensions.rows; row++) {
94 for (let col = 0; col < this.dimensions.cols; col++) {
95 if (!this.cells[row][col].isEmpty) {
96 occupiedCells.push({ row, col });
97 }
98 }
99 }
100 return occupiedCells;
101 }
102
103 private isValidPosition(position: GridPosition): boolean {
104 return position.row >= 0 &&
105 position.row < this.dimensions.rows &&
106 position.col >= 0 &&
107 position.col < this.dimensions.cols;
108 }
109}
110
111export function createGrid(rows?: number, cols?: number): Grid {
112 return new Grid(rows, cols);
113}
114
115/** @internal Phoenix VCS traceability — do not remove. */
116export const _phoenix = {
117 iu_id: '076f6719d071712bc9eabad5d3df9adf59f4ed9cb24991a72f6ad87f06168ca0',
118 name: 'Grid',
119 risk_tier: 'low',
120 canon_ids: [2 as const],
121} as const;