Ejemplo n.º 1
0
 private void BtnNewGame_Click(object sender, EventArgs e)
 {
     Count1     = 4;
     Count2     = 3;
     Count3     = 2;
     Count4     = 1;
     TotalCount = 0;
     ShipIsHorisontal.Checked = true;
     Btn1cells.Checked        = true;
     orientation              = 1;
     dataGridView2.Visible    = false;
     GroupBox.Visible         = true;
     ShipIsHorisontal.Visible = true;
     ShipIsVertical.Visible   = true;
     Sea.ClearSea(FriendlySea);
     Sea.ClearSea(EnemySea);
     UpdateSea(FriendlySea, dataGridView1, true);
     UpdateSea(EnemySea, dataGridView2, false);
     ships1.Text           = Convert.ToString(Count1);
     ships2.Text           = Convert.ToString(Count2);
     ships3.Text           = Convert.ToString(Count3);
     ships4.Text           = Convert.ToString(Count4);
     EndText.Visible       = false;
     BtnNewGame.Visible    = false;
     BtnClean.Visible      = true;
     dataGridView1.Enabled = true;
     dataGridView2.Enabled = true;
 }
Ejemplo n.º 2
0
 public Mission(Sea sea)
 {
     this.sea = sea;
     rand     = new Random();
     map      = new int[Sea.size_sea.x, Sea.size_sea.y];
     put      = new int[Sea.size_sea.x, Sea.size_sea.y];
     Reset();
 }
Ejemplo n.º 3
0
 public static bool Shot(int coordx, int coordy, Sea sea)
 {
     if (sea.Cells[coordx, coordy].status == (int)Cell.Status.Empty || sea.Cells[coordx, coordy].status == (int)Cell.Status.Blocked)
     {
         sea.Cells[coordx, coordy].status = (int)Cell.Status.Missed;
         return(false);
     }
     else
     {
         sea.Cells[coordx, coordy].status = (int)Cell.Status.Damaged;
         sea.Cells[coordx, coordy].ship.ShipIsDamaged();
         if (sea.Cells[coordx, coordy].ship.Health == 0)
         {
             sea.ShipsCount--;
             if (sea.Cells[coordx, coordy].ship.orientation == 0)
             {
                 for (int x = sea.Cells[coordx, coordy].ship.coordx - 1; x <= sea.Cells[coordx, coordy].ship.coordx + sea.Cells[coordx, coordy].ship.Size; x++)
                 {
                     for (int y = sea.Cells[coordx, coordy].ship.coordy - 1; y <= sea.Cells[coordx, coordy].ship.coordy + 1; y++)
                     {
                         try
                         {
                             if (sea.Cells[x, y].status != (int)Cell.Status.Damaged)
                             {
                                 sea.Cells[x, y].status = (int)Cell.Status.Missed;
                             }
                         }
                         catch
                         {
                         }
                     }
                 }
             }
             else
             {
                 for (int x = sea.Cells[coordx, coordy].ship.coordx - 1; x <= sea.Cells[coordx, coordy].ship.coordx + 1; x++)
                 {
                     for (int y = sea.Cells[coordx, coordy].ship.coordy - 1; y <= sea.Cells[coordx, coordy].ship.coordy + sea.Cells[coordx, coordy].ship.Size; y++)
                     {
                         try
                         {
                             if (sea.Cells[x, y].status != (int)Cell.Status.Damaged)
                             {
                                 sea.Cells[x, y].status = (int)Cell.Status.Missed;
                             }
                         }
                         catch
                         {
                         }
                     }
                 }
             }
         }
         return(true);
     }
 }
