• Apfeltalk ändert einen Teil seiner Allgemeinen Geschäftsbedingungen (AGB), das Löschen von Useraccounts betreffend.
    Näheres könnt Ihr hier nachlesen: AGB-Änderung
  • Einige Blicke in fremde Welten dürft Ihr nun bestaunen und darüber abstimmen, welche davon Euch am meisten gefällt: hier geht es lang für Euer Voting --> Klick

Hilfe mit Fragenauswahl (Quiz)

Alex0407

Alkmene
Registriert
27.09.11
Beiträge
33
Hallo Leute,

ich bin relativ frisch im Bereich XCode, habe zwar Erfahrungen mit anderen Programmen aber es ist halt doch etwas neues.
Ich bin zufällig in YouTube auf ein Video gestoßen, welches die Erstellung eines Quiz zeigte. Also dachte ich mir, warum probier ich das nicht mal.
Was ich bereits habe ist, einen Start View wo ich mit einem Button in den Game View komme. Dann habe ich einen Code im Internet gefunden, der in etwa für ein Quiz sein soll.

Das Problem das ich habe ist, dass bei diesem Code die Fragen nach der Reihe erscheinen. So wie man sie eingibt, erscheinen sie auch.
Ich hätte gerne versucht, das ganze random zu machen. Da ich jedoch nicht weiterkomme und etwas anstehe im Moment, dachte ich an euch. Vielleicht könnt ihr mir ja helfen das zu realisieren.

Vielen Dank für eure Hilfe....Alex

Im Anhang der Code...ich hoffe ihr könnt es nachvollziehen....ignoriert die angegebenen Fragen, sind nicht von mir^^



-(void)askQuestion
{
// Unhide all the answer buttons.
[answerOnesetHidden:NO];
[answerTwosetHidden:NO];
[answerThreesetHidden:NO];
[answerFoursetHidden:NO];




// Set the time for the timer
time = 10000000.0;

// Go to the next question
questionNumber = questionNumber + 1;

// We get the question from the questionNumber * the row that we look up in the array.

NSInteger row = 0;
if(questionNumber == 1)
{
row = questionNumber - 1;
}
else
{
row = ((questionNumber - 1) * 6);
}

// Set the question string, and set the buttons the the answers
NSString *selected = [theQuiz objectAtIndex:row];
NSString *activeQuestion = [[NSString alloc] initWithFormat:@"%@", selected];
[answerOnesetTitle:[theQuizobjectAtIndex:row+1] forState:UIControlStateNormal];
[answerTwosetTitle:[theQuizobjectAtIndex:row+2] forState:UIControlStateNormal];
[answerThreesetTitle:[theQuizobjectAtIndex:row+3] forState:UIControlStateNormal];
[answerFoursetTitle:[theQuizobjectAtIndex:row+4] forState:UIControlStateNormal];
rightAnswer = [[theQuizobjectAtIndex:row+5] intValue];

// Set theQuestion label to the active question
theQuestion.text = activeQuestion;

// Start the timer for the countdown
timer = [NSTimerscheduledTimerWithTimeInterval:1.0target:selfselector:@selector(countDown) userInfo:nilrepeats:YES];

[selected release];
[activeQuestion release];
}


-(void)updateScore
{
// If the score is being updated, the question is not live
questionLive = NO;

[timerinvalidate];

// Hide the answers from the previous question
[answerOnesetHidden:YES];
[answerTwosetHidden:YES];
[answerThreesetHidden:YES];
[answerFoursetHidden:YES];
NSString *scoreUpdate = [[NSString alloc] initWithFormat:@"€%d", myScore];
theScore.text = scoreUpdate;
[scoreUpdate release];

// END THE GAME.
NSInteger endOfQuiz = [theQuiz count];
if((((questionNumber - 1) * 6) + 6) == endOfQuiz)
{
// Game is over.
if(myScore > 0)
{
NSString *finishingStatement = [[NSStringalloc] initWithFormat:@"Game Over!\nNice Game \nYou scored %i!", myScore];
theQuestion.text = finishingStatement;
[finishingStatement release];
}
else
{
NSString *finishingStatement = [[NSStringalloc] initWithFormat:@"Game Over!\n You're terrible! \nYou scored %i.", myScore];
theQuestion.text = finishingStatement;
[finishingStatement release];
}
theLives.text = @"";



}
else
{
// Give a short rest between questions
time = 1;

// Initialize the timer
timer = [NSTimerscheduledTimerWithTimeInterval:1.0target:selfselector:@selector(countDown) userInfo:nilrepeats:YES];
}
}


-(void)countDown
{
// Question live counter
if(questionLive==YES)
{
time = time - 1;


if(time == 0)
{
// Loser!
questionLive = NO;
theQuestion.text = @"HAHA now you lost alot of points!";
myScore = myScore - 1000;
[timerinvalidate];
[selfupdateScore];
}
}
// In-between Question counter
else
{
time = time - 1;


if(time == 0)
{
[timerinvalidate];
theLives.text = @"";
[selfaskQuestion];
}
}
if(time < 0)
{
[timerinvalidate];
}
}




