コード例 #1
0
        public void CheckIsRoadClosed(BoardComponent bc)
        {
            if (bc.TerrainType != TerrainType.Road)
            {
                return;
            }

            // road is closed when it has two components with only one terrain (road end)
            var groupRoadsWithOneEnd = bc.Components
                                       .Select(c => c.TileTypeComponent.Terrains.Count())
                                       .GroupBy(count => count)
                                       .FirstOrDefault(w => w.Key == 1);

            if (groupRoadsWithOneEnd == null)
            {
                return;
            }

            var countOfComponentsWithRoadEnd = groupRoadsWithOneEnd.Count();

            if (countOfComponentsWithRoadEnd > 1)
            {
                bc.Points = bc.Components.Count(); // should be tiles count
            }
        }
コード例 #2
0
        public static bool ValidateTurn(int col, int row, BoardComponent oponentBoardComponent)
        {
            var cellState = oponentBoardComponent.Board.GetCellState(col, row);

            //rules (now only one):
            return(cellState == CellState.Sea || cellState == CellState.Ship);
        }
コード例 #3
0
        private BoardComponent MergeBoardComponents(BoardComponent bcTile, BoardComponent bcNeighbour)
        {
            if (bcTile.BoardComponentId == bcNeighbour.BoardComponentId)
            {
                // Components are alredy connected.
                return(bcNeighbour);
            }

            var tcs = new List <TileComponent>();

            tcs.AddRange(bcTile.Components);
            tcs.AddRange(bcNeighbour.Components);

            // TODO: - don't create new bc - use one of the two components instead
            var merged = new BoardComponent
            {
                BoardId     = bcNeighbour.BoardId,
                TerrainType = bcNeighbour.TerrainType,
                Components  = tcs,
                IsOpen      = true,
                Points      = 0,
            };

            _boardComponentRepository.Add(merged);
            _boardComponentRepository.Remove(bcTile);
            _boardComponentRepository.Remove(bcNeighbour);

            return(merged);
        }
コード例 #4
0
            private void RemoveThinWalls(BoardComponent board, NativeArray <TileType> tiles, int tilesStride)
            {
                var inconsistentTileDetected = true;

                while (inconsistentTileDetected)
                {
                    inconsistentTileDetected = false;
                    for (var y = 1; y < board.Size.y - 1; y++)
                    {
                        for (var x = 1; x < board.Size.x - 1; x++)
                        {
                            if (tiles[(y * tilesStride) + x] == TileType.Wall &&
                                ((tiles[(y * tilesStride) + x + 1] == TileType.Floor && tiles[(y * tilesStride) + x - 1] == TileType.Floor) || // pattern -> -
                                 (tiles[((y + 1) * tilesStride) + x] == TileType.Floor && tiles[((y - 1) * tilesStride) + x] == TileType.Floor) ||    // pattern -> |
                                 ((tiles[((y + 1) * tilesStride) + x + 1] == TileType.Floor && tiles[((y - 1) * tilesStride) + x - 1] == TileType.Floor) &&   // pattern -> /
                                  (tiles[((y + 1) * tilesStride) + x - 1] == TileType.Wall && tiles[((y - 1) * tilesStride) + x + 1] == TileType.Wall)) ||      // pattern -> \
                                 ((tiles[((y + 1) * tilesStride) + x + 1] == TileType.Wall && tiles[((y - 1) * tilesStride) + x - 1] == TileType.Wall) &&   // pattern -> /
                                  (tiles[((y + 1) * tilesStride) + x - 1] == TileType.Floor && tiles[((y - 1) * tilesStride) + x + 1] == TileType.Floor)) ||       // pattern -> \
                                 ((tiles[((y + 1) * tilesStride) + x + 1] == TileType.Wall) && (tiles[((y - 1) * tilesStride) + x - 1] == TileType.Wall) &&
                                  tiles[((y + 1) * tilesStride) + x] == TileType.Floor && tiles[(y * tilesStride) + x + 1] == TileType.Floor) ||
                                 ((tiles[((y + 1) * tilesStride) + x - 1] == TileType.Wall) && (tiles[((y - 1) * tilesStride) + x + 1] == TileType.Wall) &&
                                  tiles[((y + 1) * tilesStride) + x] == TileType.Floor && tiles[(y * tilesStride) + x - 1] == TileType.Floor)
                                ))
                            {
                                var currentTile = tiles[(y * tilesStride) + x];
                                currentTile = TileType.Floor;
                                tiles[(y * tilesStride) + x] = currentTile;
                                inconsistentTileDetected     = true;
                            }
                        }
                    }
                }
            }
