winforms 当船只沉没时显示船只名称有问题

n6lpvg4x  于 2023-02-09  发布在  其他
关注(0)|答案(1)|浏览(120)

我现在面临的问题是我不知道如何检查对手的行动,它击沉了哪些船,这样我就可以显示一条消息说“你的____已经沉没”。
这是我写的代码

namespace Naval
{
    public partial class Form2 : Form
    {
        const int Size_grid = 10;
        const int picturebox = 50;
        PictureBox[,] playerBoard = new PictureBox[Size_grid, Size_grid];
        PictureBox[,] opponentBoard = new PictureBox[Size_grid, Size_grid];
        int[,] playerShips = new int[Size_grid, Size_grid];
        int[,] opponentShips = new int[Size_grid, Size_grid];
        int[] Lengths = new int[] { 5, 4, 3, 2 };
        //string[] Names = new string[] { "Αεροπλανοφόρο", "Αντιτορπιλικό", "Πολεμικό", "Υποβρύχιο" };

        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            for (int row = 0; row < Size_grid; row++)
            {
                for (int col = 0; col < Size_grid; col++)
                {
                    PictureBox playerPictureBox = new PictureBox();
                    playerPictureBox.Size = new Size(picturebox, picturebox);
                    playerPictureBox.Location = new Point(col * (picturebox + 10) + 185, row * (picturebox + 10) + 245);
                    playerPictureBox.Click += PictureBox_Click;
                    playerPictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
                    playerPictureBox.BackColor = Color.Gray;
                    panel1.Controls.Add(playerPictureBox);
                    playerBoard[row, col] = playerPictureBox;
                }
            }

            for (int row = 0; row < Size_grid; row++)
            {
                for (int col = 0; col < Size_grid; col++)
                {
                    PictureBox opponentPictureBox = new PictureBox();
                    opponentPictureBox.Size = new Size(picturebox, picturebox);
                    opponentPictureBox.Location = new Point(col * (picturebox + 10) + 1145, row * (picturebox + 10) + 245);
                    opponentPictureBox.Click += PictureBox_Click;
                    opponentPictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
                    opponentPictureBox.BackColor = Color.Gray;
                    panel1.Controls.Add(opponentPictureBox);
                    opponentBoard[row, col] = opponentPictureBox;
                }
            }

            PlacePlayerShips(playerShips, Lengths);
            PlaceOpponentShips(opponentShips, Lengths);

        }
        private void PictureBox_Click(object sender, EventArgs e)
        {
            PictureBox pictureBox = (PictureBox)sender;
            int row = (pictureBox.Location.Y - 245) / (picturebox + 10);
            int col = (pictureBox.Location.X - 1145) / (picturebox + 10);

            if (pictureBox.Location.X >= 1145 && pictureBox.ImageLocation == null) // opponent board
            {
                if (row >= 0 && row < opponentShips.GetLength(0) && col >= 0 && col < opponentShips.GetLength(1))
                {
                    if (opponentShips[row, col] > 0)
                    {
                        pictureBox.ImageLocation = "x.png";
                        
                    }
                    else
                    {
                        pictureBox.ImageLocation = "-.png";
                    }
                    ComputerMove();
                }
            }
        }



        private void ComputerMove()
        {
            Random random = new Random();
            int row = random.Next(Size_grid);
            int col = random.Next(Size_grid);

            while (playerBoard[row, col].ImageLocation != null)
            {
                row = random.Next(Size_grid);
                col = random.Next(Size_grid);
            }

            if (playerShips[row, col] > 0)
            {
                playerBoard[row, col].ImageLocation = "x.png";
                
            }
            else
            {
                playerBoard[row, col].ImageLocation = "-.png";
            }
        }



        private void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void PlacePlayerShips(int[,] playerShips, int[] shipLengths)
        {
            Random random = new Random(DateTime.Now.Millisecond);

            foreach (int shipLength in shipLengths)
            {
                int row, col;
                int direction = random.Next(2);

                int placed = 0;
                while (placed == 0)
                {
                    if (direction == 0) // Horizontal
                    {
                        row = random.Next(Size_grid);
                        col = random.Next(Size_grid - shipLength + 1);

                        // Check if ship overlaps with other ships
                        int overlap = 0;
                        for (int i = 0; i < shipLength; i++)
                        {
                            if (playerShips[row, col + i] == 1)
                            {
                                overlap = 1;

                            }
                        }

                        // Place ship if no overlap
                        if (overlap == 0)
                        {
                            placed = 1;
                            for (int i = 0; i < shipLength; i++)
                            {
                                playerShips[row, col + i] = 1;
                                playerBoard[row, col + i].BackColor = Color.LightBlue;

                            }
                        }
                    }
                    else // Vertical
                    {
                        row = random.Next(Size_grid - shipLength + 1);
                        col = random.Next(Size_grid);

                        // Check if ship overlaps with other ships
                        int overlap = 0;
                        for (int i = 0; i < shipLength; i++)
                        {
                            if (playerShips[row + i, col] == 1)
                            {
                                overlap = 1;

                            }
                        }

                        // Place ship if no overlap
                        if (overlap == 0)
                        {
                            placed = 1;
                            for (int i = 0; i < shipLength; i++)
                            {
                                playerShips[row + i, col] = 1;
                                playerBoard[row + i, col].BackColor = Color.LightBlue;

                            }
                        }
                    }

                    // Change direction if ship couldn't be placed
                    if (placed == 0)
                    {
                        direction = (direction + 1) % 2;
                    }
                }
            }
        }

