玩游戏后复位阵列? [英] Reset array after playing the game?
问题描述
我在iOS中做了一个小测验,其中x code,我现在已经是一个产生20个随机回答了数组的一个测验,它控制标签的文本时按下一个按钮来检查答案是正确的。答案也是随机生成的,我确信,有没有使用相同的文字mulple答案。我的问题是,当一个问题被问,我改变一个对象的文本在我阵列删除;但是,当我把我所做的测验后重新开始,我的阵列有20个对象以字符串删除;我的问题是:我怎样才能恢复我的数组这是正常的对象?这里是我的全code,忽视了评论,但它是一种垃圾,因为我是新来的iOS编程:
//
// ViewController.m
//世界报Grafia
//
//创建者阿克塞尔Lambregts在12年2月10日。
//版权所有(C)2012AxelNiels。版权所有。
//#进口ViewController.h@interface的ViewController()@结束
@implementation的ViewController
// variabelen aanmaken
NSInteger的intScore;
NSInteger的intRandomMonument;
NSInteger的intRandomAnswerPosition;
NSInteger的intRandomAnswerA;
NSInteger的intRandomAnswerB;
NSInteger的intRandomAnswerC;
NSInteger的intRandomAnswerD;
NSInteger的intArrayEuropeLenght;
NSInteger的intAmountOfQuestionsAsked;
* NSString的scoreSpatie;
的NSString * arrEurope [] [2] = { {@Eifel.jpg,@巴黎},
{@NotreDame.jpg,@巴黎},
{@ArcDeTriompheParis.jpg,@巴黎}, {@AtomuimBrussels.jpg,@布鲁塞尔},
{@MannekePis.jpg,@布鲁塞尔}, {@Colosseum.jpg,@罗马},
{@TreviFountainRome.jpg,@罗马}, {@SagradaFamilia.jpg,@巴塞罗那},
{@SpanishSchoolMadrid.jpg,@马德里}, {@KremlinMoskou.jpg,@Moskou}, {@MermaidKopenhagen.jpg,@哥本哈根协议}, {@MonteCarloMonaco.jpg,@摩纳哥}, {@AcropolisAthens.jpg,@雅典}, {@ParlementBudapest.jpg,@布达佩斯}, {@BuildingReykjavik.jpg,@雷克雅未克}, {@StatueKiev.jpg,@基辅}, {@PortOfEuropeLissabon.jpg,@里斯本}, {@ViewSanMarino.jpg,@圣马力诺}, {@BridgesPrague.jpg,@布拉格}, {@MoskeeAnkara.jpg,@安卡拉}, {@TowerBridgeLondon.jpg,@伦敦},
{@BigBenLondon.jpg,@伦敦},
{@LondonEyeLondon.jpg,@伦敦}, {@BrandenburgerTor.jpg,@柏林},
{@ReichstagBerlin.jpg,@柏林}, {零,零} };
/ *
NSArray的* arrEurope = @
@ @Eifel.jpg,@巴黎]
@ @NotreDame.jpg,@巴黎]
@ @ArcDeTriompheParis.jpg,@巴黎]
@ @Eifel.jpg,@巴黎]
@ @NotreDame.jpg,@巴黎]
@ @ArcDeTriompheParis.jpg,@巴黎]@ @AtomuimBrussels.jpg,@布鲁塞尔],
@ @MannekePis.jpg,@布鲁塞尔],@ @Colosseum.jpg,@罗马],
@ @TreviFountainRome.jpg,@罗马],@ @SagradaFamilia.jpg,@巴塞罗那],
@ @SpanishSchoolMadrid.jpg,@马德里],@ @KremlinMoskou.jpg,@Moskou],@ @MermaidKopenhagen.jpg,@哥本哈根协议]@ @MonteCarloMonaco.jpg,@摩纳哥],@ @AcropolisAthens.jpg,@雅典],@ @ParlementBudapest.jpg,@布达佩斯],@ @BuildingReykjavik.jpg,@雷克雅未克],@ @StatueKiev.jpg,@基辅]@ @PortOfEuropeLissabon.jpg,@里斯本],@ @ViewSanMarino.jpg,@圣马力诺],@ @BridgesPrague.jpg,@布拉格],@ @MoskeeAnkara.jpg,@安卡拉],@ @TowerBridgeLondon.jpg,@伦敦],
@ @BigBenLondon.jpg,@伦敦],
@ @LondonEyeLondon.jpg,@伦敦],@ @BrandenburgerTor.jpg,@柏林]
@ @ReichstagBerlin.jpg,@柏林]];
* /// methode'startwanneer呃运去启动杏花wordt gedrukt
- (IBAction为)开始{ [个体经营NewQuestionEurope]
intAmountOfQuestionsAsked = 0;
//重新设置并显示得分
intScore = 0;
score.text = [的NSString stringWithFormat:@%D,intScore] //使按钮和标签可见
Start.hidden = YES;
question.hidden = NO;
score.hidden = NO;
scoretext.hidden = NO;
imageContainer.hidden = NO;
answerA.hidden = NO;
answerB.hidden = NO;
answerC.hidden = NO;
answerD.hidden = NO; answerA.textColor =的UIColor绿彩];
answerB.textColor =的UIColor绿彩];
answerC.textColor =的UIColor绿彩];
answerD.textColor =的UIColor绿彩];
clickButtonA.hidden = NO;
clickButtonB.hidden = NO;
clickButtonC.hidden = NO;
clickButtonD.hidden = NO; GoodFalse.hidden = NO;
GoodFalse.text = @preSS一个按钮来回答;
GoodFalse.textColor =的UIColor绿彩];
question.text = @在哪里拍摄这张照片?} // einde启动 - (IBAction为)复位{score.text = @gelukt}
- (IBAction为)NewQuestionEurope {
如果(intAmountOfQuestionsAsked == 19){ question.text = [@的Uw比分是:stringByAppendingString:[的NSString stringWithFormat:@%D,intScore]]; Start.hidden = NO;
question.hidden = NO;
score.hidden = YES;
scoretext.hidden = YES;
imageContainer.hidden = YES;
answerA.hidden = YES;
answerB.hidden = YES;
answerC.hidden = YES;
answerD.hidden = YES; clickButtonA.hidden = YES;
clickButtonB.hidden = YES;
clickButtonC.hidden = YES;
clickButtonD.hidden = YES;
GoodFalse.hidden = YES; Start.titleLabel.text = @END; } 其他{
intAmountOfQuestionsAsked ++; //德泽杆菌geeft AAN intArrayEuropeLenght德waarde面包车hoeveel rijen德阵列bevat //
intArrayEuropeLenght = 0; 而(arrEurope [intArrayEuropeLenght] [0]!= NULL)
{
intArrayEuropeLenght ++;
}
//score.text = [的NSString stringWithFormat:@%D,intScore]
scoreSpatie = [
[的NSString stringWithFormat:@%D,intScore]
stringByAppendingString:@/];
score.text = [
scoreSpatie
stringByAppendingString:[的NSString stringWithFormat:@%D,intAmountOfQuestionsAsked]
]; //偶合genereren VOOR:4个随机antwoorden; 1随机afbeelding;在德plaatsen EEN随机职位OM HET juiste antwoord
intRandomMonument = arc4random()%intArrayEuropeLenght;
intRandomAnswerA = arc4random()%intArrayEuropeLenght;
intRandomAnswerB = arc4random()%intArrayEuropeLenght;
intRandomAnswerC = arc4random()%intArrayEuropeLenght;
intRandomAnswerD = arc4random()%intArrayEuropeLenght;
intRandomAnswerPosition = arc4random()%4; 而(arrEurope [intRandomMonument] [0] == @删除){ intRandomMonument = arc4random()%intArrayEuropeLenght; }
//随机antwoorden魔剑
answerA.text = @;
answerB.text = @;
answerC.text = @;
answerD.text = @; 如果(answerA.text = @){
answerA.text = arrEurope [intRandomAnswerA] [1];
} 如果(answerB.text = @){
answerB.text = arrEurope [intRandomAnswerB] [1];
} 如果(answerC.text = @){
answerC.text = arrEurope [intRandomAnswerC] [1];
} 如果(answerD.text = @){
answerD.text = arrEurope [intRandomAnswerD] [1];
} 开关(intRandomAnswerPosition)
{
情况下0:answerA.text = arrEurope [intRandomMonument] [1];
打破;
情况1:answerB.text = arrEurope [intRandomMonument] [1];
打破;
案例2:answerC.text = arrEurope [intRandomMonument] [1];
打破;
情况3:answerD.text = arrEurope [intRandomMonument] [1];
}; // ALS呃antwoorden hetzelfde zijn,VOOR德gelijke EEN随机blijven魔剑TOT泽Niet的hetzelfde zijn
如果(
[answerA.text isEqualToString:answerB.text] ||
[answerA.text isEqualToString:answerC.text] ||
[answerA.text isEqualToString:answerD.text]
){ answerA.text = arrEurope [arc4random()%intArrayEuropeLenght] [1]; }; 如果(
[answerB.text isEqualToString:answerA.text] ||
[answerB.text isEqualToString:answerC.text] ||
[answerB.text isEqualToString:answerD.text]
){ answerB.text = arrEurope [arc4random()%intArrayEuropeLenght] [1]; }; 如果(
[answerC.text isEqualToString:answerA.text] ||
[answerC.text isEqualToString:answerB.text] ||
[answerC.text isEqualToString:answerD.text]
){ answerC.text = arrEurope [arc4random()%intArrayEuropeLenght] [1]; }; 如果(
[answerD.text isEqualToString:answerA.text] ||
[answerD.text isEqualToString:answerB.text] ||
[answerD.text isEqualToString:answerC.text]
){ answerD.text = arrEurope [arc4random()%intArrayEuropeLenght] [1]; };
// Laat德afbeelding zien面包车HET gekozen纪念碑(通过随机gekozen)
imageContainer.image = [UIImage的imageNamed:arrEurope [intRandomMonument] [0];
}}
//票数青春痘奥德EEN ergens想FOUT HIJ geeft precies随机尤斯特恩FOUT :)
//根zijn DAT HET opgelost是vanaf我们Niet的2倍dezelfde vragen hebben玛珥IK Denk开发HET Niet的。// preSS按钮a
- (IBAction为)AnswerA { 如果([answerA.text isEqualToString:arrEurope [intRandomMonument] [1]]){ GoodFalse.text = @正确!;
GoodFalse.textColor =的UIColor绿彩];
intScore ++; }其他{ GoodFalse.text = @错!
GoodFalse.textColor =的UIColor redColor] } arrEurope [intRandomMonument] [0] = @删除; [个体经营NewQuestionEurope]}// preSS buttonB
- (IBAction为)AnswerB { 如果([answerB.text isEqualToString:arrEurope [intRandomMonument] [1]]){ GoodFalse.text = @正确!;
GoodFalse.textColor =的UIColor绿彩];
intScore ++; }其他{ GoodFalse.text = @错!
GoodFalse.textColor =的UIColor redColor]
} arrEurope [intRandomMonument] [0] = @删除;
[个体经营NewQuestionEurope]
}// preSS buttonC(作品)
- (IBAction为)AnswerC {
如果([answerC.text isEqualToString:arrEurope [intRandomMonument] [1]]){ GoodFalse.text = @正确!;
GoodFalse.textColor =的UIColor绿彩];
intScore ++; }其他{ GoodFalse.text = @错!
GoodFalse.textColor =的UIColor redColor]
}arrEurope [intRandomMonument] [0] = @删除;
[个体经营NewQuestionEurope]
}
// preSS buttonD
- (IBAction为)AnswerD {
如果([answerD.text isEqualToString:arrEurope [intRandomMonument] [1]]){ GoodFalse.text = @正确的;
GoodFalse.textColor =的UIColor绿彩];
intScore ++; }其他{ GoodFalse.text = @错!
GoodFalse.textColor =的UIColor redColor]
}
arrEurope [intRandomMonument] [0] = @删除;
[个体经营NewQuestionEurope]
}//票数奥德niets wijzigen
- (无效)viewDidLoad中
{
[超级viewDidLoad中];
//装载视图,通常从笔尖后做任何额外的设置。
} - (空)didReceiveMemoryWarning
{
[超级didReceiveMemoryWarning];
可以重新创建任何资源的处置//。
}@结束
要么就比赛一开始arrEurope的副本,当游戏重新启动或使用一些其他的阵列中删除标记的问题还原到它。我会亲自尝试使用选项2。
I made a quiz in iOS with xcode, what i have now is a quiz that generates 20 random answers out of an array, it controls the text of the labels when a button is pushed to check if the answer is correct. The answers are also random generated and i made sure that there weren't mulple answers with the same text. My problem is that when a question is asked, i change the text of an object in my array to "removed"; but when i push start again after i've done the quiz, my array has 20 objects with the string "remove"; my question is: How can I reset my array to it's normal objects? Here is my full code, ignore the comments but it's kind of a junk cause i'm new to ios programming:
//
// ViewController.m
// Mundo Grafia
//
// Created by Axel Lambregts on 2/10/12.
// Copyright (c) 2012 AxelNiels. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
// variabelen aanmaken
NSInteger intScore;
NSInteger intRandomMonument;
NSInteger intRandomAnswerPosition;
NSInteger intRandomAnswerA;
NSInteger intRandomAnswerB;
NSInteger intRandomAnswerC;
NSInteger intRandomAnswerD;
NSInteger intArrayEuropeLenght;
NSInteger intAmountOfQuestionsAsked;
NSString *scoreSpatie;
NSString *arrEurope[][2] =
{
{@"Eifel.jpg",@"Paris"},
{@"NotreDame.jpg",@"Paris"},
{@"ArcDeTriompheParis.jpg",@"Paris"},
{@"AtomuimBrussels.jpg",@"Brussels"},
{@"MannekePis.jpg",@"Brussels"},
{@"Colosseum.jpg",@"Rome"},
{@"TreviFountainRome.jpg",@"Rome"},
{@"SagradaFamilia.jpg",@"Barcelona"},
{@"SpanishSchoolMadrid.jpg",@"Madrid"},
{@"KremlinMoskou.jpg",@"Moskou"},
{@"MermaidKopenhagen.jpg",@"Copenhagen"},
{@"MonteCarloMonaco.jpg",@"Monaco"},
{@"AcropolisAthens.jpg",@"Athens"},
{@"ParlementBudapest.jpg",@"Budapest"},
{@"BuildingReykjavik.jpg",@"Reykjavik"},
{@"StatueKiev.jpg",@"Kiev"},
{@"PortOfEuropeLissabon.jpg",@"Lisbon"},
{@"ViewSanMarino.jpg",@"San Marino"},
{@"BridgesPrague.jpg",@"Prague"},
{@"MoskeeAnkara.jpg",@"Ankara"},
{@"TowerBridgeLondon.jpg",@"London"},
{@"BigBenLondon.jpg",@"London"},
{@"LondonEyeLondon.jpg",@"London"},
{@"BrandenburgerTor.jpg",@"Berlin"},
{@"ReichstagBerlin.jpg",@"Berlin"},
{nil,nil}
};
/*
NSArray *arrEurope = @[
@[@"Eifel.jpg",@"Paris"],
@[@"NotreDame.jpg",@"Paris"],
@[@"ArcDeTriompheParis.jpg",@"Paris"],
@[@"Eifel.jpg",@"Paris"],
@[@"NotreDame.jpg",@"Paris"],
@[@"ArcDeTriompheParis.jpg",@"Paris"],
@[@"AtomuimBrussels.jpg",@"Brussels"],
@[@"MannekePis.jpg",@"Brussels"],
@[@"Colosseum.jpg",@"Rome"],
@[@"TreviFountainRome.jpg",@"Rome"],
@[@"SagradaFamilia.jpg",@"Barcelona"],
@[@"SpanishSchoolMadrid.jpg",@"Madrid"],
@[@"KremlinMoskou.jpg",@"Moskou"],
@[@"MermaidKopenhagen.jpg",@"Copenhagen"],
@[@"MonteCarloMonaco.jpg",@"Monaco"],
@[@"AcropolisAthens.jpg",@"Athens"],
@[@"ParlementBudapest.jpg",@"Budapest"],
@[@"BuildingReykjavik.jpg",@"Reykjavik"],
@[@"StatueKiev.jpg",@"Kiev"],
@[@"PortOfEuropeLissabon.jpg",@"Lisbon"],
@[@"ViewSanMarino.jpg",@"San Marino"],
@[@"BridgesPrague.jpg",@"Prague"],
@[@"MoskeeAnkara.jpg",@"Ankara"],
@[@"TowerBridgeLondon.jpg",@"London"],
@[@"BigBenLondon.jpg",@"London"],
@[@"LondonEyeLondon.jpg",@"London"],
@[@"BrandenburgerTor.jpg",@"Berlin"],
@[@"ReichstagBerlin.jpg",@"Berlin"]
];
*/
//methode'start' wanneer er op de start-knop wordt gedrukt
-(IBAction)start{
[self NewQuestionEurope];
intAmountOfQuestionsAsked = 0;
//reset and show score
intScore = 0;
score.text = [NSString stringWithFormat:@"%d", intScore];
//make buttons and labels visible
Start.hidden = YES;
question.hidden = NO;
score.hidden = NO;
scoretext.hidden = NO;
imageContainer.hidden = NO;
answerA.hidden = NO;
answerB.hidden = NO;
answerC.hidden = NO;
answerD.hidden = NO;
answerA.textColor = [UIColor greenColor];
answerB.textColor = [UIColor greenColor];
answerC.textColor = [UIColor greenColor];
answerD.textColor = [UIColor greenColor];
clickButtonA.hidden = NO;
clickButtonB.hidden = NO;
clickButtonC.hidden = NO;
clickButtonD.hidden = NO;
GoodFalse.hidden = NO;
GoodFalse.text=@"Press a button to answer";
GoodFalse.textColor = [UIColor greenColor];
question.text = @"Where is this picture taken?";
}//einde start
-(IBAction)Reset{
score.text= @"gelukt";
}
-(IBAction)NewQuestionEurope{
if(intAmountOfQuestionsAsked == 19){
question.text = [@"Uw score is: "stringByAppendingString: [NSString stringWithFormat:@"%d", intScore]];
Start.hidden = NO;
question.hidden = NO;
score.hidden = YES;
scoretext.hidden = YES;
imageContainer.hidden = YES;
answerA.hidden = YES;
answerB.hidden = YES;
answerC.hidden = YES;
answerD.hidden = YES;
clickButtonA.hidden = YES;
clickButtonB.hidden = YES;
clickButtonC.hidden = YES;
clickButtonD.hidden = YES;
GoodFalse.hidden = YES;
Start.titleLabel.text = @"END";
}
else{
intAmountOfQuestionsAsked ++;
//Deze lus geeft aan intArrayEuropeLenght de waarde van hoeveel rijen de array bevat//
intArrayEuropeLenght = 0;
while (arrEurope[intArrayEuropeLenght][0] != NULL)
{
intArrayEuropeLenght++;
}
//score.text = [NSString stringWithFormat:@"%d", intScore];
scoreSpatie = [
[NSString stringWithFormat:@"%d", intScore]
stringByAppendingString:@"/"];
score.text = [
scoreSpatie
stringByAppendingString:[NSString stringWithFormat:@"%d", intAmountOfQuestionsAsked]
];
// Randoms genereren voor: 4 random antwoorden; 1 random afbeelding; een random positie om het juiste antwoord in te plaatsen
intRandomMonument = arc4random()%intArrayEuropeLenght;
intRandomAnswerA = arc4random()%intArrayEuropeLenght;
intRandomAnswerB = arc4random()%intArrayEuropeLenght;
intRandomAnswerC = arc4random()%intArrayEuropeLenght;
intRandomAnswerD = arc4random()%intArrayEuropeLenght;
intRandomAnswerPosition = arc4random()%4;
while(arrEurope[intRandomMonument][0] == @"removed"){
intRandomMonument = arc4random()%intArrayEuropeLenght;
}
//Random antwoorden maken
answerA.text = @"";
answerB.text = @"";
answerC.text = @"";
answerD.text = @"";
if(answerA.text = @""){
answerA.text = arrEurope[intRandomAnswerA][1];
}
if(answerB.text = @""){
answerB.text = arrEurope[intRandomAnswerB][1];
}
if(answerC.text = @""){
answerC.text = arrEurope[intRandomAnswerC][1];
}
if(answerD.text = @""){
answerD.text = arrEurope[intRandomAnswerD][1];
}
switch(intRandomAnswerPosition)
{
case 0: answerA.text = arrEurope[intRandomMonument][1];
break;
case 1: answerB.text = arrEurope[intRandomMonument][1];
break;
case 2: answerC.text = arrEurope[intRandomMonument][1];
break;
case 3: answerD.text = arrEurope[intRandomMonument][1];
};
//als er antwoorden hetzelfde zijn, voor de gelijke een random blijven maken tot ze niet hetzelfde zijn
if(
[answerA.text isEqualToString: answerB.text]||
[answerA.text isEqualToString: answerC.text]||
[answerA.text isEqualToString: answerD.text]
){
answerA.text = arrEurope[arc4random()%intArrayEuropeLenght][1];
};
if(
[answerB.text isEqualToString: answerA.text]||
[answerB.text isEqualToString: answerC.text]||
[answerB.text isEqualToString: answerD.text]
){
answerB.text = arrEurope[arc4random()%intArrayEuropeLenght][1];
};
if(
[answerC.text isEqualToString: answerA.text]||
[answerC.text isEqualToString: answerB.text]||
[answerC.text isEqualToString: answerD.text]
){
answerC.text = arrEurope[arc4random()%intArrayEuropeLenght][1];
};
if(
[answerD.text isEqualToString: answerA.text]||
[answerD.text isEqualToString: answerB.text]||
[answerD.text isEqualToString: answerC.text]
){
answerD.text = arrEurope[arc4random()%intArrayEuropeLenght][1];
};
//Laat de afbeelding zien van het gekozen monument (via random gekozen)
imageContainer.image = [UIImage imageNamed: arrEurope[intRandomMonument][0]];
}
}
//hier onder zit ergens een fout want hij geeft precies random juist en fout :)
//kan zijn dat het opgelost is vanaf we niet 2x dezelfde vragen hebben maar ik denk het niet.
//press buttonA
-(IBAction)AnswerA {
if([answerA.text isEqualToString: arrEurope[intRandomMonument][1]]){
GoodFalse.text = @"Correct!";
GoodFalse.textColor = [UIColor greenColor];
intScore ++;
}else {
GoodFalse.text=@"Wrong!";
GoodFalse.textColor = [UIColor redColor];
}
arrEurope[intRandomMonument][0] = @"removed";
[self NewQuestionEurope];
}
//press buttonB
-(IBAction)AnswerB {
if([answerB.text isEqualToString: arrEurope[intRandomMonument][1]]){
GoodFalse.text = @"Correct!";
GoodFalse.textColor = [UIColor greenColor];
intScore ++;
}else {
GoodFalse.text=@"Wrong!";
GoodFalse.textColor = [UIColor redColor];
}
arrEurope[intRandomMonument][0] = @"removed";
[self NewQuestionEurope];
}
//press buttonC (works)
-(IBAction)AnswerC {
if([answerC.text isEqualToString: arrEurope[intRandomMonument][1]]){
GoodFalse.text = @"Correct!";
GoodFalse.textColor = [UIColor greenColor];
intScore ++;
}else {
GoodFalse.text=@"Wrong!";
GoodFalse.textColor = [UIColor redColor];
}
arrEurope[intRandomMonument][0] = @"removed";
[self NewQuestionEurope];
}
//press buttonD
-(IBAction)AnswerD {
if([answerD.text isEqualToString: arrEurope[intRandomMonument][1]]){
GoodFalse.text = @"Correct";
GoodFalse.textColor = [UIColor greenColor];
intScore ++;
}else {
GoodFalse.text=@"Wrong!";
GoodFalse.textColor = [UIColor redColor];
}
arrEurope[intRandomMonument][0] = @"removed";
[self NewQuestionEurope];
}
//hier onder niets wijzigen
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Either make a copy of arrEurope on game start and restore to it when the game is restarted or use some other array to mark a question as removed. I would personally try to use option 2.
这篇关于玩游戏后复位阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!