public static bool CheckIfDrowned(int index, Dictionary <int, Tile> tiles) { var indexList = tiles.Select(t => t.Key).ToList(); var oppositeTiles = LocateShipTool.AreOppositeTilesAvailable(indexList, index); var tilesCopy = tiles.ToDictionary(t => t.Key, t => new Tile { Type = t.Value.Type }); tilesCopy[index].Type = TileType.water; foreach (var item in oppositeTiles) { var trueIndex = indexList[item.Key]; if (item.Value && tilesCopy.ContainsKey(trueIndex)) { if (tilesCopy[trueIndex].Type == TileType.ship) { return(false); } else if (tilesCopy[trueIndex].Type == TileType.shot && !CheckIfDrowned(item.Key, tilesCopy)) { return(false); } } } return(true); }
public static void MarkAnavailableDiagonalTiles(DbContext database, int index, Dictionary <int, Tile> tiles) { var diagonalAvailability = LocateShipTool.AreDiagonalTilesAvailable(tiles.Select(t => t.Key).ToList(), index); foreach (var item in diagonalAvailability) { if (item.Value && tiles[item.Key].Type == TileType.water) { tiles[item.Key].Type = TileType.missed; database.Entry(tiles[item.Key]).State = EntityState.Added; } } }
public static void AddOppositeDrownedTiles(DbContext database, List <GameEventDTO> gameEventDTOList, Dictionary <int, Tile> tiles, int index, ShotAt shotAt, int gameNumber) { var indexList = tiles.Select(t => t.Key).ToList(); var oppositeTiles = LocateShipTool.AreOppositeTilesAvailable(indexList, index); tiles[index].Type = TileType.drowned; database.Entry(tiles[index]).State = EntityState.Modified; foreach (var item in oppositeTiles) { //find already shot tile next to drowned tile if (item.Value && tiles.ContainsKey(item.Key) && tiles[item.Key].Type == TileType.shot) { //change shot tile to drowned tile and add to list of events GameEventDTO oppositeGameEventDTO = new GameEventDTO(); oppositeGameEventDTO.IsVisible = false; oppositeGameEventDTO.GameNumber = gameNumber; oppositeGameEventDTO.Index = item.Key; oppositeGameEventDTO.Tile = TileType.drowned; gameEventDTOList.Add(oppositeGameEventDTO); //if there are any water tiles around mark them as missed for smarter AI shots if (shotAt == ShotAt.playerTile) { var tilesToMark = LocateShipTool.AreOppositeTilesAvailable(indexList, item.Key); foreach (var tile in tilesToMark) { if (tile.Value && tiles[tile.Key].Type == TileType.water) { tiles[tile.Key].Type = TileType.missed; database.Entry(tiles[tile.Key]).State = EntityState.Added; } } } AddOppositeDrownedTiles(database, gameEventDTOList, tiles, oppositeGameEventDTO.Index, shotAt, gameNumber); } } //if it is one-tile ship mark water tiles around as missed for smarter AI shots if (!tiles.Any(t => t.Value.Type == TileType.shot && oppositeTiles.Any(o => o.Key == t.Key && o.Value))) { foreach (var tile in oppositeTiles) { if (tile.Value && tiles[tile.Key].Type == TileType.water) { tiles[tile.Key].Type = TileType.missed; database.Entry(tiles[tile.Key]).State = EntityState.Added; } } } }