コード例 #5
0
        private void CreateMeshes()
        {
            for (int x = 0; x <= boardSettings.horizontalCutAmount; x++)
            {
                for (int y = 0; y <= boardSettings.verticalCutAmount; y++)
                {
                    if (gridSettings.grid[x, y].value == 0)
                    {
                        AddCube(wall, x, y, 0f);
                    }
                }
            }

            gridCurrent.Populate(gridSettings);
            gridCurrent.Shuffle(gridSettings);

            components = new BoardComponent[gridCurrent.components.Count];
            foreach (var c in gridCurrent.components)
            {
                int i = c.Key;
                if (i == 0)
                {
                    continue;
                }

                components[i - 1] = new BoardComponent("Component " + i, GetVertexPosition(c.Value[0].x, c.Value[0].y, 0f));
                foreach (var v in c.Value)
                {
                    AddCube(components[i - 1], v.x, v.y, 0f);
                }
            }
        }
コード例 #6
0
 private void InitializeCustomComponent()
 {
     _rules         = new Rules();
     _about         = new AboutBox();
     _options       = new Options(Scrabble);
     _scrabbleBoard = new BoardComponent(Scrabble)
     {
         Location = new Point(ClientSize.Width - (Common.BoardWidthPx + Common.BoardPaddingPx), Height - (Common.BoardHeightPx + Common.BoardPaddingPx + Menu.Bottom)),
         Name     = @"Scabble Board",
         Size     = new Size(Common.BoardWidthPx, Common.BoardHeightPx),
         TabIndex = 1
     };
     Controls.Add(_scrabbleBoard);
     HighScoreName.Text  = Scrabble.Settings.HighScoreName;
     HighScoreValue.Text = Scrabble.Settings.HighScoreValue.ToString();
     _windowState        = new FormState();
     if (Scrabble.Settings.Fullscreen)
     {
         _windowState.Maximize(this);
     }
     else
     {
         _windowState.Restore(this);
     }
 }
コード例 #7
0
ファイル: Virus.cs プロジェクト: thunder033/CPURTD
 public bool Attack(BoardComponent node)
 {
     //Weapon weapon = GetComponent<Weapon>();
     //weapon.Aim(node);
     //weapon.Fire();
     node.GetComponent <Combatant>().TakeDamage(GetComponent <DamageEffect>());
     GetComponent <Combatant>().Die();
     return(true);
 }
        protected override void Execute(List <GameEntity> entities)
        {
            board     = contexts.game.board;
            boardSize = contexts.config.gameConfig.value.boardSize;
            List <GameEntity> bubblesToCheck = new List <GameEntity>();

            MarkTopRowAsConnectedToTop(bubblesToCheck);
            MarkAllBubblesConnectedToTop(bubblesToCheck);
            DeleteHangingClusters();

            contexts.game.isBoardCleaningHangingClusters = false;
        }
コード例 #9
0
 private static void SunkShipsIfDestroyed(BoardComponent oponentBoardComponent, ICollection <Ship> oponentShips)
 {
     foreach (var oponentShip in oponentShips)
     {
         if (oponentShip.IsDestroyed)
         {
             foreach (var segment in oponentShip.Segments)
             {
                 oponentBoardComponent.Board.SetCellState(segment.X, segment.Y, CellState.Sunk);
                 DiscoverSurroundingCells(segment, oponentBoardComponent);
             }
         }
     }
 }
