// Save current generation, calculate the next. public void Iterate() { int cellNeighbours; Cell[,] oldGeneration = (Cell[, ])Grid.Clone(); Cell[,] cellsToAlter = (Cell[, ])Grid.Clone(); for (int y = 0; y < _gridSizeY; y++) { for (int x = 0; x < _gridSizeX; x++) { // Check the (up to) 8 immediately surrounding cells cellNeighbours = CellHasNeighbours(oldGeneration, x, y); // Less than 2; die of loneliness, greater than 3; die of overpopulation. if ((cellNeighbours < 2 || cellNeighbours > 3) && oldGeneration[x, y].State == CELL_STATE.Alive) { cellsToAlter[x, y].State = CELL_STATE.Dead; } // Empty cell with 3; now a not-so-empty cell. if ((oldGeneration[x, y].State == CELL_STATE.Empty || oldGeneration[x, y].State == CELL_STATE.Dead) && cellNeighbours == 3) { cellsToAlter[x, y].State = CELL_STATE.Alive; } } } oldGeneration = cellsToAlter; Grid = (Cell[, ])oldGeneration.Clone(); Generations.Add(oldGeneration); // Save current generation before the next iteration // Används medans UI-delen inte är färdig. Project Settings -> Output type = Console //PrintToConsole(false); }
private static ulong DfsMax(Cell[,] labyrinth, Cell cell) { var labCopy = (Cell[, ])labyrinth.Clone(); cell.isVisited = true; if (cell == null) { return(0); } var list = new List <ulong>(); foreach (var child in cell.ChildAddresses) { if (!labCopy[child.R, child.C].isVisited && labCopy[child.R, child.C].isPassable) { list.Add(DfsMax(labCopy, labCopy[child.R, child.C])); } } ulong listMax = 0; foreach (var item in list) { if (item > listMax) { listMax = item; } } return(listMax + (ulong)cell.Value); }
private int[] BFSOLUTION(Cell[,] B) { List <List <int[]> > lists = new List <List <int[]> >(); for (int i = 0; i < 7; i++) { if (B[i, 7].Valid) { Cell[,] C = (Cell[, ])B.Clone(); bool sucess; List <int[]> lst = BFS(C, new int[] { whiteKing.X, whiteKing.Y }, new int[] { i, 7 }, out sucess); if (sucess) { lists.Add(lst); } } } if (lists.Count == 0) { return new int[] { 0, 0 } } ; var min = lists.Min(i => i.Count); int[] value = { 0, 0 }; lists.ForEach(i => { if (i.Count == min) { value = i[1]; } }); return(value); }
public void Start(int seed) { // Bookkeeping CurrentTime = 0; nextPhotonId = 0; Photons.Clear(); // Get input/output from script System.Random random = new System.Random(seed); DynValue iovalue = luaEnvironment.Call( luaGetIO, (Func <int, int>)((max) => random.Next(max)) ); input = iovalue.Tuple[0].ToObject <List <int> >(); golden = iovalue.Tuple[1].ToObject <List <int> >(); output = new List <int>(); // Save the original state of board boardBackup = board.Clone() as Cell[, ]; // Find out where input is FindCell((x, y, cell) => cell.type == CellType.INPUT, (x, y, cell) => { inputPosition = new Vector2Int(x, y); inputDirection = DirectionIdToVector(cell.param); }); //Debug.Log($"Input coordinate: ({inputX}, {inputY})"); // Generate first input Photon GeneratePhoton(input[0], inputPosition, inputDirection); }
public Map Clone() { return(new Map(ROI.Width, ROI.Height) { Cells = (Cell[, ])Cells.Clone(), ROI = ROI }); }
public RouteFinder(Cell[,] field, Point init, Point finish) { _field = (Cell[, ])field.Clone(); InitPoint = init; FinishPoint = finish; _width = _field.GetLength(0); _height = field.GetLength(1); }
internal void Transform() { Cell[,] clone = (Cell[, ])_cells.Clone(); for (int i = 0; i < GetLength(); i++) { TransformCell(GetRow(i), GetColumn(i), clone); } }
/// <summary> /// Runs the game 1 tick to get next generation (TODO: check stability and stop the game when grid and nextGeneration are the identical /// </summary> private void Tick() { Cell[,] nextGeneration = (Cell[, ])grid.Clone(); for (int i = 0; i < _rows; i++) { for (int j = 0; j < _columns; j++) { int aliveNeighbors = GetAliveNeighbors(i, j); nextGeneration[i, j].IsAlive = (aliveNeighbors == 3 || (grid[i, j].IsAlive && aliveNeighbors == 2)) ? true : false; } } grid = nextGeneration; }
public static Cell[,] Play(Cell[,] board) { Cell[,] next = (Cell[, ])board.Clone(); for (int i = 0; i < board.GetLength(0); i++) { for (int j = 0; j < board.GetLength(1); j++) { next[i, j] = new Cell { Alive = true }; } } return(next); }
public Day18() { string[] input = File.ReadAllLines("Inputs/Day18.txt").ToArray(); _bounds = new IntVec2(input[0].Length, input.Length); _current = new Cell[_bounds.X, _bounds.Y]; _next = (Cell[, ])_current.Clone(); int j = 0; foreach (string s in input) { int i = 0; foreach (char c in s) { _current[i++, j] = (Cell)c; } j++; } }
// Start is called before the first frame update void Start() { // Singleton if (instance == null) { instance = this; } else { Destroy(gameObject); } // Initialize size of map map = new Cell[width, height]; Queue <Cell> floors = new Queue <Cell>(); // Randomly disperse walls onto map for (int x = 0; x <= map.GetUpperBound(0); x++) { for (int y = 0; y <= map.GetUpperBound(1); y++) { // 50-50 chance of cell becoming a wall if (Random.Range(0, 100) < initialWallPlacementProbability) { map[x, y] = new Cell(x, y, 1); } else { map[x, y] = new Cell(x, y, 0); } } } // Run algorithm for input iterations for (int i = 0; i < iterations - 1; i++) { Cell[,] newMap = new Cell[width, height]; for (int x = 0; x <= map.GetUpperBound(0); x++) { for (int y = 0; y <= map.GetUpperBound(1); y++) { if (Cell.MooresNeighborhood(map, x, y) >= wallThreshold) { newMap[x, y] = new Cell(x, y, 1); } else { newMap[x, y] = new Cell(x, y, 0); } } } map = newMap; } // Final iteration we add all floors to queue Cell[,] finalMap = new Cell[width, height]; for (int x = 0; x <= map.GetUpperBound(0); x++) { for (int y = 0; y <= map.GetUpperBound(1); y++) { if (Cell.MooresNeighborhood(map, x, y) >= wallThreshold) { finalMap[x, y] = new Cell(x, y, 1); } else { finalMap[x, y] = new Cell(x, y, 0); floors.Enqueue(finalMap[x, y]); } } } map = finalMap; // Room Creation while (floors.Count > 0) { Cell current = floors.Dequeue(); if (current.visited == false) { Room newRoom = new Room(); CreateRoom(current, newRoom); Room.rooms.Add(newRoom); } } Room largestRoom = Room.rooms[0]; // Find largest room foreach (Room curRoom in Room.rooms) { if (curRoom.edgeCells.Count > largestRoom.edgeCells.Count) { largestRoom = curRoom; } } // If a room is not connected to the largest room, connect nearest rooms until it is // This assures connectivity to all rooms foreach (Room curRoom in Room.rooms) { while (!curRoom.connectedRooms.Contains(largestRoom)) { ConnectionInformation cellsToConnect = curRoom.FindNearestUnconnected(); List <Cell> path = AStar.Search(map, cellsToConnect.startCell, cellsToConnect.endCell); foreach (Cell cell in path) { // Use connectionSize to fill in connection cells for (int i = -connectionSize; i <= connectionSize; i++) { for (int j = -connectionSize; j <= connectionSize; j++) { int tempX = cell.x + i; int tempY = cell.y + j; if (tempX >= 0 && tempX <= map.GetUpperBound(0) && tempY >= 0 && tempY <= map.GetUpperBound(1)) { map[tempX, tempY].value = 0; } } } } curRoom.connectedRooms.UnionWith(cellsToConnect.targetRoom.connectedRooms); } } // Create list that will be used for furnishing List <Cell> randomOrder = new List <Cell>(); // Placement of walls and floors for (int x = 0; x <= map.GetUpperBound(0); x++) { for (int y = 0; y <= map.GetUpperBound(1); y++) { if (map[x, y].value == 1) { Instantiate(wall, new Vector3(x, y, 0), Quaternion.identity); } else if (map[x, y].value == 0) { Instantiate(ground, new Vector3(x, y, 0), Quaternion.identity); // Add floors to list used for furnishing randomOrder.Add(map[x, y]); } } } // Furnishing for (int i = 0; i < furnishingIterations; i++) { Shuffle(randomOrder); Cell[,] newMap = (Cell[, ])map.Clone(); foreach (Cell curCell in randomOrder) { if (map[curCell.x, curCell.y].value == 0) { foreach (GameObject obj in furniture) { Furniture curFurniture = obj.GetComponent <Furniture>(); if (curFurniture.CanSpawn(map, curCell.x, curCell.y)) { curFurniture.Spawn(curCell.x, curCell.y); newMap[curCell.x, curCell.y].value = curFurniture.myValue; break; } } } } map = newMap; } // Assure required amounts are met foreach (GameObject obj in furniture) { Furniture curFurniture = obj.GetComponent <Furniture>(); int count = 0; while (curFurniture.GetAmount() < curFurniture.requiredAmount) { if (count > maxFurnishingIterations) { Debug.LogError("Could not create level with required furniture"); Debug.Break(); break; } Shuffle(randomOrder); foreach (Cell curCell in randomOrder) { if (map[curCell.x, curCell.y].value == 0 && curFurniture.CanSpawn(map, curCell.x, curCell.y)) { curFurniture.Spawn(curCell.x, curCell.y); map[curCell.x, curCell.y].value = curFurniture.myValue; break; } } count++; } } }
public Cell[,] GetField() => field.Clone() as Cell[, ];
public Field(Cell[,] cells) { this.cells = (Cell[, ])cells.Clone(); }
private Cell[,] testGrid; // After test are ran on testGrid, setGrid is then set to testGrid public Manager(Cell[,] _inputGrid) { setGrid = (Cell[, ])_inputGrid.Clone(); }
// Use this for initialization void Start() { //tilePrefab = Resources.Load("Prefabs/Tile") as GameObject; slantNE = (Sprite)Resources.Load("Backgrounds/SlantNE", typeof(Sprite)); slantNW = (Sprite)Resources.Load("Backgrounds/SlantNW", typeof(Sprite)); slantSW = (Sprite)Resources.Load("Backgrounds/SlantSW", typeof(Sprite)); slantSE = (Sprite)Resources.Load("Backgrounds/SlantSE", typeof(Sprite)); fillRatio = Random.Range(0.44f, 0.48f); cheeseThresh = (int)Mathf.Floor(CAVE_WIDTH * CAVE_HEIGHT / 6.0f); grid = new Cell[CAVE_WIDTH, CAVE_HEIGHT]; // randomize grid for (int c = 0; c < CAVE_WIDTH; c++) { for (int r = 0; r < CAVE_HEIGHT; r++) { if (Random.value < fillRatio) { grid[c, r] = Cell.Filled; } else { grid[c, r] = Cell.Empty; } } } // generate cave level for (int i = 0; i < NUM_STEPS; i++) { GenerateCave(); } //fill borders for (var r = 0; r < CAVE_HEIGHT; r++) { grid[0, r] = (int)Cell.Filled; grid[CAVE_WIDTH - 1, r] = (int)Cell.Filled; } for (var c = 0; c < CAVE_WIDTH; c++) { grid[c, 0] = (int)Cell.Filled; grid[c, CAVE_HEIGHT - 1] = (int)Cell.Filled; } // decheese pgrid = new Cell[CAVE_WIDTH, CAVE_HEIGHT]; pgrid = (Cell[, ])grid.Clone(); DeCheeseCave(); // Find/Set Slants/Edges FindSlants(); FindEdges(); /* * for (int r = 0; r < CAVE_HEIGHT; r++) * { * for (int c = 0; c < CAVE_WIDTH; c++) * { * if (grid[c, r] == Cell.Filled) * { * grid[c, r] = Cell.Empty; * } * } * } */ Debug.Log("Cave Done"); setTextureTest(); }
public Cell[,] GetGridCopy() { return((Cell[, ])grid.Clone()); }