示例#1
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();
                }
            }
        }
示例#2
0
        private void Guess1(object sender, BattleshipBoard.ImgClickEventArgs e)
        {
            int x = e.coordinates[0] - 1, y = e.coordinates[1] - 1;

            if (!finished)
            {
                Guess();
            }

            void Guess()
            {
                if (player1.guesses[x, y] == 0)
                {
                    bool hit = false;
                    foreach (BattleshipShip ship in bot.myShips)
                    {
                        if (!hit)
                        {
                            int i = 0;
                            while (i < ship.size && !(ship.positions[i][0] == x + 1 && ship.positions[i][1] == y + 1))
                            {
                                i++;
                            }
                            if (i < ship.size)
                            {
                                hit = true;
                                player1.guesses[x, y] = 2;
                                Hit(new int[] { x, y }, rightBoard);
                                ship.hp -= 1;
                                if (ship.hp == 0)
                                {
                                    Sunk(ship);
                                }
                            }
                        }
                    }
                    if (!hit)
                    {
                        player1.guesses[x, y] = 1;
                        Miss(new int[] { x, y }, rightBoard);
                    }
                    //BOT GUESS
                    if (!finished)
                    {
                        hit = false;
                        int[] botGuess = bot.Guesser();
                        foreach (BattleshipShip ship in player1.myShips)
                        {
                            if (!hit)
                            {
                                int i = 0, xBot = botGuess[0] + 1, yBot = botGuess[1] + 1;
                                while (i < ship.size && !(ship.positions[i][0] == xBot && ship.positions[i][1] == yBot))
                                {
                                    i++;
                                }
                                if (i < ship.size)
                                {
                                    hit = true;
                                    bot.guesses[x, y] = 2;
                                    bot.Hit();
                                    Hit(botGuess, leftBoard);
                                    ship.hp -= 1;
                                    if (ship.hp == 0)
                                    {
                                        bot.sunk.Add(ship);
                                        if (bot.sunk.Count == bot.myShips.Count)
                                        {
                                            finished = true;
                                            Won?.Invoke(this, new OnWin {
                                                player = bot
                                            });
                                        }
                                    }
                                }
                            }
                        }
                        if (!hit)
                        {
                            bot.guesses[x, y] = 1;
                            Miss(botGuess, leftBoard);
                        }
                    }
                }
            }
        }