Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
460 views
in Technique[技术] by (71.8m points)

c# - Add images to array, pick one at random and delete from array

So I just started programming in C# (I do know the basics though). With friends I sometimes play a card game where you have to pick a card and according to what's on it, we have to do a challenge and the loser has to take a drink. Just for fun, I want to make this game as a Windows Form Application. I'm having issues with the images though. I want to get them from a folder that's in the directory of the program executable and add them to an array. Then I want to randomly select one (with the Random class) when a button is clicked. Once a card has been picked, I want it to be deleted from the array so it can't be chosen again.

I've been looking through quite a bit of tutorials on SO and other places, but I can't find something that fully works. This one came closest, as it did select a random picture when a button was clicked and displayed it, but I didn't find a way to remove that picture from the array. How can I add the functionality to delete the image from the array?

Edit: I should've added that I've already tried some stuff that didn't work. I deleted that code though ('cause it didn't work), but I'll try some stuff again and post back when I can't make it work.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

According to the link in your example i will assume that in your code you represent an image as string.

Just use a list List<string> images = new List<string>(); to hold your images

to add elements in the list use images.Add(elemenet) where element is of type string

to draw a randrom card use

Randrom randomNumber = new Random();
int extractedCard = randomNumber.Next(images.Length);

then display images[extractedCard]

and then you can easily remove that card from the list images.RemoveAt(extractedCard)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...