Example #1
0
 private void ComputerSeaFight_MouseUp(object sender, MouseEventArgs e)
 {
     if (e.Button == MouseButtons.Left && game.WhoWalk == WW.Player && game.GameStage == GS.Play)
     {
         Point pos = new Point(e.X, e.Y);
         if (computerSeaFight.Fire(pos) < 1)
         {
             game.WhoWalk = WW.Computer;
         }
         if (game.WhoWalk == WW.Computer && !computerSeaFight.isWin)
         {
             timer1.Start();
         }
         else if (computerSeaFight.isWin)
         {
             game.GameStage = GS.End;
             addWinnerIntoDB(nicknameBox.Text);
             MessageBox.Show("Вы выиграли!");
         }
     }
 }
Example #2
0
        public bool ComputerMove(SeaFight Target)
        {
            Cell cell;

            if (!isTargetAcquired)
            {
                movePositions         = Enum.GetValues(typeof(MovePosition)).Cast <MovePosition>().Where(v => v != availablemovePosition).ToList();
                availablemovePosition = movePositions[rnd.Next(0, movePositions.Count)];
                tempPosition          = availablemovePosition;
                int xMin = 0, xMax = 0, yMin = 0, yMax = 0;
                switch (availablemovePosition)
                {
                case MovePosition.TOPLEFTCORNER:
                    xMin = 0;
                    xMax = 4;
                    yMin = 0;
                    yMax = 4;
                    break;

                case MovePosition.TOPRIGHTCORNER:
                    xMin = 5;
                    xMax = 9;
                    yMin = 0;
                    yMax = 4;
                    break;

                case MovePosition.BOTTOMLEFTCORNER:
                    xMin = 0;
                    xMax = 4;
                    yMin = 5;
                    yMax = 9;
                    break;

                case MovePosition.BOTTOMRIGHTCORNER:
                    xMin = 5;
                    xMax = 9;
                    yMin = 5;
                    yMax = 9;
                    break;
                }
                List <Cell> availableCells = cells.Where(v => v.position.X >= xMin && v.position.X <= xMax && v.position.Y >= yMin &&
                                                         v.position.Y <= yMax && v.type != TypeCell.Wounded && v.type != TypeCell.Miss).Cast <Cell>().ToList();
                while (availableCells.Count == 0)
                {
                    movePositions = movePositions.Where(v => v.ToString() != availablemovePosition.ToString()).ToList();
                    if (movePositions.Count == 0)
                    {
                        availablemovePosition = tempPosition;
                    }
                    else
                    {
                        availablemovePosition = movePositions[rnd.Next(0, movePositions.Count)];
                    }
                    xMin = xMax = yMin = yMax = 0;
                    switch (availablemovePosition)
                    {
                    case MovePosition.TOPLEFTCORNER:
                        xMin = 0;
                        xMax = 4;
                        yMin = 0;
                        yMax = 4;
                        break;

                    case MovePosition.TOPRIGHTCORNER:
                        xMin = 5;
                        xMax = 9;
                        yMin = 0;
                        yMax = 4;
                        break;

                    case MovePosition.BOTTOMLEFTCORNER:
                        xMin = 0;
                        xMax = 4;
                        yMin = 5;
                        yMax = 9;
                        break;

                    case MovePosition.BOTTOMRIGHTCORNER:
                        xMin = 5;
                        xMax = 9;
                        yMin = 5;
                        yMax = 9;
                        break;
                    }
                    availableCells = cells.Where(v => v.position.X >= xMin && v.position.X <= xMax && v.position.Y >= yMin &&
                                                 v.position.Y <= yMax && v.type != TypeCell.Wounded && v.type != TypeCell.Miss).Cast <Cell>().ToList();
                }
                cell           = availableCells[rnd.Next(0, availableCells.Count)];
                attackPosition = cell.position;
            }
            else
            {
                if (leftMove)
                {
                    prevAttackPosition = new Point(prevAttackPosition.X - 1, prevAttackPosition.Y);
                    cell = cells.FirstOrDefault(c => c.position == prevAttackPosition);
                    if (cell == null || cell.type == TypeCell.Miss)
                    {
                        leftMove           = false;
                        rightMove          = true;
                        prevAttackPosition = targetPosition;
                    }
                }
                if (rightMove)
                {
                    prevAttackPosition = new Point(prevAttackPosition.X + 1, prevAttackPosition.Y);
                    cell = cells.FirstOrDefault(c => c.position == prevAttackPosition);
                    if (cell == null || cell.type == TypeCell.Miss)
                    {
                        rightMove          = false;
                        upMove             = true;
                        prevAttackPosition = targetPosition;
                    }
                }
                if (upMove)
                {
                    prevAttackPosition = new Point(prevAttackPosition.X, prevAttackPosition.Y - 1);
                    cell = cells.FirstOrDefault(c => c.position == prevAttackPosition);
                    if (cell == null || cell.type == TypeCell.Miss)
                    {
                        upMove             = false;
                        downMove           = true;
                        prevAttackPosition = targetPosition;
                    }
                }
                if (downMove)
                {
                    prevAttackPosition = new Point(prevAttackPosition.X, prevAttackPosition.Y + 1);
                    cell = cells.FirstOrDefault(c => c.position == prevAttackPosition);
                    if (cell == null || cell.type == TypeCell.Miss)
                    {
                        downMove           = false;
                        leftMove           = true;
                        prevAttackPosition = targetPosition;
                    }
                }
                attackPosition = prevAttackPosition;
            }
            int result = Target.Fire(attackPosition);

            if (result == 1 && !isTargetAcquired)
            {
                isTargetAcquired   = true;
                leftMove           = true;
                rightMove          = false;
                upMove             = false;
                downMove           = false;
                targetPosition     = attackPosition;
                prevAttackPosition = attackPosition;
            }
            else if (result == 2)
            {
                isTargetAcquired = false;
            }
            else if (result == 0 && isTargetAcquired)
            {
                if (leftMove)
                {
                    leftMove           = false;
                    rightMove          = true;
                    prevAttackPosition = targetPosition;
                }
                else if (rightMove)
                {
                    rightMove          = false;
                    upMove             = true;
                    prevAttackPosition = targetPosition;
                }
                else if (upMove)
                {
                    upMove             = false;
                    downMove           = true;
                    prevAttackPosition = targetPosition;
                }
            }
            return(result >= 1 ? true : false);
        }