NSMutableArray
인스턴스 변수를 만듭니다. 게임/레벨이 시작되면 그것을 비 웁니다. 사용자가 버튼을 탭하면 배열에 식별자 (예 : 버튼 번호 또는 제목, 심지어 버튼 객체 자체)를 추가합니다. 마지막으로,이 배열을 준비된 배열 (올바른 해결책)과 비교하는 방법을 구현하십시오.
편집 : 여기
는 출발점이 될 것입니다.
@interface SomeClassWhereYourButtonsAre
// Array to store the tapped buttons' numbers:
@property (nonatomic) NSMutableArray *tappedButtons;
// Array to store the correct solution:
@property (nonatomic) NSArray *solution;
...
@end
@implementation SomeClassWhereYourButtonsAre
...
- (void)startGame {
self.tappedButtons = [[NSMutableArray alloc] init];
// This will be the correct order for this level:
self.solution = @[@3, @1, @2, @4];
// You probably will have to load this from some text or plist file,
// and not hardcode it.
}
- (void)buttonTapped:(Button *)b {
// Assuming your button has an ivar called number of type int:
[self.tappedButtons addObject:@(b.number)];
BOOL correct = [self correctSoFar];
if (!correct) {
if (self.tappedButtons.count == self.solution.count) {
// success!
} else {
// correct button, but not done yet
} else {
// wrong button, game over.
}
}
- (BOOL)correctSoFar {
// if he tapped more buttons then the solution requires, he failed.
if (self.tappedButtons.count > self.solution.count)
return NO;
for (int i = 0; i < self.tappedButtons; i++) {
if (self.tappedButtons[i] != self.solution[i]) {
return NO;
}
}
return YES;
}
@end
당신은 나에게 예를 들어 줄 수 있을까? 감사! – user2083920
@ user2083920 내 편집 참조 – DrummerB