0

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.

ICL1901
  • 7,632
  • 14
  • 90
  • 138

1 Answers1

3

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
}
picciano
  • 22,341
  • 9
  • 69
  • 82
  • What if I need a different item (color, shape, sound) at each square? – ICL1901 Oct 06 '14 at 21:28
  • 2
    Then, rather than an array of strings, you should create an array of `Square` objects. Create a `Square` class that has properties for what you need. If you need more help with that, probably ask a new question rather than using comments on this answer. – picciano Oct 06 '14 at 21:32
  • Yeah, the Square object is an alternative to using a dictionary. The dictionary is simpler, in a way, since you can easily add a new attribute, but it's not a neat and "structured". (I'd prefer using a dictionary in this case, but you haven't seen my office.) – Hot Licks Oct 07 '14 at 00:41