public void AddResource(Resource resource, int amount, IslandInfo island) { ISellable existingResource = null; resource.Amount -= amount; int containsIndex = entityData.Inventory.FindIndex(r => r.Name == resource.Name); if (containsIndex != -1) { existingResource = entityData.Inventory.Where(r => r.Name == resource.Name).First(); } //Resource already in list if (existingResource != null) { existingResource.Amount += amount; } //Resource is not in list else { Resource newResource = new Resource(resource); newResource.Amount = amount; entityData.Inventory.Add(newResource); } }
public void FindEdgeWater(IslandInfo iland, int[][] grid) { foreach (var item in iland.GroundList) { if (iland.GroundList.Where(p => p.x == item.x && p.y == item.y - 1).ToList().Count == 0) { iland.WaterEdgeList.Add(new IslandSqurePoint(item.y - 1, item.x)); } if (iland.GroundList.Where(p => p.x == item.x && p.y == item.y + 1).ToList().Count == 0) { iland.WaterEdgeList.Add(new IslandSqurePoint(item.y + 1, item.x)); } if (iland.GroundList.Where(p => p.x == item.x - 1 && p.y == item.y).ToList().Count == 0) { iland.WaterEdgeList.Add(new IslandSqurePoint(item.y, item.x - 1)); } if (iland.GroundList.Where(p => p.x == item.x + 1 && p.y == item.y).ToList().Count == 0) { iland.WaterEdgeList.Add(new IslandSqurePoint(item.y, item.x + 1)); } } }
public int LargestIsland(int[][] grid) { //解题思路:先找出岛屿的个数,再找出岛屿边缘水域,再找岛屿之间的边缘水域是否有重合,有的话即能链接在一起 //目前性能不好,有时候报超时,有时候能通过,待优化 int maxAra = 0; int gridSize = grid.Length * grid[0].Length; for (int y = 0; y < grid.Length; y++) { for (int x = 0; x < grid[y].Length; x++) { if (grid[y][x] == 1) { IslandInfo ii = new IslandInfo(); TraceIsland(grid, y, x, ii); FindEdgeWater(ii, grid); if (ii.IslandSize > maxAra) { maxAra = ii.IslandSize; } islandList.Add(ii); } } } if (maxAra == gridSize) { return(gridSize); } else { maxAra += 1; } for (int y = 0; y < grid.Length; y++) { for (int x = 0; x < grid[y].Length; x++) { if (grid[y][x] == 0) { int totalArea = 0; foreach (var item in islandList) { if (item.WaterEdgeList.Where(p => p.x == x && p.y == y).ToList().Count >= 1) { totalArea += item.IslandSize; } } totalArea += 1; if (totalArea > maxAra) { maxAra = totalArea; } } } } return(maxAra); }
private void Awake() { islandUI = GetComponent <IslandUI>(); col = GetComponent <BoxCollider>(); meshObjects = new List <GameObject>(); generator = GetComponent <IslandGenerator>(); UniqueIslandData = new UniqueIslandData(); info = new IslandInfo(); UniqueIslandData.Initialize(); }
public void GatherResources(CrewMember crewMember, IslandInfo island) { //TODO replace hardcoded number with one dependent on crew member's skill for (int i = 0; i < 3; i++) { int resourceIndex = Random.Range(0, island.Resources.Count); if (island.Resources.Count > 0) { Resource resource = island.Resources[resourceIndex]; int amount = Random.Range(1, resource.Amount + 1); AddResource(resource, amount, island); } } }
IslandInfo GetShapeIslandCount(Vector2 startPos, BoardLogic board) { IslandInfo info = new IslandInfo(); //NOTE: This is since shape can be blown apart we want to still be able to move a block on their //own island. So the count isn't the shapeCount but only a portion in this count. So we first search //how big the island is and match against that. bool[] boardArray = new bool[board.boardWidth * board.boardHeight]; VisitedQueue sentinel = new VisitedQueue(); sentinel.next = sentinel.prev = sentinel; addToQueryList(sentinel, startPos, boardArray, board); VisitedQueue queryAt = sentinel.next; Assert.IsTrue(queryAt != sentinel); while (queryAt != sentinel) { Vector2 pos = queryAt.pos; Assert.IsTrue(info.count < info.poses.Length); info.poses[info.count++] = pos; addToQueryList(sentinel, new Vector2(1, 0) + pos, boardArray, board); addToQueryList(sentinel, new Vector2(-1, 0) + pos, boardArray, board); addToQueryList(sentinel, new Vector2(0, 1) + pos, boardArray, board); addToQueryList(sentinel, new Vector2(0, -1) + pos, boardArray, board); queryAt = queryAt.next; } if (info.count > this.count) { //printf("Count At; %d\n", info.count); //printf("Shape Count: %d\n", shape->count); } Assert.IsTrue(info.count <= this.count); return(info); }
public void TraceIsland(int[][] grid, int y, int x, IslandInfo ilandInfo) { if (y < 0 || x < 0 || y >= grid.Length || x >= grid[y].Length) { return; } if (grid[y][x] == 1) { grid[y][x] = 2; ilandInfo.IslandSize++; ilandInfo.GroundList.Add(new IslandSqurePoint(y, x)); TraceIsland(grid, y + 1, x, ilandInfo); TraceIsland(grid, y - 1, x, ilandInfo); TraceIsland(grid, y, x + 1, ilandInfo); TraceIsland(grid, y, x - 1, ilandInfo); } else { return; } }
public bool shapeStillConnected(int currentHotIndex, Vector2 boardPosAt, BoardLogic board) { bool result = true; for (int i = 0; i < this.count; ++i) { Vector2 pos = this.blocks[i].pos; if ((int)boardPosAt.x == (int)pos.x && (int)boardPosAt.y == (int)pos.y) { result = false; break; } BoardState state = board.GetBoardState(boardPosAt); if (state != BoardState.BOARD_NULL) { result = false; break; } } if (result) { Vector2 oldPos = this.blocks[currentHotIndex].pos; BoardValue oldVal = board.GetBoardValue(oldPos); Assert.IsTrue(oldVal.state == BoardState.BOARD_SHAPE); IslandInfo mainIslandInfo = this.GetShapeIslandCount(oldPos, board); Assert.IsTrue(mainIslandInfo.count >= 1); if (mainIslandInfo.count <= 1) { //There is an isolated block result = false; } else { Vector2 idPos = mainIslandInfo.poses[1]; //won't be that starting pos since the first position will dissapear if it is correct. //temporaialy set the board state to where the shape was to be null, so this can't act as a bridge in the flood fill oldVal.state = BoardState.BOARD_NULL; //set where the board will be to a valid position BoardValue newVal = board.GetBoardValue(boardPosAt); Assert.IsTrue(newVal.state == BoardState.BOARD_NULL); newVal.state = BoardState.BOARD_SHAPE; //// This code isn't needed anymore. Just used for the assert below. IslandInfo islandInfo = this.GetShapeIslandCount(boardPosAt, board); //See if the new pos is part of the same island bool found = false; for (int index = 0; index < islandInfo.count; ++index) { Vector2 srchPos = islandInfo.poses[index]; if ((int)srchPos.x == (int)idPos.x && (int)srchPos.y == (int)idPos.y) { found = true; break; } } //// IslandInfo mainIslandInfo_after = this.GetShapeIslandCount(idPos, board); if (mainIslandInfo_after.count < mainIslandInfo.count) { result = false; } else { Assert.IsTrue(found); } //set the state back to being a shape. newVal.state = BoardState.BOARD_NULL; oldVal.state = BoardState.BOARD_SHAPE; } } return(result); }