- (IBAction)buttonOne
{
if(questionNumber == 0){
// This means that we are at the startup-state
// We need to make the other buttons visible, and start the game.
[answerTwosetHidden:NO];
[answerThreesetHidden:NO];
[answerFoursetHidden:NO];
[selfaskQuestion];
}
else
{
NSInteger theAnswerValue = 1;
[self checkAnswer:(int)theAnswerValue];
if(restartGame==YES)
{
// Create a restart game function.
}
}
}


- (IBAction)buttonTwo
{
NSInteger theAnswerValue = 2;
[self checkAnswer:(int)theAnswerValue];
}


- (IBAction)buttonThree
{
NSInteger theAnswerValue = 3;
[self checkAnswer:(int)theAnswerValue];
}


- (IBAction)buttonFour
{
NSInteger theAnswerValue = 4;
[self checkAnswer:(int)theAnswerValue];
}


// Check for the answer (this is not written right, but it runs)
-(void)checkAnswer:(int)theAnswerValue
{
if(rightAnswer == theAnswerValue)
{
theQuestion.text = @"Daaamn";
myScore = myScore + 50;
}
else
{
theQuestion.text = @"hahaha!";
myScore = myScore - 50;
}
[selfupdateScore];
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[superviewDidLoad];
questionLive = NO;
restartGame = NO;
theQuestion.text = @"Are you ready?";
theScore.text = @"";
theLives.text = @"";
questionNumber = 0;
myScore = 0;
myLives = 0;
[answerOnesetTitle:@"Start"forState:UIControlStateNormal];
[answerTwosetHidden:YES];
[answerThreesetHidden:YES];
[answerFoursetHidden:YES];
[selfloadQuiz];
}


-(void)loadQuiz
{
// This is our forced-loaded array of quiz questions.
// FORMAT IS IMPORTANT!!!!
// 1: Question, 2 3 4 5: Answers 1-4 respectively, 6: The right answer
// THIS IS A TERRIBLE WAY TO DO THIS. I will figure out how to do nested arrays to make this better.
NSArray *quizArray = [[NSArrayalloc] initWithObjects:@"Who is the president in USA?",@"Me",@"Obama",@"George Bush",@"Justin Bieber",@"2",
@"Capital in Norway?", @"Bergen", @"Trondheim", @"Oslo", @"Bærum", @"3",
@"The right answer is 3!", @"41", @"24", @"3", @"9", @"1",
@"Do I have a cat?", @"Yes", @"No", @"No, you have a dog", @"No, you have a flying hamster", @"4",
@"Baba", @"Daba jaba?", @"Laba daba haba?", @"Saba daba gaba?", @"Haba haba?", @"4",
nil];
self.theQuiz = quizArray;
[quizArray release];

}


- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}




- (void)dealloc {
[theQuestionrelease];
[theScorerelease];
[theLivesrelease];
[answerOnerelease];
[answerTworelease];
[answerThreerelease];
[answerFourrelease];
[theQuizrelease];
[timerrelease];
[super dealloc];
}
 
Hallo nochmals,

ich hab nun an dem random gearbeitet und bin auf das gekommen...beim testen bekomm ich aber immer den Fehler"SIGNABART", sobald ich auf den Start Button drücke..

Danke für die Hilfe...

-(void)loadQuiz
{


NSArray *quizArray = [[NSArray alloc] initWithObjects:

[NSArrayarrayWithObjects: @"What is Marge Simpsons hair color? ",@"Blue",@"Green",@"Yellow",@"Red",@"1", nil],

[NSArrayarrayWithObjects: @"Question number two? (correct B) ",@"A",@"B",@"C",@"D",@"2", nil],

[NSArrayarrayWithObjects: @"Question number three? (correct C) ",@"A",@"B",@"C",@"D",@"3", nil],

[NSArrayarrayWithObjects: @"Question number four? (correct D) ",@"A",@"B",@"C",@"D",@"4", nil],

[NSArrayarrayWithObjects: @"Question number five? (correct A) ",@"A",@"B",@"C",@"D",@"1", nil],

nil];


self.theQuiz = quizArray;
[quizArray release];


}
 
Ohne Objective-C-Experte zu sein: Was soll das release des Arrays direkt nach dessen Erzeugung? Die Zuweisung an self.theQuiz dürfte doch wohl kaum den Retain-Counter inkrementieren, d.h. Du speicherst eine Referenz auf ein Array und zerstörst das Array, so dass die Referenz ins Leere zeigt. Oder?



By the way: Ich habe mir vom ersten Posting nur einen ganz kurzen Ausschnitt angesehen – und kriege schon die Krise, wenn ich sowas sehe (hier geht wieder mit mir durch, dass ich mal einen Einsteiger-Programmierkurs betreut habe), denn da scheint jemand nicht wirklich nachgedacht zu haben beim Programmieren:

