private void SaveRegularGrid(Grid grid, FileStream fs, StringBuilder bits) { fs.WriteByte(0); #if LOG logger.AppendLine("Grid type : regular = 0"); #endif WriteGridProperties(grid, fs); bits.Append(grid.Cells[0].Alive ? '1' : '0'); for (int i = 1; i < grid.Cells.Length; i++) { bits.Append(grid.Cells[i].Alive ? '1' : '0'); if (i % sizeof(long) == 0) { string binary = bits.ToString().Reverse(); byte octopus = Convert.ToByte(binary, 2); fs.WriteByte(octopus); #if LOG logger.AppendLine("Bits { " + binary + " } = " + octopus); #endif bits.Clear(); } } }
private void SaveHexagonalGrid(Grid grid, FileStream fs, StringBuilder bits) { fs.WriteByte(1); #if LOG logger.AppendLine("Grid type : hexagonal = 1"); #endif HexagonalGrid hexa = (HexagonalGrid)grid; WriteGridProperties(hexa, fs); bits.Append(hexa.Cells[0].Alive ? '1' : '0'); for (int i = 1; i < hexa.Cells.Length; i++) { bits.Append(hexa.Cells[i].Alive ? '1' : '0'); if (i % sizeof(long) == 0) { string binary = bits.ToString().Reverse(); byte octopus = Convert.ToByte(binary, 2); fs.WriteByte(octopus); #if LOG logger.AppendLine("Bits { " + binary + " } = " + octopus); #endif bits.Clear(); } } }
public void SaveGrid(Grid grid, string to) { try { #if LOG logPath = Path.Combine(to.Replace(Path.GetFileName(to), ""), "log.txt"); #endif using (FileStream fs = File.Create(to)) { StringBuilder bits = new StringBuilder(); if (grid is HexagonalGrid) SaveHexagonalGrid(grid, fs, bits); else SaveRegularGrid(grid, fs, bits); #if LOG File.WriteAllText(logPath, logger.ToString()); #endif } } catch (Exception e) { throw e; } }
public Cell(Point position, bool alive, Grid parent = null) { this.parent = parent; this.alive = alive; this.position = new Point(position.X * CELL_SIZE, position.Y * CELL_SIZE); this.aliveColor = PickRandomColor(); DetermineColorFromLifeState(); }
public Cell(Cell cell) { this.parent = cell.parent; this.alive = cell.alive; this.position = cell.position; this.color = cell.color; this.aliveColor = cell.aliveColor; }
public Cell(Grid parent) { this.parent = parent; this.alive = false; this.position = new Point(); this.aliveColor = PickRandomColor(); DetermineColorFromLifeState(); }
private void glf_LoadingFinished(object sender, EventArgs e) { GridEventArgs gea = (GridEventArgs)e; grid = gea.Grid; InitializeLifeGame(hasGrid: true); nud_GridWidth.Value = (decimal)grid.Width; nud_GridHeight.Value = (decimal)grid.Height; chk_UseHexagonalGrid.Checked = grid is HexagonalGrid; RefreshUI(); DrawGrid(); }
private void CreateGrid() { Point gridSize = new Point((int)nud_GridWidth.Value, (int)nud_GridHeight.Value); if (!chk_UseHexagonalGrid.Checked) grid = new Grid(gridSize); else grid = new HexagonalGrid(new Vector2D(gridSize.X, gridSize.Y)); }
public GridEventArgs(Grid grid) { this.Grid = grid; }
private static void SetCellsParent(Grid grid) { foreach (Cell cell in grid.Cells) cell.Parent = grid; }
private Grid LoadRegularGrid(FileStream fs) { int width = fs.ReadByte(); int height = fs.ReadByte(); int sequenceLength = width * height / sizeof(long); int gridArea = width * height; int iteration = ReadGridIteration(fs); #if LOG logger.AppendLine("width : " + width); logger.AppendLine("height : " + height); logger.AppendLine("seq. length = " + sequenceLength); logger.AppendLine("area : " + gridArea); logger.AppendLine("iteration : " + iteration); #endif List<int> cellsLifeStates = GetCellsLifeState(fs, sequenceLength); #if LOG File.WriteAllText("log.txt", logger.ToString()); #endif List<Cell> cells = CreateCells(width, gridArea, cellsLifeStates); Grid grid = new Grid(new Point(width, height), cells.ToArray(), iteration); SetCellsParent(grid); return grid; }
private void WriteGridProperties(Grid grid, FileStream fs) { fs.WriteByte((byte)grid.Width); fs.WriteByte((byte)grid.Height); #if LOG logger.AppendLine("Width = " + grid.Width); logger.AppendLine("Height = " + grid.Height); #endif WriteGridIteration(grid.Iteration, fs); }
private void RaiseLoadFinishedEvent(Grid buffer) { if (LoadingFinished != null) LoadingFinished(this, new GridEventArgs(buffer)); }