Esempio n. 1
0
 /// <summary>
 /// Set the _Value to the PossibleAttack value
 /// </summary>
 /// <param name="value">either hit, miss, destroyed, shotalready</param>
 public AttackResult(ResultOfAttack value, string text, int row, int column)
 {
     _Value = value;
     _Text = text;
     _Ship = null;
     _Row = row;
     _Column = column;
 }
Esempio n. 2
0
 /// <summary>
 /// The tile constructor will know where it is on the grid, and is its a ship
 /// </summary>
 /// <param name="row">the row on the grid</param>
 /// <param name="col">the col on the grid</param>
 /// <param name="ship">what ship it is</param>
 public Tile(int row, int col, Ship ship)
 {
     _RowValue = row;
     _ColumnValue = col;
     _Ship = ship;
 }
Esempio n. 3
0
 /// <summary>
 /// Clearship will remove the ship from the tile
 /// </summary>
 public void ClearShip()
 {
     _Ship = null;
 }
        private static bool CheckIsValid(Ship ship, List<Ship> ships, int maxRow, int maxCol)
        {
            if (ship.Orientation == Orientation.Horizontal)
            {
                if (ship.GetShipLength() + ship.TopLeft.Col > maxCol)
                {
                    return false;
                }

                foreach (var item in ships)
                {
                    if (item.GetOrientation() == Orientation.Vertical)
                    {
                        for (int i = 0; i < item.GetShipLength(); i++)
                        {
                            if (ship.TopLeft.Row + i >= item.TopLeft.Row && ship.TopLeft.Row + i <= item.TopLeft.Row + item.GetShipLength())
                            {
                                if (ship.TopLeft.Col + ship.GetShipLength() >= item.TopLeft.Col)
                                {
                                    return false;
                                }
                            }
                        }
                    }
                    else
                    {
                        if (ship.TopLeft.Row == item.TopLeft.Row && ship.TopLeft.Col + ship.GetShipLength() >= item.TopLeft.Col)
                        {
                            return false;
                        }
                    }
                }
            }
            else if (ship.Orientation == Orientation.Vertical)
            {
                if (ship.GetShipLength() + ship.TopLeft.Row > maxRow)
                {
                    return false;
                }

                foreach (var item in ships)
                {
                    if (item.GetOrientation() == Orientation.Horizontal)
                    {
                        for (int i = 0; i < item.GetShipLength(); i++)
                        {
                            if (item.TopLeft.Row + i >= ship.TopLeft.Row && item.TopLeft.Row + i <= ship.TopLeft.Row + ship.GetShipLength())
                            {
                                if (item.TopLeft.Col + item.GetShipLength() >= ship.TopLeft.Col)
                                {
                                    return false;
                                }
                            }
                        }
                    }
                    else
                    {
                        if (ship.TopLeft.Col == item.TopLeft.Col && ship.TopLeft.Row + ship.GetShipLength() >= item.TopLeft.Row)
                        {
                            return false;
                        }
                    }
                }
            }

            return true;
        }
Esempio n. 5
0
 /// <summary>
 /// Set the _Value to the PossibleAttack value, and the _Ship to the ship
 /// </summary>
 /// <param name="value">either hit, miss, destroyed, shotalready</param>
 /// <param name="ship">the ship information</param>
 public AttackResult(ResultOfAttack value, Ship ship, string text, int row, int column)
     : this(value, text, row, column)
 {
     _Ship = ship;
 }
Esempio n. 6
0
        public bool AddShip(Ship ship)
        {
            bool AddShip = true;
            //check if its outside
            if (!ship.isOutsideOfSea())
            {
                if (shipsList.Count > 0)
                {
                    //check if it intersects any ships
                    foreach (Ship s in shipsList)
                    {
                        if (s.isShipIntersecting(ship))
                        {
                            AddShip = false;
                            break;
                        }
                    }

                }
                //else
                //we add it since it's the first ship

                if (AddShip)
                {
                    shipsList.Add(ship);
                    PlaceShipInSea(ship);
                }

            }
            else
            {
                AddShip = false;
            }

            return AddShip;
        }
Esempio n. 7
0
        public void DeleteShip(Ship ship)
        {
            foreach (Block b in ship.blocks)
                blocks[b.yPos, b.xPos] = new EmptySea(b.yPos, b.xPos);

            shipsList.Remove(ship);
        }
