Пример #1
0
        public void AddSuccessfulShot(MatrixCoordinates shot)
        {
            int row = shot.Row;
            int col = shot.Col * 2 - 1;

            field[row, col] = 'o';
        }
Пример #2
0
        private static Ship GetNewShip(Orientation orn, MatrixCoordinates cordinates)
        {
            Random rnd    = new Random();
            int    number = rnd.Next(1, 6);

            switch (number)
            {
            case 1:
                return(new AircraftCarrier(orn, cordinates));

            case 2:
                return(new Destroyer(orn, cordinates));

            case 3:
                return(new PatrolBoat(orn, cordinates));

            case 4:
                return(new Submarine(orn, cordinates));

            case 5:
                return(new Battleship(orn, cordinates));

            default:
                throw new ArgumentException();
            }
        }
Пример #3
0
        public void AddUnSuccessfulShot(MatrixCoordinates shot)
        {
            int row = shot.Row;
            int col = shot.Col * 2 - 1;

            field[row, col] = 'x'; //PONQKOGA GYRMI EI TUKA  s out of range
        }
Пример #4
0
        public bool IsHit(MatrixCoordinates hitPoint)
        {
            if (this.Orientation == Orientation.Horizontal)
            {
                for (int i = 0; i < this.GetShipLength(); i++)
                {
                    if (this.TopLeft.Row == hitPoint.Row && this.TopLeft.Col + i == hitPoint.Col)
                    {
                        this.hitCounter++;
                        IsShipDestroyed();
                        return(true);
                    }
                }

                return(false);
            }

            if (this.Orientation == Orientation.Vertical)
            {
                for (int i = 0; i < this.GetShipLength(); i++)
                {
                    if (this.TopLeft.Row + i == hitPoint.Row && this.TopLeft.Col == hitPoint.Col)
                    {
                        this.hitCounter++;
                        IsShipDestroyed();
                        return(true);
                    }
                }
            }

            return(false);
        }
Пример #5
0
        public bool IsHit(MatrixCoordinates hitPoint)
        {
            if (this.Orientation == Orientation.Horizontal)
            {
                for (int i = 0; i < this.GetShipLength(); i++)
                {
                    if (this.TopLeft.Row == hitPoint.Row && this.TopLeft.Col + i == hitPoint.Col)
                    {
                        this.hitCounter++;
                        IsShipDestroyed();
                        return true;
                    }
                }

                return false;
            }

            if (this.Orientation == Orientation.Vertical)
            {
                for (int i = 0; i < this.GetShipLength(); i++)
                {
                    if (this.TopLeft.Row + i == hitPoint.Row && this.TopLeft.Col == hitPoint.Col)
                    {
                        this.hitCounter++;
                        IsShipDestroyed();
                        return true;
                    }
                }
            }

            return false;
        }
        public override bool Equals(object obj)
        {
            MatrixCoordinates temp = obj as MatrixCoordinates;

            if (temp == null)
            {
                return(false);
            }
            return(Equals(temp));
        }
 public bool Equals(MatrixCoordinates value)
 {
     if (ReferenceEquals(null, value))
     {
         return false;
     }
     if (ReferenceEquals(this, value))
     {
         return true;
     }
     return row == value.row &&
         col == value.col;
 }
 public bool Equals(MatrixCoordinates value)
 {
     if (ReferenceEquals(null, value))
     {
         return(false);
     }
     if (ReferenceEquals(this, value))
     {
         return(true);
     }
     return(row == value.row &&
            col == value.col);
 }
Пример #9
0
        private void InsertHorizontalShip(int length, MatrixCoordinates topLeft, char symbol)
        {
            int row = topLeft.Row;
            int col = topLeft.Col * 2 - 1;

            for (int i = 0; i < length; i++)
            {
                this.field[row, col] = symbol;
                while (field[row, col] != '_')
                {
                    col++;
                }
            }
        }
