如何检测并将图像设置为仅1个空白的图片框?

时间:2021-01-30 00:24:10

I have a GameForm with 7 pictureboxes to represent cards from a player hand. There are 2 players and 2 picture boxes in the middle. The picturebxoes in the middle is for the player to Discard his card and another is to draw cards. When the player clicks on a image the card will go to Discard Pile picture box and thus leaving the picturebox in the hand empty. How do I make it so that when a player draws a card it goes to an empty picture box ? !

我有一个带有7个图片盒的GameForm来代表玩家手牌。中间有2个玩家和2个图片框。中间的图片用于让玩家丢弃他的卡片,另一个用于抽奖卡片。当玩家点击图像时,该卡将转到Discard Pile图片框,从而将手中的图片框留空。我如何制作它,以便当玩家拿出一张卡片时,它会进入一个空的图片框? !

I've tried using this method

我尝试过使用这种方法

if (pictureBox1.Image == null) {
        pictureBox1.ImageLocation = images[n];
        }
 ...
 ...
if (pictureBox7.Image == null) {
        pictureBox7.ImageLocation = images[n];
        }

But if I did that, then if 2 Picturebox are empty, Both will get filled with the random card drawn.. Any suggestion on how to only draw 1 card and set to only 1 empty picturebox everytime ? Thanks in advance !

但是,如果我这样做,那么如果2 Picturebox是空的,两者将填充随机卡绘制..任何建议如何只画1张卡,每次只设置1个空白的图片框?提前致谢 !

1 个解决方案

#1


2  

Just use else if instead of if, to make sure that once you assign the card to an available position, you don't accidentally assign it to other positions too.

只需使用else而不是if,以确保一旦将卡分配到可用位置,您也不会意外地将其分配给其他位置。

if (pictureBox1.Image == null)
    pictureBox1.ImageLocation = images[n];
else if (pictureBox2.Image == null)
    pictureBox2.ImageLocation = images[n];
...
...
else if (pictureBox7.Image == null)
    pictureBox7.ImageLocation = images[n];

Alternatively, you could store the PictureBox controls in a collection like this, when your program starts:

或者,您可以在程序启动时将PictureBox控件存储在这样的集合中:

List<PictureBox> hand = new List<PictureBox> { pictureBox1, ..., pictureBox7 };

Then when you need to find the first available position, use LINQ:

然后,当您需要找到第一个可用位置时,请使用LINQ:

// Find the first slot in the player's hand with a null Image, if any
var availableSlot = hand.FirstOrDefault(x => x.Image == null);

// If an empty slot was found, set it to the image from the "draw" pile
if (availableSlot != null)
    availableSlot.Image = images[n];

#1


2  

Just use else if instead of if, to make sure that once you assign the card to an available position, you don't accidentally assign it to other positions too.

只需使用else而不是if,以确保一旦将卡分配到可用位置,您也不会意外地将其分配给其他位置。

if (pictureBox1.Image == null)
    pictureBox1.ImageLocation = images[n];
else if (pictureBox2.Image == null)
    pictureBox2.ImageLocation = images[n];
...
...
else if (pictureBox7.Image == null)
    pictureBox7.ImageLocation = images[n];

Alternatively, you could store the PictureBox controls in a collection like this, when your program starts:

或者,您可以在程序启动时将PictureBox控件存储在这样的集合中:

List<PictureBox> hand = new List<PictureBox> { pictureBox1, ..., pictureBox7 };

Then when you need to find the first available position, use LINQ:

然后,当您需要找到第一个可用位置时,请使用LINQ:

// Find the first slot in the player's hand with a null Image, if any
var availableSlot = hand.FirstOrDefault(x => x.Image == null);

// If an empty slot was found, set it to the image from the "draw" pile
if (availableSlot != null)
    availableSlot.Image = images[n];