玩游戏后重置阵列?

时间:2012-10-14 15:50:10

标签: iphone ios xcode arrays cocoa-touch

我在iOS中使用xcode进行了测验,我现在所拥有的测验是从数组中生成20个随机答案,当按下按钮时它会控制标签的文本以检查答案是否正确。答案也是随机生成的,我确保没有相同文本的多个答案。我的问题是,当提出问题时,我将数组中对象的文本更改为“已删除”;但是当我完成测验后再次按下启动时,我的数组有20个对象,字符串为“remove”;我的问题是:如何将数组重置为普通对象?这是我的完整代码,忽略评论,但它是一种垃圾,因为我是ios编程的新手:

//
//  ViewController.m
//  Mundo Grafia
//
//  Created by Axel Lambregts on 2/10/12.
//  Copyright (c) 2012 AxelNiels. 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

2 个答案:

答案 0 :(得分:0)

我看到两个选项:

你再次调用init行......

String *arrEurope[][2] =

{

    {@"Eifel.jpg",@"Paris"},
    {@"NotreDame.jpg",@"Paris"},...

或者你向数组添加第三行并在那里放置“删除”(我称之为“used”)参数。在最后一种情况下,您可以再次遍历数组并将“已删除”重新置于“活动”

答案 1 :(得分:0)

在游戏开始时制作arrEurope的副本,并在重新开始游戏时恢复它,或使用其他阵列将问题标记为已移除。我个人会尝试使用选项2。