コード例 #10
0
        private static RoomComponent CreateRoom(BoardComponent board, ref Unity.Mathematics.Random random)
        {
            var roomWidth  = random.NextInt(3, board.MaxRoomSize);
            var roomHeight = random.NextInt(3, board.MaxRoomSize);

            var centerX = (int)math.round(board.Size.x / 2f - roomWidth / 2f);
            var centerY = (int)math.round(board.Size.y / 2f - roomHeight / 2f);

            return(new RoomComponent
            {
                X = centerX,
                Y = centerY,
                Size = new int2(roomWidth, roomHeight),
                EnteringCorridor = Direction.Up
            });
        }
コード例 #11
0
        private CorridorComponent CreateCorridor(RoomComponent room, BoardComponent board, ref Unity.Mathematics.Random random, int forceDirection)
        {
            var direction = (Direction)forceDirection;

            var corridorLength = random.NextInt(board.MinCorridorLength, board.MaxCorridorLength);
            var corridorX      = 0;
            var corridorY      = 0;

            var maxLength = board.MaxCorridorLength;

            switch (direction)
            {
            case Direction.Up:
                corridorX = random.NextInt(room.X, room.X + room.Size.x);
                corridorY = room.Y + room.Size.y - 2;
                maxLength = board.Size.y - 2 - corridorY;
                break;

            case Direction.Left:
                corridorX = room.X + room.Size.x - 2;
                corridorY = random.NextInt(room.Y, room.Y + room.Size.y);
                maxLength = board.Size.x - 2 - corridorX;
                break;

            case Direction.Down:
                corridorX = random.NextInt(room.X, room.X + room.Size.x + 1);
                corridorY = room.Y;
                maxLength = corridorY - 2;
                break;

            case Direction.Right:
                corridorX = room.X;
                corridorY = random.NextInt(room.Y, room.Y + room.Size.y + 1);
                maxLength = corridorX - 2;
                break;
            }

            corridorLength = math.clamp(corridorLength, 0, maxLength);

            return(new CorridorComponent
            {
                StartX = corridorX,
                StartY = corridorY,
                Length = corridorLength,
                Direction = direction
            });
        }
コード例 #12
0
            private RoomComponent CreateRoom(BoardComponent board, CorridorComponent corridor, ref Unity.Mathematics.Random random)
            {
                var roomWidth  = random.NextInt(3, board.MaxRoomSize);
                var roomHeight = random.NextInt(3, board.MaxRoomSize);

                var roomX = 0;
                var roomY = 0;

                switch (corridor.Direction)
                {
                case Direction.Up:
                    roomHeight = math.clamp(roomHeight, 0, board.Size.y - 2 - corridor.EndY);
                    roomY      = corridor.EndY;
                    roomX      = random.NextInt(corridor.EndX - roomWidth + 1, corridor.EndX + 1);
                    roomX      = math.clamp(roomX, 2, board.Size.x - 2 - roomWidth);
                    break;

                case Direction.Left:
                    roomWidth = math.clamp(roomWidth, 0, board.Size.x - 2 - corridor.EndX);
                    roomX     = corridor.EndX;
                    roomY     = random.NextInt(corridor.EndY - roomHeight + 1, corridor.EndY + 1);
                    roomY     = math.clamp(roomY, 2, board.Size.y - 2 - roomHeight);
                    break;

                case Direction.Down:
                    roomHeight = math.clamp(roomHeight, 0, corridor.EndY);
                    roomY      = corridor.EndY - roomHeight + 1;
                    roomX      = random.NextInt(corridor.EndX - roomWidth + 1, corridor.EndX + 1);
                    roomX      = math.clamp(roomX, 2, board.Size.x - 2 - roomWidth);
                    break;

                case Direction.Right:
                    roomWidth = math.clamp(roomWidth, 0, corridor.EndX);
                    roomX     = corridor.EndX - roomWidth + 1;
                    roomY     = random.NextInt(corridor.EndY - roomHeight + 1, corridor.EndY + 1);
                    roomY     = math.clamp(roomY, 2, board.Size.y - 2 - roomHeight);
                    break;
                }

                return(new RoomComponent
                {
                    X = math.clamp(roomX, 2, board.Size.x - 2),
                    Y = math.clamp(roomY, 2, board.Size.y - 2),
                    Size = new int2(roomWidth, roomHeight),
                    EnteringCorridor = corridor.Direction
                });
            }
