Пример #1
0
        private Ship MakeShip(int size, int shipNumb)
        {
            Random rnd = new Random();
            bool   isFit = false;
            int    x = 0, y = 0;
            bool   isVertical = Convert.ToBoolean(rnd.Next(2));

            while (!isFit)
            {
                x = rnd.Next(10);
                y = rnd.Next(10);

                int coord = isVertical ? x : y;
                isFit = true;
                if (coord + size > BoardSize.Height)
                {
                    x = isVertical ? BoardSize.TopEdge : x;
                    y = isVertical ? y : BoardSize.LeftEdge;
                }
                for (int i = 0; i < size; i++)
                {
                    int px = isVertical ? i : BoardSize.TopEdge;
                    int py = isVertical ? BoardSize.LeftEdge : i;
                    if (Board.GetField(x + px, y + py) != (int)Marker.EmptyField)
                    {
                        isFit = false;
                        break;
                    }
                }
            }
            return(new Ship(x, y, size, shipNumb, isVertical));
        }
Пример #2
0
 public void PrepareToAdd()
 {
     _coveringFieldsAddingShip = new int[_sizeOfCurrentAddingShip];
     for (int i = 0, j = 0; i < _sizeOfCurrentAddingShip && j < _sizeOfCurrentAddingShip;)
     {
         if (Board.GetField(_coordXAddingShip + j, _coordYAddingShip + i) == (int)Marker.EmptyField)
         {
             Board.SetField(_coordXAddingShip + j, _coordYAddingShip + i, (int)Marker.ChosenToAdd);
         }
         else
         {
             int changedFiled = _isVerticalAddingShip ? j : i;
             _coveringFieldsAddingShip[changedFiled] = Board.GetField(_coordXAddingShip + j, _coordYAddingShip + i);
             Board.SetField(_coordXAddingShip + j, _coordYAddingShip + i, (int)Marker.CannotAdd);
             _isFitAddingShip = false;
         }
         if (_isVerticalAddingShip)
         {
             ++j;
         }
         else
         {
             ++i;
         }
     }
 }
Пример #3
0
        public EasyAI(Board userBoard)
        {
            _userBoard = userBoard;

            _fieldList = new List <Field>();
            for (int y = 0; y < _userBoard.Height; y++)
            {
                for (int x = 0; x < _userBoard.Width; x++)
                {
                    Coordinate coordinate = new Coordinate(x, y);
                    _fieldList.Add(_userBoard.GetField(coordinate));
                }
            }
        }
Пример #4
0
 private bool WantAdd()
 {
     for (int i = 0, j = 0; i < _sizeOfCurrentAddingShip && j < _sizeOfCurrentAddingShip;)
     {
         int changedFiled = _isVerticalAddingShip ? j : i;
         int previousVal  = Board.GetField(_coordXAddingShip + j, _coordYAddingShip + i) == (int)Marker.CannotAdd ? _coveringFieldsAddingShip[changedFiled] : (int)Marker.EmptyField;
         Board.SetField(_coordXAddingShip + j, _coordYAddingShip + i, previousVal);
         if (_isVerticalAddingShip)
         {
             ++j;
         }
         else
         {
             ++i;
         }
     }
     return(ReadKey());
 }
Пример #5
0
        public MediumAI(Board userBoard)
        {
            _userBoard = userBoard;

            _easyAI = new EasyAI(userBoard);
            _hardAI = new HardAI(userBoard);

            _fieldList = new List <Field>();
            for (int y = 0; y < _userBoard.Height; y++)
            {
                for (int x = 0; x < _userBoard.Width; x++)
                {
                    Coordinate coordinate = new Coordinate(x, y);
                    _fieldList.Add(_userBoard.GetField(coordinate));
                }
            }

            _difficltyToUse = new List <Difficulty>();
            _difficltyToUse.Add(Difficulty.Hard);
            _difficltyToUse.Add(Difficulty.Hard);
            _difficltyToUse.Add(Difficulty.Hard);
            _difficltyToUse.Add(Difficulty.Hard);
            _difficltyToUse.Add(Difficulty.Easy);
        }
