示例#1
0
文件: Grid.cs 项目: jawbrey/uLife
        public void AddCell(ulong x, ulong y, bool state)
        {
            // outside grid, return
            // TODO: throw ex
            if (x >= Rows || y >= Cols) return;

            Tuple<ulong, ulong> loc = new Tuple<ulong, ulong>(x, y);

            if (!Cells.ContainsKey(loc))
            {
                Cell cell = new Cell(x, y);
                cell.State = state;
                Cells.Add(loc, cell);
            }
        }
示例#2
0
文件: Grid.cs 项目: jawbrey/uLife
        private Cell GetNeighbor(Compass dir, ulong x, ulong y)
        {
            ulong row = x;
            ulong col = y;

            // TODO: handle overflow, underflow, and off grid

            switch (dir)
            {
                case Compass.North:
                    if (row >= Rows || row == ulong.MaxValue) return null;
                    row++;
                    break;
                case Compass.Northeast:
                    if (row >= Rows || row == ulong.MaxValue || col >= Cols || col == ulong.MaxValue) return null;
                    row++;
                    col++;
                    break;
                case Compass.East:
                    if (col >= Cols || col == ulong.MaxValue) return null;
                    col++;
                    break;
                case Compass.Southeast:
                    if (row == 0 || col >= Cols || col == ulong.MaxValue) return null;
                    row--;
                    col++;
                    break;
                case Compass.South:
                    if (row == 0) return null;
                    row--;
                    break;
                case Compass.Southwest:
                    if (row == 0 || col == 0) return null;
                    row--;
                    col--;
                    break;
                case Compass.West:
                    if (col == 0) return null;
                    col--;
                    break;
                case Compass.Northwest:
                    if (row >= Rows || row == ulong.MaxValue || col == 0) return null;
                    row++;
                    col--;
                    break;
            }

            Cell cell = GetCell(row, col);

            if (cell == null) cell = new Cell(row, col);

            return cell;
        }