示例#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 AddShip(Ship ship, Point position)
        {
            Point tempPosition = new Point();

            tempPosition.X = position.X;
            tempPosition.Y = position.Y;

            if (!checkAddingNewShip(ship, position))
            {
                return(false);
            }

            ShipsPosition temp = new ShipsPosition(ship, tempPosition);

            ShipPositions.Add(temp);

            return(true);
        }
示例#3
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);
            }
        }
示例#4
0
        //
        // Souhrn:
        //  Změní pozici lodě podle toho, jakou klávesu stiskl hráč.
        //
        private void changeShipPositionByInput(ConsoleKeyInfo key, ShipsPosition shipPosition, TileMap map)
        {
            switch (key.Key)
            {
            case ConsoleKey.RightArrow:
            {
                Point newPoint = new Point();
                newPoint.X = shipPosition.Position.X + 1;
                newPoint.Y = shipPosition.Position.Y;
                if (map.CheckEdgeOfMapShip(shipPosition.Ship, newPoint))
                {
                    shipPosition.Position = newPoint;
                }
                break;
            }

            case ConsoleKey.LeftArrow:
            {
                Point newPoint = new Point();
                newPoint.X = shipPosition.Position.X - 1;
                newPoint.Y = shipPosition.Position.Y;
                if (map.CheckEdgeOfMapShip(shipPosition.Ship, newPoint))
                {
                    shipPosition.Position = newPoint;
                }
                break;
            }

            case ConsoleKey.UpArrow:
            {
                Point newPoint = new Point();
                newPoint.X = shipPosition.Position.X;
                newPoint.Y = shipPosition.Position.Y - 1;
                if (map.CheckEdgeOfMapShip(shipPosition.Ship, newPoint))
                {
                    shipPosition.Position = newPoint;
                }
                break;
            }

            case ConsoleKey.DownArrow:
            {
                Point newPoint = new Point();
                newPoint.X = shipPosition.Position.X;
                newPoint.Y = shipPosition.Position.Y + 1;
                if (map.CheckEdgeOfMapShip(shipPosition.Ship, newPoint))
                {
                    shipPosition.Position = newPoint;
                }
                break;
            }

            case ConsoleKey.R:
            {
                Ship tempShip = new Ship(shipPosition.Ship.Class, shipPosition.Ship.Direction);
                for (int i = 0; i < 3; i++)
                {
                    tempShip.Rotate();
                    if (map.CheckEdgeOfMapShip(tempShip, shipPosition.Position))
                    {
                        shipPosition.Ship = tempShip;
                        break;
                    }
                }
                break;
            }

            default:
                break;
            }
        }
示例#5
0
        //
        // Souhrn:
        //  Hráč si vybírají místa pro lodě.
        //
        private void pickShips(Player player)
        {
            List <Ship> userShips = new List <Ship>();

            foreach (var item in pickedShipClasses)
            {
                Ship tempShip = new Ship(item);
                userShips.Add(tempShip);
            }
            foreach (Ship ship in userShips)
            {
                Point position = new Point();
                position.X = (player.Map.Size.width - 1) / 2;
                position.Y = (player.Map.Size.height - 1) / 2;

                ShipsPosition shipPosition = new ShipsPosition(ship, position);

                string error = String.Empty;

                List <ConsoleKey> acceptable = new List <ConsoleKey>();
                acceptable.Add(ConsoleKey.RightArrow);
                acceptable.Add(ConsoleKey.LeftArrow);
                acceptable.Add(ConsoleKey.UpArrow);
                acceptable.Add(ConsoleKey.DownArrow);
                acceptable.Add(ConsoleKey.R);
                acceptable.Add(ConsoleKey.Enter);

                while (true)
                {
                    Console.WriteLine("{0}'s turn", player.Name);
                    player.Map.DisplayMap(shipPosition);

                    if (error != String.Empty)
                    {
                        Console.WriteLine(error);
                    }

                    var readKey = Console.ReadKey();


                    if (acceptable.Contains(readKey.Key))
                    {
                        if (readKey.Key == ConsoleKey.Enter)
                        {
                            if (player.Map.AddShip(shipPosition.Ship, shipPosition.Position))
                            {
                                error = String.Empty;
                                break;
                            }
                            else
                            {
                                error = "Can't add ship.";
                            }
                        }
                        else
                        {
                            error = String.Empty;
                            changeShipPositionByInput(readKey, shipPosition, player.Map);
                        }
                    }
                    Console.Clear();
                }
                Console.Clear();
            }
        }