Ejemplo n.º 1
0
        public static bool CheckIfIntersect(Ship ship, Pos newPos, int newSize, ShipDirection newDirection)
        {
            // Ship hasn't been placed yet
            if (ship.ShipPos == null)
            {
                return(false);
            }

            var padding = ActiveGame.GetRuleVal(RuleType.ShipPadding);

            // This is one retarded way of doing it, but hey, it works...
            for (int offset = 0; offset < newSize; offset++)
            {
                var newPosOffsetX = newPos.X + (newDirection == ShipDirection.Right ? offset : 0);
                var newPosOffsetY = newPos.Y + (newDirection == ShipDirection.Right ? 0 : offset);

                for (int i = 0; i < ship.Size; i++)
                {
                    var shipPosX = ship.ShipPos.X + (ship.Direction == ShipDirection.Right ? i : 0);
                    var shipPosY = ship.ShipPos.Y + (ship.Direction == ShipDirection.Right ? 0 : i);

                    if (shipPosX >= newPosOffsetX - padding && shipPosX <= newPosOffsetX + padding &&
                        shipPosY >= newPosOffsetY - padding && shipPosY <= newPosOffsetY + padding)
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Ejemplo n.º 2
0
        public static bool AutoPlaceShips(Player player)
        {
            const int tryAmount = 64;
            var       boardSize = ActiveGame.GetRuleVal(RuleType.BoardSize);

            foreach (var ship in player.Ships)
            {
                var attemptCount = 0;

                // Attempt to place ship at X different locations
                while (attemptCount < tryAmount)
                {
                    var pos = new Pos(Random.Next(0, boardSize), Random.Next(0, boardSize));
                    var dir = Random.Next(0, 2) == 1 ? ShipDirection.Right : ShipDirection.Down;

                    if (!PlayerLogic.CheckValidShipPlacementPos(player, pos, ship.Size, dir))
                    {
                        attemptCount++;
                        continue;
                    }

                    ship.SetLocation(pos, dir);
                    break;
                }

                if (attemptCount >= tryAmount)
                {
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 3
0
        public static bool CheckIfPosInBoard(Pos pos)
        {
            var boardSize = ActiveGame.GetRuleVal(RuleType.BoardSize);

            // Out of bounds
            if (pos.X < 0 || pos.X >= boardSize)
            {
                return(false);
            }
            if (pos.Y < 0 || pos.Y >= boardSize)
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 4
0
        public static Player FindNextPlayer(IReadOnlyList <Player> players, Player player)
        {
            var    playerCount = ActiveGame.GetRuleVal(RuleType.PlayerCount);
            Player nextPlayer  = null;

            if (player == null)
            {
                foreach (var tmpPlayer in players)
                {
                    if (PlayerLogic.IsAlive(tmpPlayer))
                    {
                        return(tmpPlayer);
                    }
                }

                // No more players alive but user requested next player
                throw new NullReferenceException(nameof(nextPlayer));
            }

            for (int i = 0; i < playerCount; i++)
            {
                // Find current player's index
                if (players[i] != player)
                {
                    continue;
                }

                // Find next alive player
                for (int j = 1; j < playerCount; j++)
                {
                    var tmpPlayer = players[i + j - (i + j < playerCount ? 0 : playerCount)];

                    if (PlayerLogic.IsAlive(tmpPlayer))
                    {
                        nextPlayer = tmpPlayer;
                    }
                }
            }

            // If player is the only player left (and the winner), return itself
            if (nextPlayer == null)
            {
                return(player);
            }

            return(nextPlayer);
        }
Ejemplo n.º 5
0
        public static void GenShips(List <Ship> ships, ShipType type, RuleType countRule, RuleType sizeRule)
        {
            // Get ship size according to current rule set
            var size = ActiveGame.GetRuleVal(sizeRule);

            // Create clones of ships
            for (var i = 0; i < ActiveGame.GetRuleVal(countRule); i++)
            {
                var ship = new Ship(
                    ShipInitializers.DefaultShipSet.FirstOrDefault(m => m.Type.Equals(type)))
                {
                    Size = size
                };

                ships.Add(ship);
            }
        }