开发者

Creating classes in Real Time?

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 thi开发者_如何学Gord 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?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