Пример #10
0
        public static List <Ship> Generate(int numberOfShips, List <Ship> ships, int maxRow, int maxCol)
        {
            for (int i = 0; i < numberOfShips;)
            {
                Orientation       rndOrientation = GetRandomOrientation();
                MatrixCoordinates newCordinates  = GetRandomMatrix(maxRow, maxCol);
                Ship newShip     = GetNewShip(rndOrientation, newCordinates);
                bool isValidShip = CheckIsValid(newShip, ships, maxRow, maxCol);
                if (isValidShip)
                {
                    ships.Add(newShip);
                    i++;
                }
            }

            return(ships);
        }
Пример #11
0
        private bool isAlreadyHit(MatrixCoordinates shot)
        {
            if (this.playerField.HitCoords.Contains(shot))
            {
                Console.Clear();
                Console.ForegroundColor = ConsoleColor.Red;
                Console.SetCursorPosition(Console.WindowWidth / 2 - "POINT IS ALREADY HIT!".Length / 2, Console.WindowHeight / 2 - 1);
                Console.WriteLine("POINT IS ALREADY HIT!");
                Thread.Sleep(1000);
                while (Console.KeyAvailable)
                {
                    Console.ReadKey(false);
                }
                Console.CursorVisible = false;
                Console.Clear();
                this.RenderTheWorld();
                return(false);
            }

            return(true);
        }
        private static Ship GetNewShip(Orientation orn, MatrixCoordinates cordinates)
        {
            Random rnd = new Random();
            int number = rnd.Next(1, 6);
            switch (number)
            {
                case 1:
                    return new AircraftCarrier(orn, cordinates);
                case 2:
                    return new Destroyer(orn, cordinates);
                case 3:
                    return new PatrolBoat(orn, cordinates);
                case 4:
                    return new Submarine(orn, cordinates);
                case 5:
                    return new Battleship(orn, cordinates);

                default:
                    throw new ArgumentException();
            }
        }
Пример #13
0
        private bool isAlreadyHit(MatrixCoordinates shot)
            {
            if (this.playerField.HitCoords.Contains(shot))
                {
                Console.Clear();
                Console.ForegroundColor = ConsoleColor.Red;
                Console.SetCursorPosition(Console.WindowWidth / 2 - "POINT IS ALREADY HIT!".Length / 2, Console.WindowHeight / 2 - 1);
                Console.WriteLine("POINT IS ALREADY HIT!");
                Thread.Sleep(1000);
                while (Console.KeyAvailable)
                {
                    Console.ReadKey(false);
                }
                Console.CursorVisible = false;
                Console.Clear();
                this.RenderTheWorld();
                return false;
            }

            return true;
        }
Пример #14
0
        private bool IsInRange(MatrixCoordinates shot)
        {
            if (shot.Row > fieldRowMax || shot.Row < fieldRowMin || shot.Col > fieldColMax || shot.Col < fieldColMin)
            {
                Console.Clear();
                Console.ForegroundColor = ConsoleColor.Red;
                Console.SetCursorPosition(Console.WindowWidth / 2 - "ROWS SHOULD BE IN THE RANGE[{0},{1}]".Length / 2, Console.WindowHeight / 2 - 2);
                Console.WriteLine("ROWS SHOULD BE IN THE RANGE[{0},{1}]", fieldRowMin, fieldRowMax);
                Console.SetCursorPosition(Console.WindowWidth / 2 - "AND COLS IN THE RANGE [{0},{1}]".Length / 2, Console.WindowHeight / 2 - 1);
                Console.WriteLine("AND COLS IN THE RANGE [{0},{1}]", fieldColMin, fieldColMax);
                Thread.Sleep(1000);
                while (Console.KeyAvailable)
                {
                    Console.ReadKey(false);
                }
                Console.CursorVisible = false;
                Console.Clear();
                this.RenderTheWorld();
                return false;
            }

            return true;
        }
