Пример #1
0
        // zobrazení při pokládání lodě
        public void DisplayMap(ShipsPosition newShip)
        {
            List <Point> newShipPoints = new List <Point>();

            newShipPoints = ShipFunctions.GetPoints(newShip.Ship, newShip.Position);

            List <Point> pointList = getPointsFromPlacedShips();

            Console.Write("   ");
            for (int i = 0; i < Size.width; i++)
            {
                Console.Write("{0} ", (char)(65 + i));
            }

            Console.WriteLine();

            for (int i = 0; i < Size.height; i++)
            {
                Console.Write("   ");
                for (int z = 0; z < Size.width; z++)
                {
                    Point point = new Point();
                    point.X = z;
                    point.Y = i;
                    displayPoint(point, pointList, newShipPoints);
                }

                Console.Write("{0} ", i + 1);
                Console.WriteLine();
            }
        }
Пример #2
0
        public bool CheckEdgeOfMapShip(Ship newShip, Point position)
        {
            foreach (var item in ShipFunctions.GetPoints(newShip, position))
            {
                if (item.X > Size.width - 1 || item.Y > Size.height - 1 || item.X < 0 || item.Y < 0)
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #3
0
        private List <Point> getAllCollisionPoints()
        {
            List <Point> positions = new List <Point>();

            foreach (var item in ShipPositions)
            {
                List <Point> tempPositions = ShipFunctions.GetPoints(item.Ship, item.Position);
                positions.AddRange(tempPositions);
            }

            return(positions);
        }
Пример #4
0
        private List <Point> getPointsFromPlacedShips()
        {
            List <Point> pointList = new List <Point>();

            if (ShipPositions.Count > 0)
            {
                foreach (var shipPos in ShipPositions)
                {
                    pointList.AddRange(ShipFunctions.GetPoints(shipPos.Ship, shipPos.Position));
                }
            }
            return(pointList);
        }
Пример #5
0
        private bool checkCollisionShip(Ship ship, Point position)
        {
            List <Point> shipPoints = ShipFunctions.GetPoints(ship, position);

            foreach (var item in shipPoints)
            {
                if (!checkAllNeihgbors(item))
                {
                    return(false);
                }
            }
            return(true);
        }
Пример #6
0
        public bool isShipSunk(ShipsPosition shipPosition)
        {
            if (shipPosition.Ship.Status == ShipStatus.Sunk)
            {
                return(true);
            }
            else
            {
                List <Point> shipPoints = ShipFunctions.GetPoints(shipPosition.Ship, shipPosition.Position);

                bool result = shipPoints.All(item => AttackedTiles.Any(tile => item.X == tile.X && item.Y == tile.Y));

                if (result)
                {
                    shipPosition.Ship.Sink();
                    return(true);
                }
                return(false);
            }
        }
Пример #7
0
        //
        // Souhrn:
        //  Uživatel vybírá hratelné třídy lodí.
        //
        private void pickShipClassses()
        {
            foreach (ShipClass shipClass in Enum.GetValues(typeof(ShipClass)))
            {
                Ship         ship      = new Ship(shipClass);
                Point        tempPoint = new Point();
                List <Point> points    = ShipFunctions.GetPoints(ship, tempPoint);

                int max_x = points.Max(item => item.X);
                int max_y = points.Max(item => item.Y);
                int min_x = points.Min(item => item.X);
                int min_y = points.Min(item => item.Y);

                while (min_x < 1)
                {
                    min_x++;
                    max_x++;
                    points.ForEach(item => item.X++);
                }

                while (min_y < 1)
                {
                    min_y++;
                    max_y++;
                    points.ForEach(item => item.Y++);
                }
                Console.WriteLine("Choose the ships:");
                Console.WriteLine("ENTER - yes   X - no");

                tempPoint = new Point();
                for (int i = 0; i < max_y + 2; i++)
                {
                    for (int z = 0; z < max_x + 2; z++)
                    {
                        tempPoint.X = z;
                        tempPoint.Y = i;
                        if (points.Any(item => item.X == tempPoint.X && item.Y == tempPoint.Y))
                        {
                            Console.ForegroundColor = ConsoleColor.White;
                        }
                        else
                        {
                            Console.ForegroundColor = ConsoleColor.DarkGray;
                        }

                        Console.Write("■ ");
                        Console.ResetColor();
                    }
                    Console.WriteLine();
                }

                while (true)
                {
                    List <ConsoleKey> acceptable = new List <ConsoleKey>();
                    acceptable.Add(ConsoleKey.Enter);
                    acceptable.Add(ConsoleKey.X);
                    var readKey = Console.ReadKey();

                    if (acceptable.Contains(readKey.Key))
                    {
                        if (readKey.Key == ConsoleKey.Enter)
                        {
                            pickedShipClasses.Add(shipClass);
                        }
                        break;
                    }
                }
                Console.Clear();
            }
        }