Objective-C How do I enumerate a dictionary with letters and positions

83 Views Asked by At

I think i blew up my brain. I have a dictionary with two arrays: letters and numbers. Numbers are the letters' positions on a board.

How can I enumerate over these arrays so that:

on a board of 64 squares, letter goes on its board number, and other board numbers are set to blank?

My goal is to allow people to select a square with a letter, and not crash if they select a square with no letter.

1

There are 1 best solutions below

3
picciano On

If I may suggest an alternative data structure, you might be better off with an array with a length of 64, each element representing a single square on the board. An empty string would represent an empty square, and a letter would represent a square with that letter.

For example:

// initialize game board
NSMutableArray *board = [[NSMutableArray alloc] init];
for (int loop=0; loop<64; loop++) {
    [board addObject:@""]; // indicates an empty square
}

// set the pieces
board[17] = @"a";
board[23] = @"b";
board[61] = @"c";

// test a board square
if ([board[43] isEqualToString:@""]) {
    // square is empty
} else {
    // square has a letter
}