Пример #15
0
        private bool IsInRange(MatrixCoordinates shot)
        {
            if (shot.Row > fieldRowMax || shot.Row < fieldRowMin || shot.Col > fieldColMax || shot.Col < fieldColMin)
            {
                Console.Clear();
                Console.ForegroundColor = ConsoleColor.Red;
                Console.SetCursorPosition(Console.WindowWidth / 2 - "ROWS SHOULD BE IN THE RANGE[{0},{1}]".Length / 2, Console.WindowHeight / 2 - 2);
                Console.WriteLine("ROWS SHOULD BE IN THE RANGE[{0},{1}]", fieldRowMin, fieldRowMax);
                Console.SetCursorPosition(Console.WindowWidth / 2 - "AND COLS IN THE RANGE [{0},{1}]".Length / 2, Console.WindowHeight / 2 - 1);
                Console.WriteLine("AND COLS IN THE RANGE [{0},{1}]", fieldColMin, fieldColMax);
                Thread.Sleep(1000);
                while (Console.KeyAvailable)
                {
                    Console.ReadKey(false);
                }
                Console.CursorVisible = false;
                Console.Clear();
                this.RenderTheWorld();
                return(false);
            }

            return(true);
        }
Пример #16
0
        private void ValidateHit(ref MatrixCoordinates shot, ref MatrixCoordinates robotShot)
        {
            int  counter   = 0;
            bool isInRange = true;
            bool isHitted  = true;

            do
            {
                isInRange = true;
                isHitted  = true;

                if (counter == 5)
                {
                    Console.BackgroundColor = ConsoleColor.Red;
                    Console.ForegroundColor = ConsoleColor.Black;
                    throw new DummyException("You simply don't want to play or you can't read!");
                }
                isInRange = IsInRange(shot);
                isHitted  = isAlreadyHit(shot);
                if (!(isInRange && isHitted))
                {
                    shot = keyboard.ReadUserInput();
                }
                counter++;
            }while (!(isInRange && isHitted));

            // check is already hit player shot

            // check is already hit robot shot
            while (this.gameField.HitCoords.Contains(robotShot))
            {
                robotShot = Robot.GenerateShot(fieldRowMin, fieldRowMax, fieldColMin, fieldColMax);
            }

            this.playerField.AddHitCoords(shot);
            this.gameField.AddHitCoords(robotShot);
        }
Пример #17
0
 public Battleship(Orientation orientation, MatrixCoordinates topLeft) : base(orientation, topLeft)
 {
 }
Пример #18
0
        private void ValidateHit(ref MatrixCoordinates shot, ref MatrixCoordinates robotShot)
        {
            int counter = 0;
            bool isInRange = true;
            bool isHitted = true;

            do
            {
                isInRange = true;
                isHitted = true;
                
                if (counter == 5)
                {
                    Console.BackgroundColor = ConsoleColor.Red;
                    Console.ForegroundColor = ConsoleColor.Black;
                    throw new DummyException("You simply don't want to play or you can't read!");
                }
                isInRange = IsInRange(shot);
                isHitted = isAlreadyHit(shot);
                if (!(isInRange && isHitted))
                {
                shot = keyboard.ReadUserInput();
            }
                counter++;
            }
            while (!(isInRange && isHitted));
           
            // check is already hit player shot

            // check is already hit robot shot
            while (this.gameField.HitCoords.Contains(robotShot))
            {
                robotShot = Robot.GenerateShot(fieldRowMin, fieldRowMax, fieldColMin, fieldColMax);
            }

            this.playerField.AddHitCoords(shot);
            this.gameField.AddHitCoords(robotShot);
        }
Пример #19
0
 public AircraftCarrier(Orientation orientation, MatrixCoordinates topLeft) : base(orientation, topLeft)
 {
 }
Пример #20
0
 public Submarine(Orientation orientation, MatrixCoordinates topLeft) : base(orientation, topLeft)
 {
 }
Пример #21
0
 public void AddSuccessfulShot(MatrixCoordinates shot)
 {
     int row = shot.Row;
     int col = shot.Col * 2 - 1;
     field[row, col] = 'o';
 }
Пример #22
0
 public Battleship(Orientation orientation, MatrixCoordinates topLeft) : base(orientation, topLeft)
 {
 }
Пример #23
0
 public PatrolBoat(Orientation orientation, MatrixCoordinates topLeft) : base(orientation, topLeft)
 {
 }