Ejemplo n.º 4
0
 public static bool CheckShot(int coordx, int coordy, Sea sea)
 {
     if (sea.Cells[coordx, coordy].status == (int)Cell.Status.Ship)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Ejemplo n.º 5
0
 public static void ClearSea(Sea sea)
 {
     for (int x = 0; x <= 9; x++)
     {
         for (int y = 0; y <= 9; y++)
         {
             sea.Cells[x, y].status = (int)Cell.Status.Empty;
         }
     }
     sea.ShipsCount = 10;
 }
Ejemplo n.º 6
0
 public static bool CheckCanShoot(int coordx, int coordy, Sea sea)
 {
     if ((coordx >= 0 && coordx <= 9 && coordy <= 9 && coordy >= 0) &&
         (sea.Cells[coordx, coordy].status == (int)Cell.Status.Empty ||
          sea.Cells[coordx, coordy].status == (int)Cell.Status.Blocked ||
          sea.Cells[coordx, coordy].status == (int)Cell.Status.Ship))
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Ejemplo n.º 7
0
 private void BtnClean_Click(object sender, EventArgs e)
 {
     Sea.ClearSea(FriendlySea);
     UpdateSea(FriendlySea, dataGridView1, true);
     Count1      = 4;
     Count2      = 3;
     Count3      = 2;
     Count4      = 1;
     TotalCount  = 0;
     ships1.Text = Convert.ToString(Count1);
     ships2.Text = Convert.ToString(Count2);
     ships3.Text = Convert.ToString(Count3);
     ships4.Text = Convert.ToString(Count4);
 }
Ejemplo n.º 8
0
        public static void UpdateSea(Sea sea, DataGridView data, bool Show)
        {
            Color EmptyCell  = Color.White;
            Color ShipCell   = Color.LightBlue;
            Color MissCell   = Color.LightGray;
            Color DamageCell = Color.Tomato;

            for (int x = 0; x < sea.Size; x++)
            {
                for (int y = 0; y < sea.Size; y++)
                {
                    switch (sea.Cells[x, y].status)
                    {
                    case 0:
                        data.Rows[x].Cells[y].Style.BackColor = EmptyCell;
                        break;

                    case 1:
                        if (Show)
                        {
                            data.Rows[x].Cells[y].Style.BackColor = ShipCell;
                        }
                        else
                        {
                            data.Rows[x].Cells[y].Style.BackColor = EmptyCell;
                        }
                        break;

                    case 2:
                        data.Rows[x].Cells[y].Style.BackColor = EmptyCell;
                        break;

                    case 3:
                        data.Rows[x].Cells[y].Style.BackColor = MissCell;
                        break;

                    case 4:
                        data.Rows[x].Cells[y].Style.BackColor = DamageCell;
                        break;

                    case 5:
                        data.Rows[x].Cells[y].Style.BackColor = EmptyCell;
                        break;
                    }
                }
            }
        }
Ejemplo n.º 9
0
        public void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int coordx = e.RowIndex;
            int coordy = e.ColumnIndex;

            if (Sea.CheckCanShoot(coordx, coordy, EnemySea))
            {
                if (!Sea.Shot(coordx, coordy, EnemySea))
                {
                    Enemy.Attack(FriendlySea, dataGridView1);
                }
            }
            UpdateSea(EnemySea, dataGridView2, false);
            UpdateSea(FriendlySea, dataGridView1, true);
            dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.SelectionBackColor = dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor;
            Enemy.UpdateActiveCell(dataGridView1);
            if (FriendlySea.ShipsCount == 0 || EnemySea.ShipsCount == 0)
            {
                Endgame();
            }
        }
Ejemplo n.º 10
0
        public static void Attack(Sea FriendlySea, DataGridView dataGridView1)
        {
            Initiative = true;
            do
            {
                if (IsShipFound == false)
                {
                    do
                    {
                        coordx         = rnd.Next(10);
                        coordy         = rnd.Next(10);
                        x              = coordx;
                        y              = coordy;
                        oldx           = x;
                        oldy           = y;
                        IsVectorRandom = true;
                    }while (!Sea.CheckCanShoot(coordx, coordy, FriendlySea));
                    if (Sea.Shot(oldx, oldy, FriendlySea))
                    {
                        Initiative = true;
                        if (FriendlySea.Cells[coordx, coordy].ship.Health != 0)
                        {
                            IsShipFound = true;
                        }
                    }
                    else
                    {
                        Initiative = false;
                    }
                }

                if (Initiative == true && IsShipFound == true && FriendlySea.Cells[coordx, coordy].ship.Size != 1)
                {
                    CanShoot = false;
                    oldx     = x;
                    oldy     = y;
                    while (!CanShoot)
                    {
                        if (tries == 15)
                        {
                            IsVectorRandom = false;
                            tries          = 0;
                            IsShipFound    = false;
                            do
                            {
                                coordx = rnd.Next(10);
                                coordy = rnd.Next(10);
                                x      = coordx;
                                y      = coordy;
                                oldx   = x;
                                oldy   = y;
                            }while (!Sea.CheckCanShoot(coordx, coordy, FriendlySea));
                            CanShoot = true;
                        }
                        if (IsVectorRandom)
                        {
                            RandomMove = rnd.Next(4);
                        }
                        switch (RandomMove)
                        {
                        case 0:
                            tries++;
                            if (Sea.CheckCanShoot(coordx, y + 1, FriendlySea))
                            {
                                y++;
                                CanShoot = true;
                                if (Sea.CheckShot(coordx, y, FriendlySea))
                                {
                                    RandomMove     = 0;
                                    IsVectorRandom = false;
                                }
                            }
                            break;

                        case 1:
                            tries++;
                            if (Sea.CheckCanShoot(coordx, y - 1, FriendlySea))
                            {
                                y--;
                                CanShoot = true;
                                if (Sea.CheckShot(coordx, y, FriendlySea))
                                {
                                    RandomMove     = 1;
                                    IsVectorRandom = false;
                                }
                            }
                            break;

                        case 2:
                            tries++;
                            if (Sea.CheckCanShoot(x + 1, coordy, FriendlySea))
                            {
                                x++;
                                CanShoot = true;
                                if (Sea.CheckShot(x, coordy, FriendlySea))
                                {
                                    RandomMove     = 2;
                                    IsVectorRandom = false;
                                }
                            }
                            break;

                        case 3:
                            tries++;
                            if (Sea.CheckCanShoot(x - 1, coordy, FriendlySea))
                            {
                                x--;
                                CanShoot = true;
                                if (Sea.CheckShot(x, coordy, FriendlySea))
                                {
                                    RandomMove     = 3;
                                    IsVectorRandom = false;
                                }
                            }
                            break;
                        }
                    }
                    oldx = x;
                    oldy = y;
                    if (!Sea.CheckShot(x, y, FriendlySea))
                    {
                        x = coordx;
                        y = coordy;
                    }
                }
                if (tries == 15)
                {
                    tries = 0;
                }

                if (Initiative == true && Sea.CheckShot(oldx, oldy, FriendlySea) && FriendlySea.Cells[oldx, oldy].ship.Health == 1)
                {
                    IsShipFound = false;
                }
                if (Initiative == true && Sea.CheckShot(oldx, oldy, FriendlySea) && FriendlySea.Cells[oldx, oldy].ship.Health > 1)
                {
                    Initiative = true;
                }

                if (Initiative == true && !Sea.Shot(oldx, oldy, FriendlySea))
                {
                    Initiative = false;
                    if (!IsVectorRandom)
                    {
                        switch (RandomMove)
                        {
                        case 0:
                            x          = coordx;
                            y          = coordy;
                            RandomMove = 1;
                            break;

                        case 1:
                            x          = coordx;
                            y          = coordy;
                            RandomMove = 0;
                            break;

                        case 2:
                            x          = coordx;
                            y          = coordy;
                            RandomMove = 3;
                            break;

                        case 3:
                            x          = coordx;
                            y          = coordy;
                            RandomMove = 2;
                            break;
                        }
                    }
                }
            }while (Initiative == true && FriendlySea.ShipsCount > 0);
        }