コード例 #13
0
ファイル: EnemyAi.cs プロジェクト: orzech123123/battleships
        public (int Col, int Row) GetNextBestShot(BoardComponent boardComponent, ICollection <Ship> ships)
        {
            var damagedShip = ships.FirstOrDefault(ship => ship.IsDamaged && !ship.IsDestroyed);

            if (damagedShip != null)
            {
                return(GetNextShotInDamagedShip(boardComponent, damagedShip));
            }

            var(col, row) = (_rnd.Next(0, boardComponent.Board.Cols), _rnd.Next(0, boardComponent.Board.Rows));

            //brute-force
            if (!new[] { CellState.Sea, CellState.Ship }.Contains(boardComponent.Board.GetCellState(col, row)))
            {
                return(GetNextBestShot(boardComponent, ships));
            }

            return(col, row);
        }
コード例 #14
0
        private void CreateBoardComponentsForOrphans(Tile tile)
        {
            var orphanComponents = tile.Components.Where(c => c.BoardComponent == null);

            foreach (var tc in orphanComponents)
            {
                var boardComponent = new BoardComponent
                {
                    BoardId     = tile.BoardId,
                    IsOpen      = true,
                    Points      = 0,
                    TerrainType = tc.TileTypeComponent.TerrainType,
                    Components  = new List <TileComponent> {
                        tc
                    }
                };
                _boardComponentRepository.Add(boardComponent);
            }
        }
コード例 #15
0
    public BoardSystem(BoardComponent comp)
    {
        boardComp  = comp;
        CandyGrid  = new Grid <Candy>(boardComp.Width, boardComp.Height, boardComp.CellSize, boardComp.OriginPosition, GridType.Grid2D, boardComp.DebugLine);
        Bananas    = new List <Candy>();
        Grapes     = new List <Candy>();
        Watermelon = new List <Candy>();
        Cherry     = new List <Candy>();
        Apple      = new List <Candy>();
        Triangle   = new List <Candy>();
        Square     = new List <Candy>();
        Hexagon    = new List <Candy>();
        Pentagon   = new List <Candy>();
        Scatter    = new List <Candy>();
        Jackpot    = new List <Candy>();

        GameObject CoroutineObject = new GameObject("Board System Coroutine Helper");

        _Coroutine = CoroutineObject.AddComponent <CoroutineHelper>();
    }
コード例 #16
0
ファイル: EnemyAi.cs プロジェクト: orzech123123/battleships
        private (int Col, int Row) GetNextShotInDamagedShip(BoardComponent boardComponent, Ship damagedShip)
        {
            foreach (var damagedSegment in damagedShip.Segments.Where(seg => seg.IsHit))
            {
                var possibleNextHits = new List <(int col, int row)>();
                for (var xAxis = -1; xAxis <= 1; xAxis++)
                {
                    for (var yAxis = -1; yAxis <= 1; yAxis++)
                    {
                        if (xAxis == 0 && yAxis == 0)
                        {
                            continue;
                        }

                        var cellX = damagedSegment.X + xAxis;
                        var cellY = damagedSegment.Y + yAxis;

                        if (!boardComponent.Board.IsValidCell(cellX, cellY))
                        {
                            continue;
                        }

                        if (!new [] { CellState.Sea, CellState.Ship }.Contains(boardComponent.Board.GetCellState(cellX, cellY)))
                        {
                            continue;
                        }

                        possibleNextHits.Add((cellX, cellY));
                    }
                }

                if (possibleNextHits.Any())
                {
                    return(possibleNextHits.OrderBy(hit => _rnd.Next()).First());
                }
            }

            throw new NotSupportedException("Though there is at least one damage ship I didn't find cell to shot!'");
        }