Пример #24
0
 public void AddUnSuccessfulShot(MatrixCoordinates shot)
 {
     int row = shot.Row;
     int col = shot.Col * 2 - 1;
     field[row, col] = 'x'; //PONQKOGA GYRMI EI TUKA  s out of range
 }
Пример #25
0
 public void AddHitCoords(MatrixCoordinates shot)
 {
     this.hitCoords.Add(shot);
 }
Пример #26
0
 public void AddHitCoords(MatrixCoordinates shot)
 {
     this.hitCoords.Add(shot);
 }
Пример #27
0
        private void InsertHorizontalShip(int length, MatrixCoordinates topLeft, char symbol)
        {
            int row = topLeft.Row;
            int col = topLeft.Col * 2 - 1;

            for (int i = 0; i < length; i++)
            {
                this.field[row, col] = symbol;
                while (field[row, col] != '_') 
                {
                    col++;
                }
            }
       }
Пример #28
0
 public Destroyer(Orientation orientation, MatrixCoordinates topLeft) : base(orientation, topLeft)
 {
 }
Пример #29
0
 public Ship(Orientation orientation, MatrixCoordinates topLeft)
 {
     this.Orientation = orientation;
     this.TopLeft = topLeft;
     this.isDestroyed = false;
 }
        public AircraftCarrier(Orientation orientation, MatrixCoordinates topLeft) : base(orientation, topLeft)
        {

        }
Пример #31
0
 public PatrolBoat(Orientation orientation, MatrixCoordinates topLeft) : base( orientation, topLeft)
 {
     
 }
Пример #32
0
 public Submarine(Orientation orientation, MatrixCoordinates topLeft) : base(orientation, topLeft)
 {
 }
Пример #33
0
        public void Run()
        {
            // add  playership in player field
            foreach (var item in playerShips)
            {
                playerField.AddShip(item);
            }

            this.RenderTheWorld();

            while (playerShips.Count != 0 && computerShips.Count != 0)
            {
                // read player coordinates
                MatrixCoordinates shot      = keyboard.ReadUserInput();
                MatrixCoordinates robotShot = Robot.GenerateShot(fieldRowMin, fieldRowMax, fieldColMin, fieldColMax);
                ValidateHit(ref shot, ref robotShot);

                Console.Clear();
                // add shot as unsuccessful
                gameField.AddUnSuccessfulShot(shot);
                playerField.AddUnSuccessfulShot(robotShot);

                // check if computer ship is Hit

                foreach (var item in computerShips)
                {
                    if (item.IsHit(shot))
                    {
                        PlayExplosion();
                        gameField.AddSuccessfulShot(shot);
                        break;
                    }
                }

                // check if player ship is Hit
                foreach (var item in playerShips)
                {
                    if (item.IsHit(robotShot))
                    {
                        PlayExplosion();
                        playerField.AddSuccessfulShot(robotShot);
                        break;
                    }
                }
                this.RenderTheWorld();
                computerShips.RemoveAll(ship => ship.IsDestroyed);
                playerShips.RemoveAll(ship => ship.IsDestroyed);
            }

            if (computerShips.Count == 0)
            {
                Console.Clear();
                Console.SetCursorPosition(Console.WindowWidth / 2 - "Congratssss You Win!".Length / 2, Console.WindowHeight / 2 - 2);
                Console.WriteLine("Congratssss You Win!");
            }
            else
            {
                Console.Clear();
                Console.SetCursorPosition(Console.WindowWidth / 2 - "ROWS SHOULD BE IN THE RANGE[{0},{1}]".Length / 2, Console.WindowHeight / 2 - 2);
                Console.WriteLine("You LOST!");
            }
        }
Пример #34
0
 public Destroyer(Orientation orientation, MatrixCoordinates topLeft) : base(orientation, topLeft)
 {
 }
Пример #35
0
 public Ship(Orientation orientation, MatrixCoordinates topLeft)
 {
     this.Orientation = orientation;
     this.TopLeft     = topLeft;
     this.isDestroyed = false;
 }