Book Image

Sparrow iOS Game Framework Beginner's Guide

By : Johannes Stein
Book Image

Sparrow iOS Game Framework Beginner's Guide

By: Johannes Stein

Overview of this book

Table of Contents (20 chapters)
Sparrow iOS Game Framework Beginner's Guide
About the Author
About the Reviewers

Time for action – spicing up the AI with fuzzy values

To replace our hardcoded values, we need to use the following steps:

  1. Open the Battlefield.m file.

  2. Add a new method called fuzzyValue, as shown in the following code:

    -(float) fuzzyValue: (NSString *) value
      if ([value isEqualToString:@"Very near"]) {
        return (float) (arg4random() % 40) + 40.0f;
      } else if ([value isEqualToString:@"Quite near"]) {
        result = (float) (arc4random() % 30) + 70.0f;
      } else {
        result = (float) (arc4random() % 50) + 150.0f;
  3. Using the following code, update the hardcoded values with the values from the fuzzyValue method:

    if ([ship checkDistanceToShip:_pirateShip] < [self fuzzyValue:@"Near"]) {
    if ([ship checkDistanceToShip:_pirateShip] < [self fuzzyValue:@"Very near"]) {
    if ([ship checkDistanceToShip:_pirateShip] < [self fuzzyValue:@"Quite near"]) {
  4. Run the example. If we were to insert logging to see what the values actually are, we would see the following output:

What just happened?