Esempio n. 8
0
 private void PlaceShipInSea(Ship ship)
 {
     foreach (Block b in ship.blocks)
     {
         this.blocks[b.yPos, b.xPos] = new Block(b);
         this.BlockChanged(this.blocks[b.yPos, b.xPos]);
     }
 }
Esempio n. 9
0
        public bool isShipIntersecting(Ship ship)
        {
            bool intersecting = false;

            foreach (Block b in this.blocks)
            {
                foreach (Block b2 in ship.blocks)
                {
                    if (b.xPos == b2.xPos && b.yPos == b2.yPos)
                    {
                        intersecting = true;
                        break;
                    }
                }
                if (intersecting) break;
            }

            return intersecting;
        }
Esempio n. 10
0
        /// <summary>
        /// AddShip add a ship to the SeaGrid
        /// </summary>
        /// <param name="row">row coordinate</param>
        /// <param name="col">col coordinate</param>
        /// <param name="direction">direction of ship</param>
        /// <param name="newShip">the ship</param>
        private void AddShip(int row, int col, Direction direction, Ship newShip)
        {
            try {
                int size = newShip.Size;
                int currentRow = row;
                int currentCol = col;
                int dRow = 0;
                int dCol = 0;

                if (direction == Direction.LeftRight) {
                    dRow = 0;
                    dCol = 1;
                } else {
                    dRow = 1;
                    dCol = 0;
                }

                //place ship's tiles in array and into ship object
                int i = 0;
                for (i = 0; i <= size - 1; i++) {
                    if (currentRow < 0 | currentRow >= Width | currentCol < 0 | currentCol >= Height) {
                        throw new InvalidOperationException("Ship can't fit on the board");
                    }

                    _GameTiles[currentRow, currentCol].Ship = newShip;

                    currentCol += dCol;
                    currentRow += dRow;
                }

                newShip.Deployed(direction, row, col);
            } catch (Exception e) {
                newShip.Remove();
                //if fails remove the ship
                throw new ApplicationException(e.Message);

            } finally {
                if (Changed != null) {
                    Changed(this, EventArgs.Empty);
                }
            }
        }
Esempio n. 11
0
        /// <summary>
        /// ProcessDetroy is able to process the destroyed ships targets and remove _LastHit targets.
        /// It will also call RemoveShotsAround to remove targets that it was going to shoot at
        /// </summary>
        /// <param name="row">the row that was shot at and destroyed</param>
        /// <param name="col">the row that was shot at and destroyed</param>
        /// <param name="ship">the row that was shot at and destroyed</param>
        private void ProcessDestroy(int row, int col, Ship ship)
        {
            bool foundOriginal = false;
            Location source = null;
            Target current = null;
            current = _CurrentTarget;

            foundOriginal = false;

            //i = 1, as we dont have targets from the current hit...
            int i = 0;

            for (i = 1; i <= ship.Hits - 1; i++) {
            if (!foundOriginal) {
                source = current.Source;
                //Source is nnothing if the ship was originally hit in
                // the middle. This then searched forward, rather than
                // backward through the list of targets
                if (source == null) {
                    source = current.ShotAt;
                    foundOriginal = true;
                }
            } else {
                source = current.ShotAt;
            }

            //find the source in _LastHit
            foreach (Target t in _LastHit) {
                if ((!foundOriginal && t.ShotAt == source) || (foundOriginal & t.Source == source)) {
                    current = t;
                    _LastHit.Remove(t);
                    break; // TODO: might not be correct. Was : Exit For
                }
            }

            RemoveShotsAround(current.ShotAt);
            }
        }
