您知道如何用一系列图片随机填充表格吗?

发布于 2024-10-10 02:38:52 字数 149 浏览 8 评论 0原文

我正在尝试创建一个记忆游戏,但我不知道如何用数组中的图片随机填充我的单元格。我刚刚开始主要针对 iOS 进行 Xcode 编程,并且在 Stack Overflow 上搜索了几天,但似乎找不到答案。要么就是我不太明白你们专业人士所说和解释的内容。

有人可以请启发我吗?

I'm trying to create a memory game and I'm stuck with no idea how to randomly populate my cells with pictures from an array. I've just started out Xcode programming mainly for the iOS and I have searched for days on Stack Overflow but I can't seem to find an answer. Either that or I don't really understand what you professionals have said and explained.

Could anyone kindly please enlighten me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

本王不退位尔等都是臣 2024-10-17 02:38:52

此代码将采用包含图片的 NSArray 并生成一个包含随机顺序图片的新数组。然后,您可以保存新数组并将其在表的数据源中使用。

NSArray *pictures; //Array containing all of the pictures to display
srandomdev();
NSArray *randomPictures = [pictures sortedArrayUsingComparator:(NSComparisonResult)^(id obj1, id obj2) {
    long val = random();
    if(val & 1) return NSOrderedAscending;
    return NSOrderedSame; //or NSOrderedDescending
}];

This code will take an NSArray containing the pictures and produce a new array containing the pictures in random order. Then, you can save the new array and use it in your data source for your table.

NSArray *pictures; //Array containing all of the pictures to display
srandomdev();
NSArray *randomPictures = [pictures sortedArrayUsingComparator:(NSComparisonResult)^(id obj1, id obj2) {
    long val = random();
    if(val & 1) return NSOrderedAscending;
    return NSOrderedSame; //or NSOrderedDescending
}];
预谋 2024-10-17 02:38:52

如果您始终拥有相同的元素集,并且只想将它们放入随机顺序,那么我建议您阅读 Fisher-Yates 洗牌算法 (http://en.wikipedia.org/wiki/Fisher–耶茨_洗牌)。这与您在纸牌游戏中随机化一副​​纸牌或您想要排列的任何其他有限集的过程相同。

If you always have the same set of elements, and you just want to put them into a randomized order, then I recommend you read up on the Fisher-Yates shuffling algorithm (http://en.wikipedia.org/wiki/Fisher–Yates_shuffle). It's the same process you'd use to randomize a deck of cards in a card game, or any other finite set you want to permute.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文