实时创建课程?

发布于 2024-10-25 07:02:57 字数 516 浏览 1 评论 0原文

我正在 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 技术交流群。

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

发布评论

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

评论(1

眉黛浅 2024-11-01 07:02:57

我认为您不理解向您建议的解决方案。您可以动态实例化类的对象(这是完全正常的事情),而不是动态生成类

  1. 您的问题列表是“扁平化”的,并且只是约定保证每组 6 个相邻项目对应一个问题。相反,这样做会创建一个问题数组(其中每个问题都是一个问题元素数组),然后您可以放心地打乱外部数组。

    [Q1, a1, b1, c1, Q2, a2, c2] --> [[Q1,a1,b1,c1],[Q2,a2,b2,c2]]
    
  2. 下一点是,按照第一个元素是问题文本、下一个元素是选项的约定,使用数组来表示问题是不好的编程习惯。不要使用这种表示形式,而是创建一个 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)

  1. 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.

    [Q1, a1, b1, c1, Q2, a2, c2] --> [[Q1, a1, b1, c1], [Q2, a2, b2, c2]]
    
  2. 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:

    • You can use proper names and methods when accessing a questions, instead of ad-hoc indexes.
    • It becomes easier to add different kinds of Question latter on.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文