Esempio n. 12
0
        private void PlaceUserShip(Ship sh)
        {
            ConsoleKeyInfo rkey;

            SelectedBlocks sb = new SelectedBlocks(sh.blocks[0].xPos, sh.blocks[0].yPos, sh.length, sh.direction);

            UserSea.SelectBlocks(sb);

            do
            {
                rkey = Console.ReadKey(true);
                if (rkey.Key != ConsoleKey.Enter &&
                    (rkey.Key == ConsoleKey.LeftArrow || rkey.Key == ConsoleKey.RightArrow || rkey.Key == ConsoleKey.UpArrow || rkey.Key == ConsoleKey.DownArrow || rkey.Key == ConsoleKey.D))
                {
                    UserSea.UnSelectBlocks(sb);
                    switch (rkey.Key)
                    {
                        case ConsoleKey.LeftArrow:
                            sb.Move(MoveDirection.Left);
                            break;
                        case ConsoleKey.RightArrow:
                            sb.Move(MoveDirection.Right);
                            break;
                        case ConsoleKey.UpArrow:
                            sb.Move(MoveDirection.Up);
                            break;
                        case ConsoleKey.DownArrow:
                            sb.Move(MoveDirection.Down);
                            break;
                        case ConsoleKey.D:
                            UserSea.ChangeDirection(ref sb);
                            break;
                        default:
                            break;
                    }
                    UserSea.SelectBlocks(sb);
                }
                else
                {
                    for (int i = 0; i < sh.length; i++)
                    {
                        sh.blocks[i].xPos = sb.blocks[i].xPos;
                        sh.blocks[i].yPos = sb.blocks[i].yPos;
                    }

                    if (!UserSea.AddShip(sh))
                    {
                        //didnt add ship
                        AddMessage(MessageType.System, this.m.SHIP_INTERSECT);
                        rkey = new ConsoleKeyInfo();
                    }

                }

            } while (rkey.Key != ConsoleKey.Enter);
        }
Esempio n. 13
0
        private void PlaceComputerShip(Ship sh)
        {
            Ship shnew;
            ShipDirection direction;
            int xpp, ypp;

            do
            {
                direction = (ShipDirection)randomNum(0, 2);
                xpp = randomNum(0, SeaConstants.SEA_SIZE);
                ypp = randomNum(0, SeaConstants.SEA_SIZE);

                shnew = new Ship(sh.shipType, xpp, ypp, sh.length, direction, 0, false, sh.blocks[0].printChar, sh.blocks[0].shipname);

            } while (!ComputerSea.AddShip(shnew));
        }
Esempio n. 14
0
        private static bool CheckIsValid(Ship ship, List <Ship> ships, int maxRow, int maxCol)
        {
            if (ship.Orientation == Orientation.Horizontal)
            {
                if (ship.GetShipLength() + ship.TopLeft.Col > maxCol)
                {
                    return(false);
                }

                foreach (var item in ships)
                {
                    if (item.GetOrientation() == Orientation.Vertical)
                    {
                        for (int i = 0; i < item.GetShipLength(); i++)
                        {
                            if (ship.TopLeft.Row + i >= item.TopLeft.Row && ship.TopLeft.Row + i <= item.TopLeft.Row + item.GetShipLength())
                            {
                                if (ship.TopLeft.Col + ship.GetShipLength() >= item.TopLeft.Col)
                                {
                                    return(false);
                                }
                            }
                        }
                    }
                    else
                    {
                        if (ship.TopLeft.Row == item.TopLeft.Row && ship.TopLeft.Col + ship.GetShipLength() >= item.TopLeft.Col)
                        {
                            return(false);
                        }
                    }
                }
            }
            else if (ship.Orientation == Orientation.Vertical)
            {
                if (ship.GetShipLength() + ship.TopLeft.Row > maxRow)
                {
                    return(false);
                }

                foreach (var item in ships)
                {
                    if (item.GetOrientation() == Orientation.Horizontal)
                    {
                        for (int i = 0; i < item.GetShipLength(); i++)
                        {
                            if (item.TopLeft.Row + i >= ship.TopLeft.Row && item.TopLeft.Row + i <= ship.TopLeft.Row + ship.GetShipLength())
                            {
                                if (item.TopLeft.Col + item.GetShipLength() >= ship.TopLeft.Col)
                                {
                                    return(false);
                                }
                            }
                        }
                    }
                    else
                    {
                        if (ship.TopLeft.Col == item.TopLeft.Col && ship.TopLeft.Row + ship.GetShipLength() >= item.TopLeft.Row)
                        {
                            return(false);
                        }
                    }
                }
            }

            return(true);
        }