Пример #6
0
        public Field Hit()
        {
            // Creating lists of fields that contain fields that were not hit
            // and fields that were hit and contain ships or water
            _notHitFieldsList = new List <Field>();
            _hitShipList      = new List <Field>();
            _hitWaterList     = new List <Field>();
            foreach (Field field in _fieldList)
            {
                if (field.IsHit == false)
                {
                    _notHitFieldsList.Add(field);
                }

                if (field.IsHit == true && field.Ship != null)
                {
                    _hitShipList.Add(field);
                }
                else if (field.IsHit == true && field.Ship == null)
                {
                    _hitWaterList.Add(field);
                }
            }

            // Creating list that contains of ships that were destroyed
            _destroyedShipsList = new List <Ship>();
            foreach (Field field in _hitShipList)
            {
                Ship shipInField = field.Ship;

                if (shipInField.Size == 0 && _destroyedShipsList.Contains(shipInField) == false)
                {
                    _destroyedShipsList.Add(shipInField);
                }
            }

            // Creating list that contains the fields that may contain the rest of any ship
            _mayHaveRestOfShip = new List <Field>();
            foreach (Field field in _hitShipList)
            {
                if (field.Ship.Size != 0)
                {
                    List <Field> shipFields = new List <Field>();
                    shipFields.Add(field);

                    for (int i = 0; i < shipFields.Count; i++)
                    {
                        List <Field> surroundingFields = GetSurroundingFields(shipFields[i]);

                        foreach (Field surrounding in surroundingFields)
                        {
                            if (surrounding.IsHit == true && surrounding.Ship != null && shipFields.Contains(surrounding) == false)
                            {
                                shipFields.Add(surrounding);
                            }
                        }
                    }

                    if (shipFields.Count > 1)
                    {
                        Direction  direction;
                        Coordinate coordinateField1 = _userBoard.GetCoordinates(shipFields[0]);
                        Coordinate coordinateField2 = _userBoard.GetCoordinates(shipFields[1]);

                        if (coordinateField1.X - coordinateField2.X == 0)
                        {
                            direction = Direction.Vertical;
                        }
                        else
                        {
                            direction = Direction.Horizontal;
                        }

                        Field beginningOfShip = field; // By default
                        if (direction.Equals(Direction.Horizontal))
                        {
                            int minX = _userBoard.GetCoordinates(shipFields[0]).X;
                            int y    = _userBoard.GetCoordinates(shipFields[0]).Y;
                            for (int i = 0; i < shipFields.Count; i++)
                            {
                                int x = _userBoard.GetCoordinates(shipFields[i]).X;
                                if (x < minX)
                                {
                                    minX = x;
                                }
                            }
                            beginningOfShip = _userBoard.GetField(new Coordinate(minX, y));
                        }
                        else if (direction.Equals(Direction.Vertical))
                        {
                            int x    = _userBoard.GetCoordinates(shipFields[0]).X;
                            int minY = _userBoard.GetCoordinates(shipFields[0]).Y;
                            for (int i = 0; i < shipFields.Count; i++)
                            {
                                int y = _userBoard.GetCoordinates(shipFields[i]).Y;
                                if (y < minY)
                                {
                                    minY = y;
                                }
                            }
                            beginningOfShip = _userBoard.GetField(new Coordinate(x, minY));
                        }

                        Field endOfShip = field; // By default
                        if (direction.Equals(Direction.Horizontal))
                        {
                            int maxX = _userBoard.GetCoordinates(shipFields[0]).X;
                            int y    = _userBoard.GetCoordinates(shipFields[0]).Y;
                            for (int i = 0; i < shipFields.Count; i++)
                            {
                                int x = _userBoard.GetCoordinates(shipFields[i]).X;
                                if (x > maxX)
                                {
                                    maxX = x;
                                }
                            }
                            endOfShip = _userBoard.GetField(new Coordinate(maxX, y));
                        }
                        else if (direction.Equals(Direction.Vertical))
                        {
                            int x    = _userBoard.GetCoordinates(shipFields[0]).X;
                            int maxY = _userBoard.GetCoordinates(shipFields[0]).Y;
                            for (int i = 0; i < shipFields.Count; i++)
                            {
                                int y = _userBoard.GetCoordinates(shipFields[i]).Y;
                                if (y > maxY)
                                {
                                    maxY = y;
                                }
                            }
                            endOfShip = _userBoard.GetField(new Coordinate(x, maxY));
                        }

                        if (direction.Equals(Direction.Horizontal))
                        {
                            Field leftField = GetLeftField(beginningOfShip);
                            if (_hitShipList.Contains(GetLeftField(leftField)) == false && _hitShipList.Contains(GetDownField(leftField)) == false && _hitShipList.Contains(GetUpField(leftField)) == false)
                            {
                                if (_notHitFieldsList.Contains(leftField) == true)
                                {
                                    if (_mayHaveRestOfShip.Contains(leftField) == false)
                                    {
                                        _mayHaveRestOfShip.Add(leftField);
                                    }
                                }
                            }

                            Field rightField = GetRightField(endOfShip);
                            if (_hitShipList.Contains(GetRightField(rightField)) == false && _hitShipList.Contains(GetDownField(rightField)) == false && _hitShipList.Contains(GetUpField(rightField)) == false)
                            {
                                if (_notHitFieldsList.Contains(rightField) == true)
                                {
                                    if (_mayHaveRestOfShip.Contains(rightField) == false)
                                    {
                                        _mayHaveRestOfShip.Add(rightField);
                                    }
                                }
                            }
                        }
                        else if (direction.Equals(Direction.Vertical))
                        {
                            Field upField = GetDownField(beginningOfShip);
                            if (_hitShipList.Contains(GetDownField(upField)) == false && _hitShipList.Contains(GetLeftField(upField)) == false && _hitShipList.Contains(GetRightField(upField)) == false)
                            {
                                if (_notHitFieldsList.Contains(upField) == true)
                                {
                                    if (_mayHaveRestOfShip.Contains(upField) == false)
                                    {
                                        _mayHaveRestOfShip.Add(upField);
                                    }
                                }
                            }

                            Field downField = GetUpField(endOfShip);
                            if (_hitShipList.Contains(GetUpField(downField)) == false && _hitShipList.Contains(GetLeftField(downField)) == false && _hitShipList.Contains(GetRightField(downField)) == false)
                            {
                                if (_notHitFieldsList.Contains(downField) == true)
                                {
                                    if (_mayHaveRestOfShip.Contains(downField) == false)
                                    {
                                        _mayHaveRestOfShip.Add(downField);
                                    }
                                }
                            }
                        }
                    }
                    else // if ship size is 1
                    {
                        Field leftField = GetLeftField(field);
                        if (_hitShipList.Contains(GetLeftField(leftField)) == false && _hitShipList.Contains(GetDownField(leftField)) == false && _hitShipList.Contains(GetUpField(leftField)) == false)
                        {
                            if (_notHitFieldsList.Contains(leftField) == true)
                            {
                                if (_mayHaveRestOfShip.Contains(leftField) == false)
                                {
                                    _mayHaveRestOfShip.Add(leftField);
                                }
                            }
                        }

                        Field rightField = GetRightField(field);
                        if (_hitShipList.Contains(GetRightField(rightField)) == false && _hitShipList.Contains(GetDownField(rightField)) == false && _hitShipList.Contains(GetUpField(rightField)) == false)
                        {
                            if (_notHitFieldsList.Contains(rightField) == true)
                            {
                                if (_mayHaveRestOfShip.Contains(rightField) == false)
                                {
                                    _mayHaveRestOfShip.Add(rightField);
                                }
                            }
                        }

                        Field upField = GetUpField(field);
                        if (_hitShipList.Contains(GetUpField(upField)) == false && _hitShipList.Contains(GetLeftField(upField)) == false && _hitShipList.Contains(GetRightField(upField)) == false)
                        {
                            if (_notHitFieldsList.Contains(upField) == true)
                            {
                                if (_mayHaveRestOfShip.Contains(upField) == false)
                                {
                                    _mayHaveRestOfShip.Add(upField);
                                }
                            }
                        }

                        Field downField = GetDownField(field);
                        if (_hitShipList.Contains(GetDownField(downField)) == false && _hitShipList.Contains(GetLeftField(downField)) == false && _hitShipList.Contains(GetRightField(downField)) == false)
                        {
                            if (_notHitFieldsList.Contains(downField) == true)
                            {
                                if (_mayHaveRestOfShip.Contains(downField) == false)
                                {
                                    _mayHaveRestOfShip.Add(downField);
                                }
                            }
                        }
                    }
                }
            }

            // Creating list of fields that may contain fields with new ships
            _mayHaveShipsAnySize = new List <Field>();

            foreach (Field field in _notHitFieldsList)
            {
                List <Field> surroundingFields = GetSurroundingFields(field);

                bool isShipAround = false;

                foreach (Field surrounding in surroundingFields)
                {
                    if (_hitShipList.Contains(surrounding))
                    {
                        isShipAround = true;
                    }
                }

                if (isShipAround == false)
                {
                    _mayHaveShipsAnySize.Add(field);
                }
            }

            // Creating list of fields that may contain fields with ships of size 2, 3, 4, 5
            _mayHaveShipsSize2 = GetMayHaveShips(2);
            _mayHaveShipsSize3 = GetMayHaveShips(3);
            _mayHaveShipsSize4 = GetMayHaveShips(4);
            _mayHaveShipsSize5 = GetMayHaveShips(5);

            Random rand = new Random();

            if (_mayHaveRestOfShip.Count > 0)
            {
                Field field = _mayHaveRestOfShip[rand.Next(0, _mayHaveRestOfShip.Count)];
                return(field);
            }
            else if (_mayHaveShipsSize5.Count > 0)
            {
                Field field = _mayHaveShipsSize5[rand.Next(0, _mayHaveShipsSize5.Count)];
                return(field);
            }
            else if (_mayHaveShipsSize4.Count > 0)
            {
                Field field = _mayHaveShipsSize4[rand.Next(0, _mayHaveShipsSize4.Count)];
                return(field);
            }
            else if (_mayHaveShipsSize3.Count > 0)
            {
                Field field = _mayHaveShipsSize3[rand.Next(0, _mayHaveShipsSize3.Count)];
                return(field);
            }
            else if (_mayHaveShipsSize2.Count > 0)
            {
                Field field = _mayHaveShipsSize2[rand.Next(0, _mayHaveShipsSize2.Count)];
                return(field);
            }
            else
            {
                Field field = _mayHaveShipsAnySize[rand.Next(0, _mayHaveShipsAnySize.Count)];
                return(field);
            }
        }