Code:
[COLOR=#6e3fa7]NSInteger[/COLOR] row = [COLOR=#0031d4]0[/COLOR];
[COLOR=#4b8187][COLOR=#bc2ba0]if[/COLOR][COLOR=#000000]([/COLOR]questionNumber[COLOR=#000000] == [/COLOR][COLOR=#0031d4]1[/COLOR][COLOR=#000000])[/COLOR][/COLOR]
{
[COLOR=#4b8187][COLOR=#000000]row = [/COLOR]questionNumber[COLOR=#000000] - [/COLOR][COLOR=#0031d4]1[/COLOR][COLOR=#000000];[/COLOR][/COLOR]
}
[COLOR=#bc2ba0]else[/COLOR]
{
row = (([COLOR=#4b8187]questionNumber[/COLOR] - [COLOR=#0031d4]1[/COLOR]) * [COLOR=#0031d4]6[/COLOR]);
}

Erst wird eine Variable mit 0 initialisiert, obwohl sie anschließend definitiv überschrieben wird. Was soll also die unsinnige Initialisierung, die höchstens dazu führen könnte, dass im Falle eines Programmierfehlers (vergessene Zuweisung in einem speziellen Fall) eine Compilerwarnung ausbleibt, weil ja ein Default-Wert vorhanden ist?
Dann der Then-Zweig: Wir rechnen dynamisch mit dem Wert einer Variablen, den wir schon kennen (wenn questionNumber == 1 ist, dann berechne questionNumber - 1, obwohl wir schon wissen, dass das 0 sein muss. Und die 0 steht eh schon in der Variablen.
Noch bescheuerter: Die Fallunterscheidung ist gar nicht nötig: Im Fall questionNumber==1 gilt (questionNumber-1)*6 == 0, d.h. der ganze Code ließe sich zusammenschrumpfen auf:
Code:
[COLOR=#6e3fa7]NSInteger[/COLOR] row = (([COLOR=#4b8187]questionNumber[/COLOR] - [COLOR=#0031d4]1[/COLOR]) * [COLOR=#0031d4]6[/COLOR]);

(Ich habe Dich so verstanden, dass Du den Code nur kopiert hast. Aber dann wirft das zumindest Zweifel ob der Qualität des kopierten Codes auf, denn mangelnde Sorgfalt kann nicht nur zu unnötig langem Code, sondern eben auch leicht zu fehlerhaftem Code führen.)
 
Danke für deine Hilfe. Ja der Code ist kopiert, fand ich bei dem Video. Ich werde das Ganze jedoch neu beginnen, könnt ihr mir da vielleicht helfen?
Ich bin wie gesagt eher ein Frischling, hab jedoch großes Interesse und bin auch sehr lernbereit...nur etwas Hilfe wäre wahnsinnig klasse.

Also, als Erstes habe ich ja die 4 Antwort Buttons als Buttons definiert.

IBOutlet UIButton *answerOne;
IBOutlet UIButton *answerTwo;
IBOutlet UIButton *answerThree;
IBOutlet UIButton *answerFour;

Dann den score, natürlich als integer. Und das Quiz Array...

NSInteger myScore;
NSArray *theQuiz;

Dann die zugehörigen Labels um die Frage und den Score anzuzeigen:

IBOutlet UILabel *theQuestion;
IBOutlet UILabel *theScore;

Dann die IBActions für die Buttons:

-(IBAction)buttonOne;
-(IBAction)buttonTwo;
-(IBAction)buttonThree;
-(IBAction)buttonFour;

Und abschließend noch die checkAnswer etc.

-(void)checkAnswer;


-(void)askQuestion;


-(void)updateScore;


-(void)loadQuiz;

Ich bräuchte jetzt nur etwas Hilfe bei der .m Datei.

Würde es theoretisch funktionieren, die Fragen im Zufallsprinzip erscheinen zu lassen?

Zum erkennen hätte ich auch eine Idee. Jede Frage hat eine Zahl von 1-4 als richtige Antwort. Also 1 wäre erste Antwort etc.
Dann die Buttons 1-4 mit einem Wert versehen...also wenn Button 1 gedrückt wird, Wert1 usw...
Dann müsste man nur erkennen lassen welche Frage gerade an der Reihe ist und welche Antwort, also welcher Wert) richtig ist.

Ich hoffe ihr könnt mir helfen :)

Danke Alex
 
Ich habe nun versucht den Buttons die Werte 1-4 zuzuweisen und auch den Befehl zu schreiben, das überprüft wird, ob die Antwort richtig ist. Wenn sie richtig ist, wird der Score um 50 erhöht, sonst hätte ich gerne ein GameOver...


- (IBAction)buttonOne
{
NSInteger theAnswerValue = 1;
[self checkAnswer:(int)theAnswerValue];
}




- (IBAction)buttonTwo
{
NSInteger theAnswerValue = 2;
[self checkAnswer:(int)theAnswerValue];
}


- (IBAction)buttonThree
{
NSInteger theAnswerValue = 3;
[self checkAnswer:(int)theAnswerValue];
}


- (IBAction)buttonFour
{
NSInteger theAnswerValue = 4;
[self checkAnswer:(int)theAnswerValue];
}


-(void)checkAnswer:(int)theAnswerValue
{
if(rightAnswer == theAnswerValue)
{
myScore = myScore + 50;
}

[self updateScore];
}


Alex