コード例 #17
0
        public async Task <(bool Hit, bool GameOver)> ProcessTurnAsync( //would rather return "out bool gameOver" but async methods doesnt support such params
            int col,
            int row,
            BoardComponent oponentBoardComponent,
            ICollection <Ship> oponentShips,
            string gameOverMessage)
        {
            var hit = HitOrMishitShip(col, row, oponentBoardComponent, oponentShips);

            SunkShipsIfDestroyed(oponentBoardComponent, oponentShips);

            await oponentBoardComponent.RedrawAsync();

            var gameOver = CheckIfGameIsOver(oponentShips);

            if (gameOver)
            {
                await _jsRuntime.InvokeAsync <string>("alert", gameOverMessage);
            }

            return(hit, gameOver);
        }
コード例 #18
0
 /// <summary>
 /// A constructor for the class inheriting the base class.
 /// </summary>
 /// <param name="component">The BoardComponent the constructor uses.</param>
 public BonusDecorator(BoardComponent component)
     : base(component)
 {
 }
コード例 #19
0
            private CorridorComponent CreateCorridor(RoomComponent room, BoardComponent board, ref Unity.Mathematics.Random random)
            {
                var direction         = (Direction)random.NextInt(0, 4);
                var oppositeDirection = (Direction)(((int)room.EnteringCorridor + 2) % 4);

                if (random.NextInt(0, 100) > 75) //25% chance to go further from center
                {
                    var centerX = (int)math.round(board.Size.x / 2f);
                    var centerY = (int)math.round(board.Size.y / 2f);
                    if (room.X > centerX && room.Y > centerY)
                    {
                        direction = random.NextBool() ? Direction.Right : random.NextBool() ? Direction.Up : Direction.Down;
                    }
                    else if (room.X < centerX && room.Y > centerY)
                    {
                        direction = random.NextBool() ? Direction.Left : random.NextBool() ? Direction.Up : Direction.Down;
                    }
                    else if (room.X > centerX && room.Y < centerY)
                    {
                        direction = random.NextBool() ? Direction.Right : random.NextBool() ? Direction.Up : Direction.Down;
                    }
                    else
                    {
                        direction = random.NextBool() ? Direction.Left : random.NextBool() ? Direction.Up : Direction.Down;
                    }
                }

                if (direction == oppositeDirection)
                {
                    var directionInt = (int)direction;
                    directionInt++;
                    directionInt = directionInt % 4;
                    direction    = (Direction)directionInt;
                }

                var corridorLength = random.NextInt(board.MinCorridorLength, board.MaxCorridorLength);
                var corridorX      = 0;
                var corridorY      = 0;

                var maxLength = board.MaxCorridorLength;

                switch (direction)
                {
                case Direction.Up:
                    corridorX = random.NextInt(room.X, room.X + room.Size.x);
                    corridorY = room.Y + room.Size.y - 2;
                    maxLength = board.Size.y - 2 - corridorY;
                    break;

                case Direction.Left:
                    corridorX = room.X + room.Size.x - 2;
                    corridorY = random.NextInt(room.Y, room.Y + room.Size.y);
                    maxLength = board.Size.x - 2 - corridorX;
                    break;

                case Direction.Down:
                    corridorX = random.NextInt(room.X, room.X + room.Size.x + 1);
                    corridorY = room.Y;
                    maxLength = corridorY - 2;
                    break;

                case Direction.Right:
                    corridorX = room.X;
                    corridorY = random.NextInt(room.Y, room.Y + room.Size.y + 1);
                    maxLength = corridorX - 2;
                    break;
                }

                corridorLength = math.clamp(corridorLength, 0, maxLength);

                return(new CorridorComponent
                {
                    StartX = corridorX,
                    StartY = corridorY,
                    Length = corridorLength,
                    Direction = direction
                });
            }
