ZenScript main repository
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConwayGrid.zs 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import example.gui.UpdatableGrid;
  2. public class ConwayGrid {
  3. public var cells as Cell[,];
  4. private var width as usize;
  5. private var height as usize;
  6. public this(width as usize, height as usize) {
  7. this.cells = new Cell[,](width, height, (row, column) => new Cell(row, column));
  8. this.width = width;
  9. this.height = height;
  10. }
  11. public []=(row as int, column as int, alive as bool) as void {
  12. this.cells[row,column].alive = alive;
  13. }
  14. public update() as void {
  15. var gridCapture = this;
  16. this.cells = new Cell[,](width, height, (row, column) => gridCapture.cells[row, column].getCellNextTick(gridCapture));
  17. }
  18. //public [](row as usize, column as usize) as Cell => cells[row,column];
  19. public [](rows as usize .. usize, columns as usize .. usize) as Cell[]{
  20. var usedRows = rows.withBounds(0 .. height);
  21. var usedColumns = columns.withBounds(0 .. width);
  22. var rowSpan = usedRows.to - usedRows.from;
  23. var columnSpan = usedColumns.to - usedColumns.from;
  24. var cells = this.cells;
  25. return new Cell[](rowSpan * columnSpan, cellNo => cells[usedRows.from + (cellNo / columnSpan), usedColumns.from + (cellNo % columnSpan)]);
  26. }
  27. public updateDisplay(display as UpdatableGrid) as void {
  28. for row in 0 .. height {
  29. for column in 0 .. width {
  30. display[row, column] = (cells[row,column].alive ? 'X' : ' ') as char;
  31. }
  32. }
  33. }
  34. }