Generate random questions in swift -
can help? have true or false game, however, want questions shuffled/random. know there previous shuffle answers can't head around it. example, questions in append section should appear @ random. have been trying different things days. had @ arc4random had hard time implementing it. don't want spoon fed decent answer looking for.
here swift code
// viewcontroller.swift // trueorfalse // // // import uikit class viewcontroller: uiviewcontroller { // classes class newlabel:uilabel { convenience required init(width:cgfloat, height:cgfloat, framewidth: cgfloat, frameheight: cgfloat) { self.init(frame: cgrectmake(0, 0, framewidth, frameheight)) self.center = cgpointmake(width, height) self.font = uifont(name: "helveticaneue-medium", size: 18) } } class newbutton:uibutton { var button:uibutton! convenience required init(width:cgfloat, height:cgfloat, framewidth: cgfloat, frameheight: cgfloat) { self.init(frame: cgrectmake(0, 0, framewidth, frameheight)) self.button = uibutton.buttonwithtype(uibuttontype.system) uibutton self.center = cgpointmake(width, height) } } let width:cgfloat = uiscreen.mainscreen().bounds.width let height:cgfloat = uiscreen.mainscreen().bounds.height var model:tofmodel = tofmodel() var statement:uilabel! var correctcount:newlabel! var incorrectcount:newlabel! var timercount:newlabel! var correctamt:int = 0 var incorrectamt:int = 0 var buttontrue:newbutton! var buttonfalse:newbutton! var count:int = 0 var counter:int = 60 var answer:string! var wall:uiview! var lastbool:string! var displaylastbool:newlabel! var questionnumber:newlabel! var frame:uiimageview! var image:uiimage! var splashimage:uiimage! var splashlandingtext:uilabel! var splashbuttonstart:newbutton! var timer = nstimer() var userdefaults = nsuserdefaults.standarduserdefaults() func nextiter() { statement.text = model.statements[count][0] answer = model.statements[count][1] questionnumber.text = "question- \(count+1)/\(model.statements.count)" if count == model.statements.count-1{ statement.textcolor = uicolor.redcolor() } else{ statement.textcolor = uicolor.blackcolor() } if count > 0 && count < model.statements.count { // displaylastbool.text = "" view.addsubview(displaylastbool) } else { displaylastbool.text = "" } count += 1 } // view functions func start(sender:newbutton) { count = 0 frame.image = image displaylastbool = newlabel(width: 170, height: height*0.27, framewidth: 200, frameheight: 50) //timer timer = nstimer.scheduledtimerwithtimeinterval(1, target:self, selector: selector("updatecounter"), userinfo: nil, repeats: true) // put image frame // remove current stuff splashbuttonstart.removefromsuperview() splashlandingtext.removefromsuperview() // add stuff view view.addsubview(timercount) view.addsubview(correctcount) view.addsubview(incorrectcount) view.addsubview(statement) view.addsubview(buttontrue) view.addsubview(buttonfalse) view.addsubview(questionnumber) nextiter() } func splashpage() { count = 0 displaylastbool?.removefromsuperview() // put wall view.addsubview(wall) // hang frame on wall wall.addsubview(frame) // put text view.addsubview(splashlandingtext) // put button view.addsubview(splashbuttonstart) frame.image = splashimage } func reset() { correctcount.hidden = true incorrectcount.hidden = true count = 0 incorrectamt = 0 correctamt = 0 correctcount.removefromsuperview() incorrectcount.removefromsuperview() statement.removefromsuperview() buttonfalse.removefromsuperview() buttontrue.removefromsuperview() questionnumber.removefromsuperview() displaylastbool.removefromsuperview() splashpage() } // boolean detection func boolresponse(sender:newbutton) { if count == model.statements.count { if answer == string(sender.tag) { count = 0 correctamt = 0 incorrectamt = 0 correctcount.hidden = true incorrectcount.hidden = true displaylastbool.hidden = false timer = nstimer.scheduledtimerwithtimeinterval(1, target:self, selector: selector("updatecounter"), userinfo: nil, repeats: true) } else { timer.invalidate() reset() } } else if answer == string(sender.tag) { lastbool = "correct" displaylastbool.textcolor = uicolor.greencolor() // correct count increments here correctamt += 1 } else if answer != string(sender.tag) { lastbool = "incorrect" displaylastbool.textcolor = uicolor.redcolor() // incorrect count increments here incorrectamt += 1 count = 9; } if count == model.statements.count-1 { correctcount.hidden = false incorrectcount.hidden = false correctcount.text = "correct- \(correctamt)" //incorrectcount.text = "incorrect- \(incorrectamt)" //saving highscore var highscore=userdefaults.integerforkey("highscore") if(correctamt>highscore) { userdefaults.setinteger(correctamt, forkey: "highscore") } var highscoreshow=userdefaults.integerforkey("highscore") incorrectcount.text = "high score- \(highscoreshow)" timer.invalidate() counter = 60 timercount.text = string(counter) timercount.textcolor = uicolor.blackcolor() } nextiter() } //timer update function func updatecounter() { timercount.text = string(counter--) if counter <= 9{ timercount.textcolor = uicolor.redcolor() }else{ timercount.textcolor = uicolor.blackcolor() } if counter == 0{ count=9 nextiter() resettimer() } } //timer reset function func resettimer() { correctcount.hidden = false incorrectcount.hidden = false correctcount.text = "correct- \(correctamt)" incorrectcount.text = "incorrect- \(incorrectamt)" timer.invalidate() counter = 60 timercount.text = string(counter) timercount.textcolor = uicolor.blackcolor() // displaylastbool.hidden = true //questionnumber.hidden = true } // make image view can modify function in class var imageview:uiimageview = uiimageview() override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. // append model.statementsappend("sneezes regularly exceed 200 mph.", bool: "0") model.statementsappend("virtually las vegas gambling casinos ensure have no clocks.", bool: "1") model.statementsappend("two human lungs have surface area of approximately 750 square feet.", bool: "1") model.statementsappend("the 'black box' in airplane colored black.", bool: "0") model.statementsappend("the statue of liberty gift germany america.", bool: "0") model.statementsappend("ozone helpful in trophosphere damaging stratosphere.", bool: "0") model.statementsappend("the can opener invented after can.", bool: "1") model.statementsappend("the world's oldest tree on 9000 years old.", bool:"1") model.statementsappend("emus can fly.", bool: "0") model.statementsappend("game on \n play again?", bool: "1") // make wall, frame, , image frame wall = uiview(frame: cgrectmake(0, 0, width, height)) frame = uiimageview(frame: cgrectmake(0, 0, width, height)) //load text statement = uilabel(frame: cgrectmake(0, 0, 280, height)) statement.center = cgpointmake(width/2, height*0.5) statement.textalignment = nstextalignment.center statement.linebreakmode = nslinebreakmode.bywordwrapping statement.numberoflines = 99 statement.font = uifont(name: "arial", size: 24) splashimage = uiimage(named: "splashgradient") image = uiimage(named: "minimalgradient") //load counts timercount = newlabel(width: 100, height: height*0.10, framewidth:200, frameheight:100) correctcount = newlabel(width: 159, height: height*0.15, framewidth:100, frameheight:50) incorrectcount = newlabel(width:147, height: height*0.19, framewidth:130, frameheight:50) questionnumber = newlabel(width: 197, height: height*0.23, framewidth: 200, frameheight: 50) timercount.center = cgpointmake(width/2, height*0.09) timercount.textalignment = nstextalignment.center timercount.font = uifont(name: "avenirnext-demibold", size: 40) timercount.text = string(counter) correctcount.text = "correct- \(correctamt)" incorrectcount.text = "high score- \(incorrectamt)" incorrectcount.textcolor = uicolor.greencolor() lastbool = "incorrect" correctcount.hidden = true incorrectcount.hidden = true questionnumber.hidden = true //load buttons buttontrue = newbutton(width: 80, height: height/1.2, framewidth: 111, frameheight: 45) buttonfalse = newbutton(width: 240, height: height/1.2, framewidth: 111, frameheight: 45) // style of button buttontrue.setbackgroundimage(uiimage(named: "true"), forstate: uicontrolstate.normal) buttonfalse.setbackgroundimage(uiimage(named: "false"), forstate: uicontrolstate.normal) // .tag buttontrue.tag = 1 buttonfalse.tag = 0 // functions if button clicked buttontrue.addtarget(self, action: "boolresponse:", forcontrolevents: uicontrolevents.touchupinside) buttonfalse.addtarget(self, action: "boolresponse:", forcontrolevents: uicontrolevents.touchupinside) // make splash button splashbuttonstart = newbutton(width: width/2, height: height*0.8, framewidth: 244, frameheight: 58) splashbuttonstart.setbackgroundimage(uiimage(named: "splashbutton"), forstate: uicontrolstate.normal) splashbuttonstart.addtarget(self, action: "start:", forcontrolevents: uicontrolevents.touchupinside) // configure text on splash page splashlandingtext = uilabel(frame: cgrectmake(0, 0, 280, 280)) splashlandingtext.center = cgpointmake(width/2, height*0.4) splashlandingtext.textalignment = nstextalignment.center splashlandingtext.numberoflines = 99 splashlandingtext.linebreakmode = nslinebreakmode.bywordwrapping splashlandingtext.font = uifont(name: "avenirnext-demibold", size: 24) splashlandingtext.text = "impossible true or false \n \n ready start journey?" // configure statement splashpage()
}
override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. }
}
i didn't try wade through code. it's much. (tldr) here general solution. you'll need adapt needs. create struct contains question, possible answers, , correct answer (if true/false questions skip possible answers part.) let's call questionstruct.
then create array of questionstruct structures. populate questions.
copy list of questions working array of questions. let's call remainingquestions
then user arc4random_uniform select question @ random , remove remaining questions array
let index = arc4random_uniform(remainingquestions.count) let aquestion = remainingquestions.removeatindex(index)
when remainingquestions array empty, repopulate array of questions start over.
Comments
Post a Comment