IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

XCode Discussion :

Générer un choix aléatoire


Sujet :

XCode

  1. #1
    Invité
    Invité(e)
    Par défaut Générer un choix aléatoire
    Bonjour,

    J'ai l'intention de créer un quizz dont les questions (et réponses) seront stockés dans un fichier texte.

    Je me suis inspiré de script déjà disponible sur le net, cependant il me manque le côté "aléatoire", en effet j'aimerais pouvoir générer au hasard les questions mais je bloque.

    J'ai pense utiliser la fonction arc4random(), mais je ne vois pas où implémenter dans le code déjà éxistant.

    Voilà mon code en question :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    //
    //  Quiz_GameViewController.m
    //  Quiz Game
    //
    //  Created by David Cole on 3/17/09.
    //  Copyright __MyCompanyName__ 2009. All rights reserved.
    //
    
    #import "Quiz_GameViewController.h"
    
    @implementation Quiz_GameViewController
    @synthesize theQuestion;
    @synthesize theScore;
    @synthesize theLives;
    @synthesize answerOne;
    @synthesize answerTwo;
    @synthesize answerThree;
    @synthesize answerFour;
    @synthesize theQuiz;
    @synthesize timer;
    
    
    -(void)askQuestion
    {
    	// Unhide all the answer buttons.
    	[answerOne setHidden:NO];
    	[answerTwo setHidden:NO];
    	[answerThree setHidden:NO];
    	[answerFour setHidden:NO];
    	
    	// Set the game to a "live" question (for timer purposes)
    	questionLive = YES;
    	
    	// Set the time for the timer
    	time = 8.0;
    	
    	// Go to the next question
        
    	questionNumber = questionNumber + 1;
        
        
    	
    	// THIS IS REALLY TERRIBLE CODE!!!
    	// We get the question from the questionNumber * the row that we look up in the array.
    	// This is absolutely horrible, just a placeholder until the right way.
    	// I cannot even begin to describe how wrong this solution is.
    	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:@"Question: %@", selected];
    	[answerOne setTitle:[theQuiz objectAtIndex:row+1] forState:UIControlStateNormal];
    	[answerTwo setTitle:[theQuiz objectAtIndex:row+2] forState:UIControlStateNormal];
    	[answerThree setTitle:[theQuiz objectAtIndex:row+3] forState:UIControlStateNormal];
    	[answerFour setTitle:[theQuiz objectAtIndex:row+4] forState:UIControlStateNormal];
    	rightAnswer = [[theQuiz objectAtIndex:row+5] intValue];
    	
    	// Set theQuestion label to the active question
    	theQuestion.text = activeQuestion;
    	
    	// Start the timer for the countdown
    	timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
    	
    	[selected release];
    	[activeQuestion release];
    }
    
    -(void)updateScore
    {
    	// If the score is being updated, the question is not live
    	questionLive = NO;
    	
    	[timer invalidate];
    	
    	// Hide the answers from the previous question
    	[answerOne setHidden:YES];
    	[answerTwo setHidden:YES];
    	[answerThree setHidden:YES];
    	[answerFour setHidden:YES];
    	NSString *scoreUpdate = [[NSString alloc] initWithFormat:@"Score: %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 = [[NSString alloc] initWithFormat:@"That's game!\nNice going \nYou scored %i!", myScore];
    			theQuestion.text = finishingStatement;
    			[finishingStatement release];
    		}
    		else
    		{
    			NSString *finishingStatement = [[NSString alloc] initWithFormat:@"That's game!\nWow. You're terrible! \nYou scored %i.", myScore];
    			theQuestion.text = finishingStatement;
    			[finishingStatement release];
    		}
    		theLives.text = @"";
    		
    		// Make button 1 appear as a reset game button
    		restartGame = YES;
    		[answerOne setHidden:NO];
    		[answerOne addTarget:self action:@selector(reset)forControlEvents:UIControlEventTouchDown];
    		[answerOne setTitle:@"Restart the game" forState:UIControlStateNormal];
    		
    	}
    	else
    	{
    	// Give a short rest between questions
    	time = 3.0;
    	
    	// Initialize the timer
    	timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
    	}
    }
    
    -(void)countDown
    {
    	// Question live counter
    	if(questionLive==YES)
    	{
    		time = time - 1;
    		theLives.text = [NSString stringWithFormat:@"Time remaining: %i!", time];
    		
    		if(time == 0)
    		{
    			// Loser!
    			questionLive = NO;
    			theQuestion.text = @"Sorry, you ran out of time!";
    			myScore = myScore - 50;
    			[timer invalidate];
    			[self updateScore];
    		}
    	}
    	// In-between Question counter
    	else
    	{
    		time = time - 1;
    		theLives.text = [NSString stringWithFormat:@"Next question in...%i!", time];
    	
    		if(time == 0)
    		{
    			[timer invalidate];
    			theLives.text = @"";
    			[self askQuestion];
    		}
    	}
    	if(time < 0)
    	{
    		[timer invalidate];
    	}
    }
    
    
    - (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.
    		[answerTwo setHidden:NO];
    		[answerThree setHidden:NO];
    		[answerFour setHidden:NO];
    		[self askQuestion];
    	}
    	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 = @"You're so money baby! Good work.";
    		myScore = myScore + 50;
    	}
    	else
    	{
    		theQuestion.text = @"Sorry weaksauce, you came up empty on that one!";
    		myScore = myScore - 50;
    	}
    	[self updateScore];
    }
    
    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    - (void)viewDidLoad {
        [super viewDidLoad];
    	questionLive = NO;
    	restartGame = NO;
    	theQuestion.text = @"Welcome to the Quiz Game! Think you're a pretty smart cookie, huh? Well, let's test your knowledge!";
    	theScore.text = @"Score:0";
    	theLives.text = @"";
    	questionNumber = 0;
    	myScore = 0;
    	myLives = 0;
    	[answerOne setTitle:@"Let's Play!" forState:UIControlStateNormal];
    	[answerTwo setHidden:YES];
    	[answerThree setHidden:YES];
    	[answerFour setHidden:YES];
    	[self loadQuiz];
    }
    
    -(void)loadQuiz
    {
    	NSBundle *bundle = [NSBundle mainBundle];
    	NSString *textFilePath = [bundle pathForResource:@"quizgame" ofType:@"txt"];
    	NSString *fileContents = [NSString stringWithContentsOfFile:textFilePath];
    	NSArray *quizArray = [[NSArray alloc] initWithArray:[fileContents componentsSeparatedByString:@"\n"]];
    	self.theQuiz = quizArray;
    	//[quizArray release];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
        // Release anything that's not essential, such as cached data
    }
    
    
    - (void)dealloc {
    	[theQuestion release];
    	[theScore release];
    	[theLives release];
    	[answerOne release];
    	[answerTwo release];
    	[answerThree release];
    	[answerFour release];
    	[theQuiz release];
    	[timer release];
        [super dealloc];
    }
    
    -(void)reset {
    	[answerOne removeTarget:self action:@selector(reset)forControlEvents:UIControlEventTouchDown];
    	[self viewDidLoad];	
    }
    
    @end
    En vous remerciant par avance.

    -----------------------
    L'ensemble du projet : http://www.megaupload.com/?d=HW9PGCJ6
    Dernière modification par kOrt3x ; 14/07/2011 à 09h25. Motif: ajout d'un tag

Discussions similaires

  1. Réponses: 16
    Dernier message: 08/03/2006, 11h18
  2. Besoin de générer une chaine aléatoire
    Par warsky dans le forum Langage
    Réponses: 7
    Dernier message: 01/12/2005, 19h34
  3. Requete MySQL + choix aléatoire d'un enreg
    Par Steph4fun dans le forum Requêtes
    Réponses: 2
    Dernier message: 08/08/2005, 18h03
  4. générer un nombre aléatoire gaussien
    Par kayari dans le forum Probabilités
    Réponses: 9
    Dernier message: 24/05/2005, 23h57
  5. Générer un nombre aléatoire entre 0 et 1 (INCLUS !!!)
    Par haypo dans le forum Algorithmes et structures de données
    Réponses: 3
    Dernier message: 22/08/2002, 16h30

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo