Beispiel #1
0
 //ADDING ONE SINGLE SHIP
 public void UpdateShips(BattleshipShip ship)
 {
     for (int i = 0; i < ship.size; i++)
     {
         PictureBox cell = (PictureBox)this.GetControlFromPosition(ship.positions[i][0], ship.positions[i][1]);
         Image      img  = ship.components[i];
         cell.BackgroundImage = img;
     }
 }
Beispiel #2
0
 public void Sunk(BattleshipShip ship)
 {
     player1.sunk.Add(ship);
     for (int i = 0; i < ship.size; i++)
     {
         rightBoard.cells[ship.positions[i][0] - 1, ship.positions[i][1] - 1].BackgroundImage = ship.components[i];
     }
     if (player1.sunk.Count == player1.myShips.Count)
     {
         finished = true;
         //rightBoard.ImgClick -= Guess1;
         Won?.Invoke(this, new OnWin {
             player = player1
         });
     }
 }
        public BattleshipBot(string n, int[] shipNum) : base("BOT", new int[] { })
        {
            name = n;
            Random rand = new Random();

            for (int i = 0; i < 100; i++)
            {
                all.Add(i);
            }

            /*
             * for (int i = 0; i < 50; i++)
             * {
             *  odd.Add(i * 2);
             *  even.Add((i * 2) + 1);
             * }
             */
            int id = 1;
            BattleshipPlacement x = new BattleshipPlacement(shipNum);

            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < shipNum[i]; j++)
                {
                    BattleshipShip ship;
                    int            xCoord, yCoord;
                    do
                    {
                        ship = new BattleshipShip(i, id);
                        ship.dir(rand.Next(-4, 4) * 90);
                        xCoord = rand.Next(1, 12);
                        yCoord = rand.Next(1, 12);
                    }while (!x.Placeable(ship, myShips, new int[] { xCoord, yCoord }));
                    x.placeBoard.UpdateShips(ship);
                    id++;
                    myShips.Add(ship);
                }
            }
            botBoard = x.placeBoard;
            x.Close();
        }
Beispiel #4
0
        //PREPARATION
        private void SetUp()
        {
            selectPictureBox = new List <PictureBox>()
            {
                carrierPictureBox, battleshipPictureBox, destroyerPictureBox, submarinePictureBox, patrolBoatPictureBox
            };
            selectLabel = new List <Label>()
            {
                carrierLabel, battleshipLabel, destroyerLabel, submarineLabel, patrolBoatLabel
            };

            for (int i = 0; i < 5; i++) //ADDING SELECT OPTION ON THE RIGHT SIDE
            {
                int currentId = i;      //FOR REFERENCE
                selectPictureBox[i].Click += delegate(object sender, EventArgs e) { SelectId(sender, e, currentId); };
            }

            void SelectId(object sender, EventArgs e, int idClicked)
            {
                for (int i = 0; i < 5; i++)
                {
                    if (selectLabel[i].BackColor != Color.Black)
                    {
                        //SETTING BACK TO GRAY
                        selectLabel[i].BackColor      = Color.DimGray;
                        selectPictureBox[i].BackColor = Color.Gainsboro;
                    }
                }
                if (selectLabel[idClicked].BackColor != Color.Black) //IF AVAILABLE
                {
                    selectPictureBox[idClicked].BackColor = Color.CadetBlue;
                    selectLabel[idClicked].BackColor      = Color.CadetBlue;

                    selected     = idClicked;
                    selectedShip = new BattleshipShip(idClicked, shipID); //GIVING SHIP TYPE AND AN ID
                    SetRotatePictureBox();
                }
            }
        }
Beispiel #5
0
        //SHIP PLACEMENT
        public void PlaceShip(object sender, BattleshipBoard.ImgClickEventArgs e)
        {
            if (selectedShip != null && Placeable(selectedShip, shipsPlaced, e.coordinates)) //IF SELECTED AND PLACEABLE
            {
                shipsPlaced.Add(selectedShip);
                //REMAINING NUMBER OF SHIPS
                int remaining = int.Parse(selectLabel[selected].Text);
                selectLabel[selected].Text = Convert.ToString(--remaining);
                if (remaining == 0)
                {
                    selectLabel[selected].BackColor      = Color.Black;
                    selectPictureBox[selected].BackColor = Color.Black;
                    //SETTINB BACK TO DEFAULT
                    rotatePictureBox.Image = null;
                    selected = -1;
                }
                placeBoard.UpdateShips(selectedShip); //ADDING SHIP TO BOARD

                shipID += 1;
                if (selected != -1)
                {
                    selectedShip = new BattleshipShip(selected, shipID);
                }                                                                            //CREATING NEW SHIP IF IT'S IN THE INVENTORY

                SetRotatePictureBox();
                //ENDING THIS PHASE
                if (shipID > goal)
                {
                    placeBoard.ImgClick -= PlaceShip;
                    OnPlacedAllShips?.Invoke(this, new PlacedAllShips {
                        inventory = shipsPlaced, myBoard = placeBoard
                    });
                    shipID = 1;
                    Close();
                }
            }
        }
Beispiel #6
0
        public bool Placeable(BattleshipShip ship, List <BattleshipShip> otherShips, int[] coordinates)
        {
            int xDiff = 0, yDiff = 0;

            //SETTING CHECKER FOR DIRECTION
            if (ship.direction == 90)
            {
                xDiff = 1;
            }
            else if (ship.direction == 270)
            {
                xDiff = -1;
            }
            else if (ship.direction == 180)
            {
                yDiff = 1;
            }
            else
            {
                yDiff = -1;
            }

            if (Checker(xDiff, yDiff))
            {
                //ADDING COORDINATES TO SHIP
                for (int i = 0; i < ship.size; i++)
                {
                    ship.positions.Add(new int[] { coordinates[0] + (i * xDiff), coordinates[1] + (i * yDiff) });
                }
                //COMPARING BOATS
                foreach (BattleshipShip item in otherShips)
                {
                    foreach (int[] pos in item.positions)
                    {
                        for (int i = 0; i < ship.positions.Count; i++)
                        {
                            int[] x = ship.positions[i];
                            if (pos[0] == x[0] && pos[1] == x[1])
                            {
                                ship.positions.Clear();
                                return(false);
                            }
                        }
                    }
                }
                //THERE ARE NO PROBLEMS ->
                return(true);
            }
            else
            {
                return(false);
            }

            bool Checker(int x, int y) //IF IT COULD BE ON THE BOARD
            {
                int i = 0;
                int currX = coordinates[0], currY = coordinates[1];

                while ((currX < 11 && currX > 0) && (currY < 11 && currY > 0) && i < ship.size)
                {
                    i++;
                    currX = coordinates[0] + (i * x);
                    currY = coordinates[1] + (i * y);
                }
                //RETURNING RESULT
                if (i == ship.size)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
Beispiel #7
0
 public void SunkShip(BattleshipShip s)
 {
     sunk.Add(s);
 }