ios - Displaying random questions by looping through a dictionary -


i started learn swift trying make quiz game. i'm stuck @ trying display random questions dictionary. reason why use dictionary because quiz i'm working on based on chemistry elements has different symbols.

so based on selected difficulty created 4 different dictionaries:

//defining dictionaries per difficulty var easy: [string: string] = [     "h": "waterstof",     "he": "helium",     "li": "lithium",     "b": "boor",     "c": "koolstof",     "o": "zuurstof",     "ne": "neon",     "al": "aliminium",     "si": "silicium",     "k": "kalium",     "fe": "ijzer",     "ni": "nikkel",     "zn": "zink",     "cd": "cadmium",     "i": "jood" ] var normal: [string: string] = [     "n": "stikstof",     "f": "fluor",     "na": "natrium",     "mg": "magnesium",     "p": "fosfor",     "ci": "chloor",     "ar": "argon",     "s": "zwavel",     "ca": "calcium",     "cu": "koper",     "au": "goud",     "br": "broom",     "ag": "zilver",     "pt": "platina",     "ba": "barium" ] 

and on.. (hard, extreme)

this viewdidload() method:

override func viewdidload() {      switch (self.selecteddifficultybyuser){         case "makkelijk":// means 'easy' in dutch             //assigning easy dictionary temporary dictionary             self.temp = self.easy         case "normaal":             //assigning normal dictionary temporary dictionary             self.temp = self.normal         case "moeilijk":             //assigning hard dictionary temporary dictionary             self.temp = self.moeilijk         case "extreem":             //assigning extreme dictionary temporary dictionary             self.temp = self.extreem         default:             println("something went wrong")      }      super.viewdidload()     self.answers = [self.btn1, self.btn2, self.btn3, self.btn4]      pickrandomelement()     randomizeanswers()      let updatetime : selector = "updatetime"     timer = nstimer.scheduledtimerwithtimeinterval(0.01, target: self, selector: updatetime, userinfo: nil, repeats: true)     starttime = nsdate.timeintervalsincereferencedate()  } 

as can see i'm assigning buttons array 'answers' looping through soon.

the function pickrandomelement called, looks this:

func pickrandomelement() -> () {     //first here make sure 'answeroptions' array cleared,     //because cause correct answer won't showed.     answeroptions = [string]()       if self.selecteddifficultybyuser == "makkelijk" {         let index: int = int(arc4random_uniform(uint32(easy.count)))          let symbol = array(easy.keys)[index]         let element = array(easy.values)[index]          self.currentquestion = symbol         self.correctanswer = element          //assign correctanswer answeroptions array         self.answeroptions.append(element)          //show question user         self.questionlabel.text = self.currentquestion          //remove correctanswer dictionary,         //make sure answers won't duplicated         self.easy[symbol] = nil         self.easy[element] = nil          (var = 0; < 3; i++) {             let randomindex: int = int(arc4random_uniform(uint32(easy.count)))              let optionsymbol = array(easy.keys)[randomindex]             let optionelement = array(easy.values)[randomindex]              self.answeroptions.append(optionelement)              //removing 'optionsymbol' , 'optionelement' array             //to prevent duplicated answer             self.easy[optionelement] = nil             self.easy[optionsymbol] = nil         }         self.easy = self.temp //filling 'easy' array temporary array      } } 

the problem i'm having in loop shown above. i'm looping 3 times pick random elements , show them user. , during process i'm deleting randomly chosen elements (easy) array prevent duplicate answer. , because i'm removing elements easy array i'm assigning temporary array, created in begin, easy array.

if don't this, whole easy array empty after 3 rounds or so. if do opposite infinte loop.

can please put me in right direction if i'm doing wrong way or me out of problem?

thanks in advance!

updated

firo's solution worked me. i'm know faced bug. namely, pickrandomelement function returns asked question more once. tried make dictionary , put asked questions in there , check see if question has been asked. however, number of questions answered increases, require linearly increasing amount of lookups each question asked, until lookups reached. know how can take care of problem? –

so without of code can give educated guess way of handing infinite loop situation current code.

an easy way prevent infinite loop here verify still have remaining questions want ask. keep instance value check how many questions still want ask.

var remainingsteps = 5 

then when user answers correct question, check before presenting next question

func useransweredquestioncorrectly() {     if self.remainingsteps > 0 {         // present next question         // decrement remaining steps         self.remainingsteps--     else {         // done asking questions, menu (or whatever next)     } } 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -