views:

163

answers:

0

Hello everyone,

I'd like to code a high score board for my application, can anyone advise the best way to do it?

#import "MathsTestViewController.h"


@implementation MathsTestViewController

@synthesize theQuestion;
@synthesize theScore;
@synthesize theLives;
@synthesize answerOne;
@synthesize answerTwo;
@synthesize answerThree;
@synthesize answerFour;
@synthesize theQuiz;
@synthesize timer;

-(void)askQuestion 
{
  //Here we un-hide all the answer buttons, suggests we're starting a new game
 [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 as 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 = 6;
 if(questionNumber == 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 setTitle:@"Restart the game" forState:UIControlStateNormal];


 }
 else
 {
  // Give a short rest between questions
  time = 1.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 - 100;

   [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)
  {
   questionLive = 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];

  }
 }
}

- (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 = @"Well Done your right! Good work.";
  myScore = myScore + 50;

 if(rightAnswer == theAnswerValue && time > 4 ){
  theQuestion.text = @"Wow your FAST.";
  myScore = myScore + 100;
 }
 }
 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:@"mathsquestions" 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)removeSelfFromWindow
{
 [self.view removeFromSuperview];
}


- (void)dealloc {
 [theQuestion release];
 [theScore release];
 [theLives release];
 [answerOne release];
 [answerTwo release];
 [answerThree release];
 [answerFour release];
 [theQuiz release];
 [timer release];
    [super dealloc];
}



@end

`

I read a little about NSUserDefaults is this the only way to go? I don't really understand how it works???