实时创建课程?
我正在 Objective-C 中制作一个匹配游戏,并且正在尝试找出一种方法来在数组被洗牌后保留它们的值。
三个不同的数组由“plist”文件填充,其各自的元素彼此对应(即第一个数组中的第 n 个元素与第二个和第三个数组中的第 n 个元素匹配)。
然后对数组进行打乱并显示。然而,在洗牌过程中,我失去了在元素之间找到匹配项的能力,因为它们现在是随机顺序的。
可以在此 页面。用户“chrisL”建议为每个问题/答案匹配创建一个类。这是一个合理的解决方案,但我需要使这个游戏变得灵活,以便有人可以添加任意数量的匹配元素,而无需接触任何代码。
我觉得唯一可以做到的方法是确定有多少匹配项并实时为它们创建类,但这听起来像是编程不切实际。
有人可以给我一些建议来解决这个问题吗?
I'm making a matching game in Objective-C and I'm trying to figure out a way to retain the values of an array after they are shuffled.
Three different arrays are populated from 'plist' files with their respective elements corresponding to each other (i.e. nth element in first array matches with nth element in second and third arrays).
The arrays are then shuffled and displayed. During the process of shuffling however, I lose the ability to find a match between the elements because they are now in random order.
A solution to this problem can be found on this page. User "chrisL" suggests making a class for each question/answer match. This is a reasonable solution however I need to make this game flexible so that someone can add as many matching elements as they want without having to touch any code.
I feel like the only way this could be done is to determine how many matching items there are and create classes for them in real-time but this sounds like a programming impracticality.
Can anyone shoot me some pointers as to get around this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您不理解向您建议的解决方案。您可以动态实例化类的对象(这是完全正常的事情),而不是动态生成类
您的问题列表是“扁平化”的,并且只是约定保证每组 6 个相邻项目对应一个问题。相反,这样做会创建一个问题数组(其中每个问题都是一个问题元素数组),然后您可以放心地打乱外部数组。
下一点是,按照第一个元素是问题文本、下一个元素是选项的约定,使用数组来表示问题是不好的编程习惯。不要使用这种表示形式,而是创建一个 Question 类,然后实例化 Question 对象来填充您的问题列表。
这种面向对象的方法有两个主要好处:
I think you didn't understand the solution suggested to you. Instead of dyynamically generating classes, you would dynamically instantiate objects of a class (something perfectly normal to do)
Your list of questions is "flattened" and just convention guarantees that each group of 6 adjacent items corresponds to a question. Instead, of doing this create an array of questions, (where each question is an array of question elements) and then you can shuffle the outer array without worrying.
The next point is that using an array to represent a question, by the convention that the first element is the question text and that the next element are the options is bad programming practice. Instead of using this representation, create a Question class and then instantiate Question objects to populate you question list.
This object-oriented approach gives 2 major benefits: