public String Save(Dictionary <Point, Cell> cells, GameMode mode) { StringBuilder sb = new StringBuilder(); sb.Append((int)mode); foreach (var cell in cells) { if (!cell.Value.Death) { sb.Append(cell.Value.Position.X); sb.Append(","); sb.Append(cell.Value.Position.Y); if (mode == GameMode.WireWorld) { WireCell wc = cell.Value as WireCell; if (wc != null) { sb.Append(","); sb.Append((int)wc.WireState); } } sb.Append("|"); } } return(Convert.ToBase64String(Encoding.UTF8.GetBytes(sb.ToString()))); }
public Dictionary <Point, Cell> Load(String base64Str, out GameMode mode) { Dictionary <Point, Cell> cells = new Dictionary <Point, Cell>(); mode = GameMode.GameOfLife; try { String result = Encoding.UTF8.GetString(Convert.FromBase64String(base64Str)); int intMode = 0; if (int.TryParse(result.Substring(0, 1), out intMode)) { mode = (GameMode)intMode; result = result.Substring(1, result.Length - 1); } //result = result.Replace("{", ""); var points = result.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries); foreach (var point in points) { try { String[] pointArr = point.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); Cell cell; int x = int.Parse(pointArr[0]); int y = int.Parse(pointArr[1]); if (mode == GameMode.WireWorld) { int intState = int.Parse(pointArr[2]); cell = new WireCell(cells, false) { Position = new Point(x, y), WireState = (WireState)intState, NextState = (WireState)intState }; } else { cell = new Cell(cells, false) { Position = new Point(x, y) }; } //cell.Init(); cells.Add(cell.Position, cell); } catch { } } } catch { } return(cells); }
protected void AddCellsWireWorld(Point mouse, bool isClick, MouseButtons mouseButtons) { Point p = CalculateMouseRoot(mouse); if (true) { if (_cells.ContainsKey(p)) { if (_cells[p].Death) { if (!_drawMode) { _cells[p].Death = false; //_cells[p].Init(); } } else { if (mouseButtons == MouseButtons.Left) { if (!_drawMode) { _cells.Remove(p); OnCellsUpdated(); } } if (mouseButtons == MouseButtons.Right) { var c = _cells[p] as WireCell; if (c != null) { if (c.WireState == WireState.Head) { c.WireState = WireState.Tail; } else if (c.WireState == WireState.Tail || c.WireState == WireState.Wire) { c.WireState = WireState.Head; } } OnCellsUpdated(); } } } else { if (_drawMode) { if (mouseButtons == MouseButtons.Left) { _cells[p] = new WireCell(_cells) { Position = p }; } if (mouseButtons == MouseButtons.Right) { _cells[p] = new WireCell(_cells) { Position = p, WireState = WireState.Head }; } OnCellsUpdated(); //_cells[p].Init(); } } } _lastPoint = p; }