jpassetti
jpassetti

Reputation: 41

Build Array with Structure in Objective-C

I would like to know the process to create a set of letters, assign point values to them (use the game "Scrabble" as reference), and be able to access them via an index number, so I can shuffle and manipulate the order of tiles.... all in Objective-C.

I'm using XCode4, and I'm trying to program a simple word game loosely based on Scrabble.

I was able to do this in C by creating a structure to hold the different variables of a letter block.

struct singleTile {
char letter;
int value;
};

Another structure that held an array, storing the data for each letter.

struct singleTile set[] =
{
  {"A", 1}, 
  {"B", 4}, 
  {"C", 4},
  {"D", 2}, ...etc.
}

I'm able to display the information about each letter via an index number.

printf("The second letter is %s and is worth %d points\n", set[2].letter, set[2].value);

How do I translate this into Objective-C? I've read some tutorials about NSArrays and NSMutableArrays, but I haven't found examples of these arrays that hold more than one piece of information, like mine above (specifically a letter and a value).

Please don't type everything out, unless you feel inclined to do so. If you explain the process and/or logic to me and point me in the right direction, I should be able to figure out how to program it.

In the future, I'd like to have each tile hold more information beyond a letter and a value, so this basic lesson that I can't hurdle is important to me.

I appreciate your time and generosity.

Thank you in advance. -Jeff

Upvotes: 0

Views: 673

Answers (3)

Alex Reynolds
Alex Reynolds

Reputation: 96927

You want to look into writing your own Tile class, so that you can store a bunch of Tile objects in an NSArray or NSMutableArray. A Tile class would contain, at least, an NSString for the letter value, and an NSNumber for the score value.

You can add @property properties, so that you can easily set and access variables in an instance of a Tile, e.g., letterQTile.score gives you the score of the Q tile, letterYTile.color describes the tile's appearance, etc. The Tile class can be extended with other properties, or subclassed for creating "special" kinds of tiles (e.g., double-letter, double-word, etc.).

As jsumners suggests, instead of an array, your Tile objects can each be associated with a key in an NSDictionary, for fast lookups.

An overview of object-oriented programming is beyond the scope of what I could fit into a paragraph, but there are several tutorials online (example).

Upvotes: 1

James Sumners
James Sumners

Reputation: 14775

NSMutableDictionary *letterPoints = [[NSMutableDictionary alloc] init];
[letterPoints setObject:[NSNumber numberWithInt:1] forKey:@"A"];
...
...
[letterPoints setObject:[NSNumber numberWithInt:26] forKey:@"Z"];

This is a very basic conversion of your array struct to a native Objective-C dictionary. The idea is that you can look up your point value based on the letter, and not a mapping of letters to an index.

You can extend this concept by storing a different object with each letter. That is, instead of simply storing a point value, you can store an object that contains properties/methods that are associated with the letter.

Upvotes: 2

Hot Licks
Hot Licks

Reputation: 47699

Depending on what you want there are several approaches.

First off, for the most part, you can use regular C concepts and constructs in Objective-C. And if you make the module .mm rather than .m, you can use C++.

Second, you can define a simple Objective-C class that contains a char and an int, and insert multiple instances of that into an NSArray.

Third, you can use an NSDictionary that maps NSString to NSNumber, though this only maps from the char value to the int, not the other way around.

And there are a bunch of other combos.

Upvotes: 1

Related Questions