在按钮单击时从文件夹中选择随机,唯一的图像

时间:2015-01-31 20:28:04

标签: c# image random

我正在开发一款C#纸牌游戏,我希望在点击按钮时随机选择图像。一旦选择了卡,就必须向用户显示该卡,并且必须发生某些事情,因此无法再次选择它。我得到了第一部分,选择了一个随机图像并显示给用户,但我无法做到这一点,因此无法再次选择。有时会多次挑选一张卡片,有时根本没有选择任何图像,我会得到错误图像。这是到目前为止的代码。

public partial class Form1 : Form
    {
        private List<int> useableNumbers;

        public Form1()
        {
            InitializeComponent();
            // Creates a list of numbers (card names) that can be chosen from
            useableNumbers = new List<int>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
                                          35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54};
            settings = new Settings();
        }

        private void btnDrawCard_Click(object sender, EventArgs e)
        {
            this.setImage();
        }

        private void setImage()
        {
            // Checks if there are still numbers left in the List
            if (useableNumbers.Count() == 0)
            {
                MessageBox.Show("The game has ended");
                Application.Exit();
            }
            else
            {
                Random r = new Random();
                int i = r.Next(useableNumbers.Count());
                // Looks for the path the executable is in
                string path = System.IO.Path.GetDirectoryName(Application.ExecutablePath) + @"\images\";
                // Looks up the image in the images folder, with the name picked by the Random class and with the extension .png
                string image = path + i + ".png";
                // Sets the image in the pictureBox as the image looked up
                pictureBox1.ImageLocation = image;
                // Removes the selected image from the List so it can't be used again
                useableNumbers.RemoveAt(i);
            }
        }

        private void quitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            settings.Show();
        }

澄清一点;我在与可执行文件相同的文件夹中有一个名为“images”的文件夹。在该文件夹中,有54个名为“1”到“54”的图像(52个普通卡和两个笑话者)。 useableNumbers列表中的数字代表该文件夹中的图像名称。选择图像后,我想从列表useableNumbers.RemoveAt(i);中删除该图像的名称。虽然我收到的消息是“游戏已经结束”,但我也遇到了上述问题。

我觉得useableNumbers.RemoveAt(i);不会改变List的索引,所以当'10'被删除时,它会保持索引为11而不是将所有值减少1,如果你知道我的意思吗。

我也尝试将图像存储在List中,但是无法使其工作,所以这就是我这样做的原因。对C#来说还是新手,所以也许有更好的方法可以做到。

如何修复从列表中删除的内容,因此我没有获得两次或更多相同的图像,或者根本没有图像?

2 个答案:

答案 0 :(得分:0)

使用创建“i”整数时,将从剩余值的数量中选择值,而不是从值本身中选择。我建议在int i = r.Next(useableNumbers.Count());下面放一行,使整数等于List中包含的值之一: int j = useablenumbers(i); 之后,所有使用“i”整数都将被“j”替换,除了最后一行useablenumbers.RemoveAt(i);保持不变。

答案 1 :(得分:0)

而不是这个,它得到一个介于0和当前可用数字大小之间的随机数,

int i = r.Next(useableNumbers.Count());

我会这样做,

int i = useableNumbers[r.Next(useableNumbers.Count())];

因此,从剩余的可用数字中选择一个而不是当前大小。目前你可以有重复项,因为,你只是减少了要选择的数字的范围 - 从一开始它是零到53,但在一半之后你减少到这个范围的一半,重复变得更有可能。最后你保证得到1.范围不应该改变,你总是想要一个随机数1 - 54,而不是一个已经被选中的。所以这种新的方式是你在列表中获得一个随机索引来检索一个值,而不仅仅是得到一个随机索引并将其作为你的值。

与此类似,

useableNumbers.RemoveAt(i);

这样做,

useableNumbers.RemoveAt(useableNumbers.IndexOf(i));

然后将从列表中删除那个号码,而不是列表中该位置的值。