public void Execute(object parameter)
 {
     if (_Cell.ParentBattleField.Game.Stage == GameStage.BoatsArrange)
     {
         if (_Cell.ParentBattleField is UserBattleField)
         {
             if (_MB == MouseButton.Left)
             {
                 _Cell.DrawCell();
             }
             else if (_MB == MouseButton.Right)
             {
                 _Cell.RemoveCell();
             }
         }
     }
     else if (_Cell.ParentBattleField.Game.Stage == GameStage.Playing)
     {
         if (_Cell.ParentBattleField is EnemyBattleField)
         {
             if (_MB == MouseButton.Left && _Cell.ParentBattleField.Game.CurrentActor == Actor.User)
             {
                 _Cell.Shoot();
             }
         }
     }
 }
Beispiel #2
0
        public void ShootToUserBFAutomatically()
        {
            int _targetX = 0;
            int _targetY = 0;

            //have we already knew anything
            List <Cell> _knownCells = new List <Cell>();

            for (int i = 0; i < UserBattleField.DefaultFieldSize; i++)
            {
                for (int j = 0; j < UserBattleField.DefaultFieldSize; j++)
                {
                    if (UserBattleField.Cells[i, j].Style == CellStyle.Shooted || UserBattleField.Cells[i, j].Style == CellStyle.WoundedCell || UserBattleField.Cells[i, j].Style == CellStyle.DeadCell)
                    {
                        _knownCells.Add(UserBattleField.Cells[i, j]);
                    }
                }
            }

            //looking for coordinates

            if (_knownCells.Count == 0)  //this is the first shoot
            {
                #region
                //we start from any corner
                int corner    = r.Next(3);
                int padding_x = r.Next(100) % 2;
                int padding_y = r.Next(100) % 2;

                if (corner == 0)//NW
                {
                    _targetX = 0 + padding_x;
                    _targetY = 0 + padding_y;
                }
                else if (corner == 1)//NE
                {
                    _targetX = 9 - padding_x;
                    _targetY = 0 + padding_y;
                }
                else if (corner == 2)//SW
                {
                    _targetX = 0 + padding_x;
                    _targetY = 9 - padding_y;
                }
                else if (corner == 3)//SE
                {
                    _targetX = 9 - padding_x;
                    _targetY = 9 - padding_y;
                }
                #endregion
            }

            else
            {
                List <Cell> woundedCells = (from cell in _knownCells where cell.Style == CellStyle.WoundedCell select cell).ToList();

                if (woundedCells.Count() > 0)   //finish him!
                {
                    #region
                    if (woundedCells.Count > 1)
                    {
                        //where is the line
                        if ((from cell in woundedCells select cell.x).Distinct().Count() == 1)//line is vertical
                        {
                            int  searchDirection  = 0;
                            bool IsNextShootFound = false;

                            while (searchDirection < 2 && !IsNextShootFound)
                            {
                                if (searchDirection == 0)//to the east
                                {
                                    _targetY = (from cell in woundedCells select cell.y).Max() + 1;
                                    _targetX = woundedCells[0].x;
                                }
                                else //to the west
                                {
                                    _targetY = (from cell in woundedCells select cell.y).Min() - 1;
                                    _targetX = woundedCells[0].x;
                                }

                                if (_targetX >= 0 &&
                                    _targetX <= 9 &&
                                    _targetY >= 0 &&
                                    _targetY <= 9)
                                {
                                    if (UserBattleField.Cells[_targetX, _targetY].Style == CellStyle.HealthyCell ||
                                        UserBattleField.Cells[_targetX, _targetY].Style == CellStyle.Empty)
                                    {
                                        IsNextShootFound = true;
                                    }
                                }

                                searchDirection++;
                            }
                        }
                        else if ((from cell in woundedCells select cell.y).Distinct().Count() == 1)//line is horisontal
                        {
                            int  searchDirection  = 0;
                            bool IsNextShootFound = false;

                            while (searchDirection < 2 && !IsNextShootFound)
                            {
                                if (searchDirection == 0)//to the nord
                                {
                                    _targetX = (from cell in woundedCells select cell.x).Max() + 1;
                                    _targetY = woundedCells[0].y;
                                }
                                else //to the south
                                {
                                    _targetX = (from cell in woundedCells select cell.x).Min() - 1;
                                    _targetY = woundedCells[0].y;
                                }

                                if (_targetX >= 0 &&
                                    _targetX <= 9 &&
                                    _targetY >= 0 &&
                                    _targetY <= 9)
                                {
                                    if (UserBattleField.Cells[_targetX, _targetY].Style == CellStyle.HealthyCell ||
                                        UserBattleField.Cells[_targetX, _targetY].Style == CellStyle.Empty)
                                    {
                                        IsNextShootFound = true;
                                    }
                                }

                                searchDirection++;
                            }
                        }
                    }
                    else
                    {
                        int  searchDirection  = 0;
                        bool IsNextShootFound = false;
                        Cell selectedCell     = woundedCells[0];

                        while (searchDirection < 4 && !IsNextShootFound)
                        {
                            if (searchDirection == 0)//nord
                            {
                                _targetX = selectedCell.x;
                                _targetY = selectedCell.y - 1;
                            }
                            else if (searchDirection == 1)//east
                            {
                                _targetX = selectedCell.x + 1;
                                _targetY = selectedCell.y;
                            }
                            else if (searchDirection == 2)//south
                            {
                                _targetX = selectedCell.x;
                                _targetY = selectedCell.y + 1;
                            }
                            else if (searchDirection == 3)//west
                            {
                                _targetX = selectedCell.x - 1;
                                _targetY = selectedCell.y;
                            }

                            if (_targetX >= 0 &&
                                _targetX <= 9 &&
                                _targetY >= 0 &&
                                _targetY <= 9)
                            {
                                if (UserBattleField.Cells[_targetX, _targetY].Style == CellStyle.HealthyCell ||
                                    UserBattleField.Cells[_targetX, _targetY].Style == CellStyle.Empty)
                                {
                                    IsNextShootFound = true;
                                }
                            }

                            searchDirection++;
                        }
                    }

                    #endregion
                }
                else //hunter mode
                {
                    //we are looking for the biggest alive ship
                    int maxLenght =
                        (from boat in UserBattleField.Boats where boat.Cells.Any(c => c.Style == CellStyle.HealthyCell) select boat.Cells.Count).Max();

                    if (maxLenght > 1)
                    {
                        //find an area longer than victim's length

                        bool IsVerticalSearch = Convert.ToBoolean(r.Next(100) % 2);
                        bool IsReverseSearch  = Convert.ToBoolean(r.Next(50) % 2);

                        List <Cell> LongLine        = new List <Cell>();
                        bool        IsLongLineFound = false;
                        #region
                        while (!IsLongLineFound)
                        {
                            if (!IsVerticalSearch)//horisontal lines
                            {
                                if (!IsReverseSearch)
                                {
                                    int i = 0;
                                    int j = 0;

                                    while (i < 10 && j < 10 && !IsLongLineFound)
                                    {
                                        if (UserBattleField.Cells[i, j].Style == CellStyle.HealthyCell || UserBattleField.Cells[i, j].Style == CellStyle.Empty)
                                        {
                                            LongLine.Add(UserBattleField.Cells[i, j]);
                                        }
                                        else
                                        {
                                            if (LongLine.Count >= maxLenght)
                                            {
                                                IsLongLineFound = true;
                                            }
                                            else
                                            {
                                                LongLine = new List <Cell>();
                                            }
                                        }

                                        i++;

                                        if (i > 9)
                                        {
                                            i = 0;
                                            j++;
                                            if (LongLine.Count >= maxLenght)
                                            {
                                                IsLongLineFound = true;
                                            }
                                            else
                                            {
                                                LongLine = new List <Cell>();
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    int i = 9;
                                    int j = 9;
                                    while (i >= 0 && j >= 0 && !IsLongLineFound)
                                    {
                                        if (UserBattleField.Cells[i, j].Style == CellStyle.HealthyCell || UserBattleField.Cells[i, j].Style == CellStyle.Empty)
                                        {
                                            LongLine.Add(UserBattleField.Cells[i, j]);
                                        }
                                        else
                                        {
                                            if (LongLine.Count >= maxLenght)
                                            {
                                                IsLongLineFound = true;
                                            }
                                            else
                                            {
                                                LongLine = new List <Cell>();
                                            }
                                        }

                                        i--;

                                        if (i < 0)
                                        {
                                            i = 9;
                                            j--;
                                            if (LongLine.Count >= maxLenght)
                                            {
                                                IsLongLineFound = true;
                                            }
                                            else
                                            {
                                                LongLine = new List <Cell>();
                                            }
                                        }
                                    }
                                }

                                IsVerticalSearch = !IsVerticalSearch;
                            }

                            else //vertical lines
                            {
                                if (!IsReverseSearch)
                                {
                                    int i = 0;
                                    int j = 0;

                                    while (i < 10 && j < 10 && !IsLongLineFound)
                                    {
                                        if (UserBattleField.Cells[i, j].Style == CellStyle.HealthyCell || UserBattleField.Cells[i, j].Style == CellStyle.Empty)
                                        {
                                            LongLine.Add(UserBattleField.Cells[i, j]);
                                        }
                                        else
                                        {
                                            if (LongLine.Count >= maxLenght)
                                            {
                                                IsLongLineFound = true;
                                            }
                                            else
                                            {
                                                LongLine = new List <Cell>();
                                            }
                                        }

                                        j++;

                                        if (j > 9)
                                        {
                                            j = 0;
                                            i++;
                                            if (LongLine.Count >= maxLenght)
                                            {
                                                IsLongLineFound = true;
                                            }
                                            else
                                            {
                                                LongLine = new List <Cell>();
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    int i = 9;
                                    int j = 9;
                                    while (i >= 0 && j >= 0 && !IsLongLineFound)
                                    {
                                        if (UserBattleField.Cells[i, j].Style == CellStyle.HealthyCell || UserBattleField.Cells[i, j].Style == CellStyle.Empty)
                                        {
                                            LongLine.Add(UserBattleField.Cells[i, j]);
                                        }
                                        else
                                        {
                                            if (LongLine.Count >= maxLenght)
                                            {
                                                IsLongLineFound = true;
                                            }
                                            else
                                            {
                                                LongLine = new List <Cell>();
                                            }
                                        }

                                        j--;

                                        if (j < 0)
                                        {
                                            j = 9;
                                            i--;
                                            if (LongLine.Count >= maxLenght)
                                            {
                                                IsLongLineFound = true;
                                            }
                                            else
                                            {
                                                LongLine = new List <Cell>();
                                            }
                                        }
                                    }
                                }
                            }

                            IsVerticalSearch = !IsVerticalSearch;
                        }
                        #endregion
                        _targetX = LongLine[maxLenght - 1].x;
                        _targetY = LongLine[maxLenght - 1].y;
                    }
                    else
                    {
                        List <Cell> possibleVictim = new List <Cell>();
                        for (int i = 0; i < UserBattleField.DefaultFieldSize; i++)
                        {
                            for (int j = 0; j < UserBattleField.DefaultFieldSize; j++)
                            {
                                if (UserBattleField.Cells[i, j].Style == CellStyle.Empty || UserBattleField.Cells[i, j].Style == CellStyle.HealthyCell)
                                {
                                    possibleVictim.Add(UserBattleField.Cells[i, j]);
                                }
                            }
                        }
                        if (possibleVictim.Count > 0)
                        {
                            int victimNumber = r.Next(possibleVictim.Count - 1);
                            _targetX = possibleVictim[victimNumber].x;
                            _targetY = possibleVictim[victimNumber].y;
                        }
                    }
                }
            }


            //a shoot itself
            Cell shootedCell = UserBattleField.Cells[_targetX, _targetY];
            shootedCell.Shoot();
        }