Esempio n. 15
0
        /// <summary>
        /// This method holds the whole game logic.
        /// It contains hit, miss, sunk and game won actions and feedbacks to the user.
        /// </summary>
        /// <param name="grid">a grid object</param>
        /// <param name="destroyer1">a ship of type destroyer</param>
        /// <param name="destroyer2">a ship of type destroyer</param>
        /// <param name="battleship1">a ship of type battleship</param>
        /// <param name="totalAttempts">the total valid inputs by the user</param>
        /// <returns>an updated board</returns>
        public static Grid CallUserInput(Grid grid, Ship destroyer1, Ship destroyer2, Ship battleship1, int totalAttempts)
        {
            string constantMessage = "Enter coordinates (row, col), e.g. A5";
            string feedbackMiss    = "*** Miss ***";
            string feedbackHit     = "*** Hit ***";
            string feedbackSunk    = "*** Sunk ***";
            string error           = "*** Error ***";

            string topMessage = string.Empty;

            bool   validInput = false;
            string userInput  = null;

            // This While Loop makes sure the user's input is in the correct format - [letter from A to J / number from 1 to 10]
            while (validInput == false)
            {
                userInput = Console.ReadLine();

                if (userInput == "show" || userInput == "hide" || userInput == "reset")
                {
                    EnableCheatMode(userInput, grid);
                    userInput = Console.ReadLine();
                }

                string patternRegex = @"^[A-J](10|[1-9])$";
                Regex  inputRegex   = new Regex(patternRegex);
                bool   validPattern = inputRegex.IsMatch(userInput);

                if (validPattern && userInput.Length <= 3)
                {
                    validInput = true;
                }
                else
                {
                    topMessage = error;
                    Console.Clear();
                    Console.WriteLine(topMessage);
                    Console.WriteLine();
                    Grid.PrintGrid(grid);
                    Console.WriteLine();
                    Console.WriteLine(constantMessage);
                }
            }

            int rowInput = userInput[0] - 65;
            int colInput = userInput.Length == 2 ? int.Parse(userInput[1].ToString()) - 1 : 9;

            // Next conditional statement holds the logic behind a "hit or miss" action.
            // It also holds the condition when the game is won - when all ships are sunk
            if (grid.Board[rowInput, colInput] == '\0' || grid.Board[rowInput, colInput] == '-')
            {
                grid.Board[rowInput, colInput] = '-';         // Miss logic - grid position is updated with a '-' char on the missed spot
                topMessage = feedbackMiss;                    // Miss logic - feedback
            }
            else if (grid.Board[rowInput, colInput] == 'H')
            {
                topMessage = feedbackMiss;
            }
            else
            {
                grid.Board[rowInput, colInput] = 'H';         // Hit action - 'H' marks a hit position on the grid

                if (
                    (destroyer1.Sunk == false && Ship.CriticalHit(destroyer1, grid) == true) ||              // sunk ship from current hit condition
                    (destroyer2.Sunk == false && Ship.CriticalHit(destroyer2, grid) == true) ||              // sunk ship from current hit condition
                    (battleship1.Sunk == false && Ship.CriticalHit(battleship1, grid) == true)               // sunk ship from current hit condition
                    )
                {
                    if (destroyer1.Sunk == false && Ship.CriticalHit(destroyer1, grid) == true)
                    {
                        destroyer1.Sunk = true;
                        topMessage      = feedbackSunk;
                    }
                    else if (destroyer2.Sunk == false && Ship.CriticalHit(destroyer2, grid) == true)
                    {
                        destroyer2.Sunk = true;
                        topMessage      = feedbackSunk;
                    }
                    else if (battleship1.Sunk == false && Ship.CriticalHit(battleship1, grid) == true)
                    {
                        battleship1.Sunk = true;
                        topMessage       = feedbackSunk;
                    }
                }
                else
                {
                    topMessage = feedbackHit;           // Hit without Sink
                }
            }

            if (IsGameWon(destroyer1, destroyer2, battleship1))
            {
                constantMessage        = "Well done! You completed the game in " + (totalAttempts + 1) + " shots";     // game won - final message
                topMessage             = feedbackSunk;
                grid.AllShipsDestroyed = true;
            }

            Console.Clear();
            Console.WriteLine(topMessage);
            Console.WriteLine();
            Grid.PrintGrid(grid);
            Console.WriteLine();
            Console.WriteLine(constantMessage);

            return(grid);
        }