        private void PlaceOpponentShips(int[,] opponentShips, int[] shipLengths)
        {
            Random random = new Random(DateTime.Now.Millisecond);
            ;

            foreach (int shipLength in shipLengths)
            {
                int row, col;
                int direction = random.Next(2);

                int placed = 0;
                while (placed == 0)
                {
                    if (direction == 0) // Horizontal
                    {
                        row = random.Next(Size_grid);
                        col = random.Next(Size_grid - shipLength + 1);

                        // Check if ship overlaps with other ships
                        int overlap = 0;
                        for (int i = 0; i < shipLength; i++)
                        {
                            if (opponentShips[row, col + i] == 1)
                            {
                                overlap = 1;
                                
                            }
                        }

                        // Place ship if no overlap
                        if (overlap == 0)
                        {
                            placed = 1;
                            for (int i = 0; i < shipLength; i++)
                            {
                                opponentShips[row, col + i] = 1;
                                
                            }
                        }
                    }
                    else // Vertical
                    {
                        row = random.Next(Size_grid - shipLength + 1);
                        col = random.Next(Size_grid);

                        // Check if ship overlaps with other ships
                        int overlap = 0;
                        for (int i = 0; i < shipLength; i++)
                        {
                            if (opponentShips[row + i, col] == 1)
                            {
                                overlap = 1;
                                
                            }
                        }

                        // Place ship if no overlap
                        if (overlap == 0)
                        {
                            placed = 1;
                            for (int i = 0; i < shipLength; i++)
                            {
                                opponentShips[row + i, col] = 1;
                                
                            }
                        }
                    }

                    // Change direction if ship couldn't be placed
                    if (placed == 0)
                    {
                        direction = (direction + 1) % 2;
                    }
                }
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
        int time = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            time++;
            label42.Text= time.ToString();
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            label44.Text = " ";
            timer2.Enabled = false;
        }
    }
}

我试着添加一个switch,选项1-4,但是没有成功,我也试过有一个int[] ship Hits = new int[] {0,0,0,0},每次有船被击中就加1,但是没有按计划进行,因为我不知道如何将数组中的每一项绑定到船上,我想大概就是这样了

kg7wmglp

kg7wmglp1#

一种有用的方法是将Ship的所有信息打包到一个类中,这是一个abstraction,它可以使在船只沉没时更容易地显示船只名称,同时,使用inheritance,这样Ship仍然是PictureBox,具有所有隐含的功能。

船舶最小等级示例

成员属性告诉我们需要知道的关于一艘船的信息。使用enum值可以使意图非常清楚。

class Ship : PictureBox
{
    #region P R O P E R T I E S
    [Description("Type")]
    public τύπος τύπος
    {
        get => _τύπος;
        set
        {
            if (!Equals(_τύπος, value))
            {
                _τύπος = value;
                switch (_τύπος)
                {
                    case τύπος.Αεροπλανοφόρο: Image = Image.FromFile(Path.Combine(_imageDir, "aircraft-carrier.png")); break;
                    case τύπος.Αντιτορπιλικό: Image = Image.FromFile(Path.Combine(_imageDir, "destroyer.png")); break;
                    case τύπος.Πολεμικό: Image = Image.FromFile(Path.Combine(_imageDir, "military.png")); break;
                    case τύπος.Υποβρύχιο: Image = Image.FromFile(Path.Combine(_imageDir, "submarine.png")); break;
                }
            }
        }
    }
    τύπος _τύπος = 0;

    public bool Sunk { get; set; }

    [Description("Flag")]
    public σημαία σημαία
    {
        get => _σημαία;
        set
        {
            _σημαία = value;
            onUpdateColor();
        }
    }
    σημαία _σημαία = σημαία.Player;
    #endregion P R O P E R T I E S

    private void onUpdateColor()
    {
        var color =
            Sunk ? Color.Red :
                σημαία.Equals(σημαία.Player) ?
                    Color.Navy :
                    Color.DarkOliveGreen;
        for (int x = 0; x < Image.Width; x++) for (int y = 0; y < Image.Height; y++)
            {
                Bitmap bitmap = (Bitmap)Image;
                if (bitmap.GetPixel(x, y).R < 0x80)
                {
                    bitmap.SetPixel(x, y, color);
                }
            }
        Refresh();
    }

    public Point[] Hits { get; set; } = new Point[0];
    public override string ToString() =>
        $"{σημαία} {τύπος} @ {((TableLayoutPanel)Parent)?.GetCellPosition(this)}";

    private readonly static string _imageDir =
        Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images");
}
  • 其中enum值为:*
enum Direction
{
    Horizontal,
    Vertical,
}
enum τύπος
{
    [Description("Aircraft Carrier")]
    Αεροπλανοφόρο = 5,

    [Description("Destroyer")]
    Αντιτορπιλικό = 4,

    [Description("Military")]
    Πολεμικό = 3,

    [Description("Submarine")]
    Υποβρύχιο = 2,
}
/// <summary>
/// Flag
/// </summary>
enum σημαία
{
    Player,
    Opponent,
}

在船只沉没时显示其名称

单击PictureBox的继承Ship版本时,信息现在可用。

private void onAnyShipClick(object sender, EventArgs e)
{
    if (sender is Ship ship)
    {
        MessageBox.Show(ship.ToString());
    }
}

图片来源:Robuart根据许可证使用。

相关问题