/// <summary>
        /// Checks if parsed boats are correct and connects them to the BattleField in the case of success
        /// </summary>
        /// <param name="p_message">Error string</param>
        /// <returns>returns true if arrangement was correct</returns>
        public bool CheckArrangement(out string p_message)
        {
            // 3. Check if Boats are correct

            bool _result = true;

            //first check quantity

            if (Boats != null)
            {
                if (Boats.All(b => (b.Cells.Select(c => c.x).Distinct().Count() == 1) ||
                              (b.Cells.Select(c => c.y).Distinct().Count() == 1)
                              )
                    )
                {
                    if (Boats.Count == 10)
                    {
                        if (Boats.All(a => a.Cells.Count <= 4))
                        {
                            if (
                                Boats.Count(a => a.Cells.Count == 4) == 1 &&
                                Boats.Count(a => a.Cells.Count == 3) == 2 &&
                                Boats.Count(a => a.Cells.Count == 2) == 3 &&
                                Boats.Count(a => a.Cells.Count == 1) == 4
                                )
                            {
                                _result   = true;
                                p_message = "The Boats arranged correctly";
                            }
                            else
                            {
                                _result   = false;
                                p_message = "Wrong set of Boats";
                            }
                        }
                        else
                        {
                            _result   = false;
                            p_message = "Some Boats are too long";
                        }
                    }
                    else
                    {
                        _result   = false;
                        p_message = "Incorrect quantity of Boats";
                    }
                }
                else
                {
                    _result   = false;
                    p_message = "A Boat can't bend or touch an other Boat diagonally";
                }
            }
            else
            {
                throw new Exception("Before Check the Boats should be parsed");
            }


            if (_result)
            {
                foreach (var boat in Boats)
                {
                    this.ConnectBoatToBF(boat);
                }
            }
            return(_result);
        }
Exemple #2
0
        public string GetFiredAtMessage(string input)
        {
            input = input.Split(' ')[1].ToUpper();
            var boatName = "";
            var boatEnum = 0;
            var sunk     = true;

            if (!GetFiredAt(input))
            {
                return(StatusCode.Miss.GetDescription());
            }

            foreach (var boat in Boats)
            {
                foreach (var coor in boat.Coordinates)
                {
                    if (coor.Key == input)
                    {
                        boatName = boat.Name;
                    }
                }
            }

            foreach (var boat in Boats)
            {
                foreach (var coor in boat.Coordinates)
                {
                    if (boat.Name == boatName && coor.Value == false)
                    {
                        sunk = false;
                    }
                }

                if (boat.Name == boatName && sunk)
                {
                    boat.Alive = false;
                }
            }

            if (Boats.All(x => x.Alive == false))
            {
                // WON
                return(StatusCode.YouWin.GetDescription());
            }


            // SUNK
            if (sunk)
            {
                switch (boatName)
                {
                case "Carrier":
                    boatEnum = 251;
                    break;

                case "Battleship":
                    boatEnum = 252;
                    break;

                case "Destroyer":
                    boatEnum = 253;
                    break;

                case "Submarine":
                    boatEnum = 254;
                    break;

                case "Patrol Boat":
                    boatEnum = 255;
                    break;
                }
            }
            else
            {
                // HIT
                switch (boatName)
                {
                case "Carrier":
                    boatEnum = 241;
                    break;

                case "Battleship":
                    boatEnum = 242;
                    break;

                case "Destroyer":
                    boatEnum = 243;
                    break;

                case "Submarine":
                    boatEnum = 244;
                    break;

                case "Patrol Boat":
                    boatEnum = 245;
                    break;
                }
            }

            var myEnum = (StatusCode)boatEnum;

            return(myEnum.GetDescription());
        }