public CubeChunk GetAdjacentChunk(CubeChunk chunk, AdjacentDirection direction) { CubeCoordinate adjChunkCoordinate = chunk.ChunkCoordinate.GetAdjacentCoordinate(direction); if (chunks.ContainsKey(adjChunkCoordinate.ToString())) { return(chunks[adjChunkCoordinate.ToString()]); } else { return(null); } }
public void ConnectNeighbors() { for (int i = 0; i < 6; i++) { if (!NeighborChunks[i]) { AdjacentDirection chunkDir = (AdjacentDirection)i; CubeChunk neighbor = grid.GetAdjacentChunk(this, chunkDir); if (neighbor) { NeighborChunks[i] = neighbor; neighbor.NeighborChunks[(int)chunkDir.ChunkOpposite()] = this; } } } }
private bool TryReverseLine(Stone stone, AdjacentDirection direction) { if (!TryGetAdjacentStone(stone, direction, out var adjacentStone)) { return(false); } if (adjacentStone.IsEmpty) { return(false); } void UpdateReversedStoneCount() { switch (stone.CurrentColor) { case StoneColor.Black: _blackCount++; _whiteCount--; break; case StoneColor.White: _blackCount--; _whiteCount++; break; } } if (stone.CurrentColor != adjacentStone.CurrentColor) { if (!stone.TryReverse()) { return(false); } UpdateReversedStoneCount(); return(true); } if (!TryReverseLine(adjacentStone, direction) || !stone.TryReverse()) { return(false); } UpdateReversedStoneCount(); return(true); }
public Point TranslatePoint(AdjacentDirection direction) { switch (direction) { case AdjacentDirection.Down: return(new Point(X, Y - 1)); case AdjacentDirection.Left: return(new Point(X - 1, Y)); case AdjacentDirection.Up: return(new Point(X, Y + 1)); case AdjacentDirection.Right: return(new Point(X + 1, Y)); } throw new Exception("Uh oh"); }
private AdjacentDirection RotateDirectionCounterClockwise(AdjacentDirection direction) { switch (direction) { case AdjacentDirection.Down: return(AdjacentDirection.Right); case AdjacentDirection.Left: return(AdjacentDirection.Down); case AdjacentDirection.Up: return(AdjacentDirection.Left); case AdjacentDirection.Right: return(AdjacentDirection.Up); } throw new Exception("Uh oh"); }
public CubeCoordinate GetAdjacentCoordinate(AdjacentDirection dir) { switch (dir) { case AdjacentDirection.front: return(new CubeCoordinate(x, y, z + 1)); case AdjacentDirection.back: return(new CubeCoordinate(x, y, z - 1)); case AdjacentDirection.left: return(new CubeCoordinate(x - 1, y, z)); case AdjacentDirection.right: return(new CubeCoordinate(x + 1, y, z)); case AdjacentDirection.up: return(new CubeCoordinate(x, y + 1, z)); case AdjacentDirection.down: return(new CubeCoordinate(x, y - 1, z)); default: return(this); } }
private bool TryGetAdjacentStone(Stone currentStone, AdjacentDirection adjacentDirection, out Stone adjacentStone) { adjacentStone = null; switch (currentStone.Position.raw) { // 一番上と一番下のケース case 0 when adjacentDirection == AdjacentDirection.UpperLeft || adjacentDirection == AdjacentDirection.Up || adjacentDirection == AdjacentDirection.UpperRight: case width - 1 when adjacentDirection == AdjacentDirection.LowerLeft || adjacentDirection == AdjacentDirection.Lower || adjacentDirection == AdjacentDirection.LowerRight: return(false); } switch (currentStone.Position.column) { // 一番左と一番右のケース case 0 when adjacentDirection == AdjacentDirection.Left || adjacentDirection == AdjacentDirection.LowerLeft || adjacentDirection == AdjacentDirection.UpperLeft: case width - 1 when adjacentDirection == AdjacentDirection.Right || adjacentDirection == AdjacentDirection.LowerRight || adjacentDirection == AdjacentDirection.UpperRight: return(false); } adjacentStone = adjacentDirection switch { AdjacentDirection.UpperLeft => _stones[currentStone.Position.raw - 1, currentStone.Position.column - 1], AdjacentDirection.Up => _stones[currentStone.Position.raw - 1, currentStone.Position.column], AdjacentDirection.UpperRight => _stones[currentStone.Position.raw - 1, currentStone.Position.column + 1], AdjacentDirection.Left => _stones[currentStone.Position.raw, currentStone.Position.column - 1], AdjacentDirection.Right => _stones[currentStone.Position.raw, currentStone.Position.column + 1], AdjacentDirection.LowerLeft => _stones[currentStone.Position.raw + 1, currentStone.Position.column - 1], AdjacentDirection.Lower => _stones[currentStone.Position.raw + 1, currentStone.Position.column], AdjacentDirection.LowerRight => _stones[currentStone.Position.raw + 1, currentStone.Position.column + 1], }; return(true); }
bool GetAdjacentPath(CubeCoordinate coordinate, CubeOrientate orientate, out CubeCoordinate result) { AdjacentDirection temp = AdjacentDirection.front; switch (orientate) { case CubeOrientate.back: temp = AdjacentDirection.back; break; case CubeOrientate.left: temp = AdjacentDirection.left; break; case CubeOrientate.right: temp = AdjacentDirection.right; break; } if (HasCube(coordinate.GetAdjacentCoordinate(temp, AdjacentDirection.up, AdjacentDirection.up))) { result = coordinate.GetAdjacentCoordinate(temp, AdjacentDirection.up, AdjacentDirection.up); return(false); } if (HasCube(coordinate.GetAdjacentCoordinate(temp, AdjacentDirection.up))) { result = coordinate.GetAdjacentCoordinate(temp, AdjacentDirection.up); return(true); } if (HasCube(coordinate.GetAdjacentCoordinate(temp))) { result = coordinate.GetAdjacentCoordinate(temp); return(true); } if (HasCube(coordinate.GetAdjacentCoordinate(temp, AdjacentDirection.down))) { result = coordinate.GetAdjacentCoordinate(temp, AdjacentDirection.down); return(true); } result = new CubeCoordinate(0, 0, 0); return(false); }
public PositionalMemoryCell GetCellAtIndex(int index) { // If we've already expanded the spiral past the input index, early out if (_indexToCellMap.ContainsKey(index)) { return(_indexToCellMap[index]); } while (nextIndex <= index) { int newValue = 0; // Explore neighbourhood of the new cell and sum the adjacent values for (int x = -1; x < 2; x++) { for (int y = -1; y < 2; y++) { if (x == 0 && y == 0) { continue; } Point adjacentPoint = new Point(_currentPoint.X + x, _currentPoint.Y + y); if (_pointToCellMap.ContainsKey(adjacentPoint)) { newValue += _pointToCellMap[adjacentPoint].Value; } } } // Base case where we have no filled in squares yet. Origin is initialized to 1. if (newValue == 0) { newValue = 1; } // Create the next cell in the spiral var newCell = new PositionalMemoryCell(_currentPoint, nextIndex, newValue); _pointToCellMap.Add(_currentPoint, newCell); _indexToCellMap.Add(nextIndex, newCell); // Translate point AdjacentDirection lookLeftForEdgeOfSpiralDirection = RotateDirectionCounterClockwise(_currentSearchDirection); Point lookLeftForEdgeOfSpiralPoint = _currentPoint.TranslatePoint(lookLeftForEdgeOfSpiralDirection); if (_pointToCellMap.ContainsKey(lookLeftForEdgeOfSpiralPoint)) { // Continue expanding in the current direction _currentPoint = _currentPoint.TranslatePoint(_currentSearchDirection); } else { // Turn left after passing the inner spiral edge to create the counter-clockwise // expansion. _currentSearchDirection = lookLeftForEdgeOfSpiralDirection; _currentPoint = lookLeftForEdgeOfSpiralPoint; } nextIndex++; } return(_indexToCellMap[index]); }
public static AdjacentDirection ChunkOpposite(this AdjacentDirection direction) { return((int)direction % 2 == 0 ? (AdjacentDirection)((int)direction + 1) : (AdjacentDirection)((int)direction - 1)); }