コード例 #20
0
        public async Task CheckIsCastleClosed(BoardComponent bc, int currentTileX, int currentTileY)
        {
            if (bc.TerrainType != TerrainType.Castle)
            {
                return;
            }

            // castle is closed if each component has a neighbouring tile on each tile side
            foreach (var component in bc.Components)
            {
                var tile = component.Tile;

                if (tile.TileId != 0)
                {
                    await _tileRepository.GetTileWithTileType(tile.TileId);
                }

                if (tile.GetComponentAt(TilePosition.Top).TileTypeComponentId == component.TileTypeComponentId)
                {
                    bool existNeighbour = await _tileRepository.ExistTile(bc.BoardId, tile.X, tile.Y - 1);

                    bool isClosedByCurrentTile = tile.X == currentTileX && tile.Y - 1 == currentTileY;
                    if (!existNeighbour && !isClosedByCurrentTile)
                    {
                        return;
                    }
                }

                if (tile.GetComponentAt(TilePosition.Right).TileTypeComponentId == component.TileTypeComponentId)
                {
                    bool existNeighbour = await _tileRepository.ExistTile(bc.BoardId, tile.X + 1, tile.Y);

                    bool isClosedByCurrentTile = tile.X + 1 == currentTileX && tile.Y == currentTileY;
                    if (!existNeighbour && !isClosedByCurrentTile)
                    {
                        return;
                    }
                }

                if (tile.GetComponentAt(TilePosition.Bottom).TileTypeComponentId == component.TileTypeComponentId)
                {
                    bool existNeighbour = await _tileRepository.ExistTile(bc.BoardId, tile.X, tile.Y + 1);

                    bool isClosedByCurrentTile = tile.X == currentTileX && tile.Y + 1 == currentTileY;
                    if (!existNeighbour && !isClosedByCurrentTile)
                    {
                        return;
                    }
                }

                if (tile.GetComponentAt(TilePosition.Left).TileTypeComponentId == component.TileTypeComponentId)
                {
                    bool existNeighbour = await _tileRepository.ExistTile(bc.BoardId, tile.X - 1, tile.Y);

                    bool isClosedByCurrentTile = tile.X - 1 == currentTileX && tile.Y == currentTileY;
                    if (!existNeighbour && !isClosedByCurrentTile)
                    {
                        return;
                    }
                }
            }

            // castle is closed
            bc.IsOpen = false;
            bc.Points = bc.Components.Count(); // should be tiles count
        }
コード例 #21
0
 /// <summary>
 /// The constructor that adds the properties of BoardComponent to the decorated component.
 /// </summary>
 /// <param name="component">An instance of the BoardComponent is passed as parameter.</param>
 protected BoardComponentDecorator(BoardComponent component)
 {
     this.decoratedComponent = component;
 }
コード例 #22
0
        private void GetMergedComponent(TileComponent neighbourComponent, TileComponent component, out BoardComponent merged)
        {
            var bcComponent = _boardComponentRepository.Get(component.BoardComponentId) ?? component.BoardComponent;
            var bcNeighbour = _boardComponentRepository.Get(neighbourComponent.BoardComponentId) ?? neighbourComponent.BoardComponent;

            merged = bcNeighbour;

            // join new component to existing bc
            if (bcComponent != null)
            {
                merged = MergeBoardComponents(bcComponent, bcNeighbour);
            }

            merged.Components.Add(component);
            component.BoardComponent   = merged;
            component.BoardComponentId = merged.BoardComponentId;
        }