1

I'm trying to build an array of 400 integers (treated in the context of a 20 x 20 matrix). All elements are initialized to 0, and then 40 random spots are chosen to have an integer 9 (designated as a mine in a minesweeper context). For some reason, my program hangs on the line:

[map replaceObjectAtIndex: mine_placement withObject:[NSNumber numberWithInt:9]];

Following this, I have a routine that accumulates the proximity of mines per each element, and the final bits create string objects to send out to the console (log). I'd love to hear from the Objective-C pros as to what I'm doing wrong, and why this isn't outputting correctly. It builds fine, just no output. I get some message about threads being started. I'm new to Obj-C, so any thoughts on the matter would be awesome.

Here's the contents inside main:

@autoreleasepool
{

    int number_of_mines = 40;
    int mine_placement;

    // create map of 400 char elements
    NSMutableArray* map = [[NSMutableArray alloc] init];

    // char map[400];
    // build array with zeros everywhere
    for(int i = 0; i < 400; i++)
        [map addObject:[NSNumber numberWithInt:0]];

    // add randomly placed mines

    for(int i = 0; i < number_of_mines; i++) // loop for 40 mines
    {
        mine_placement = arc4random() % 400;
        while([map objectAtIndex: mine_placement] == [NSNumber numberWithInt:9]) // if we are already on a mine, repeat
            {
                mine_placement = arc4random() % 400;
            }
        [map replaceObjectAtIndex: mine_placement withObject:[NSNumber numberWithInt:9]];
    }

    // proximity accumulator (an iterative approach)
    for(int i = 0; i < 400; i++)
    {
        if([map objectAtIndex: i] != [NSNumber numberWithInt:9])
        {
            int accumulator = 0;

            // check top three neighbors
            if(i >= 20) // past first row
            {
                if(i % 20 != 0) // past first column
                    if([map objectAtIndex: (i-21)] == [NSNumber numberWithInt:9]) // top-left
                        accumulator++;

                if((i+1) % 20 != 0) // before last column
                    if([map objectAtIndex: (i-19)] == [NSNumber numberWithInt:9]) //top-right
                            accumulator++;

                if([map objectAtIndex: (i-20)] == [NSNumber numberWithInt:9]) // top
                    accumulator++;
            }

            // check bottom three neighbors
            if(i < 380) // before last row
            {
                if(i % 20 != 0) // past first column
                    if([map objectAtIndex: (i+19)] == [NSNumber numberWithInt:9]) // bottom-left
                        accumulator++;

                if((i+1) % 20 != 0) // before last column
                    if([map objectAtIndex: (i+21)] == [NSNumber numberWithInt:9]) // bottom-right
                            accumulator++;

                if([map objectAtIndex: (i+20)] == [NSNumber numberWithInt:9]) // bottom
                    accumulator++;
            }

            // left neighbor
            if(i % 20 != 0) // past first column
                if([map objectAtIndex: (i-1)] == [NSNumber numberWithInt:9]) // left
                    accumulator++;

            // right neighbor
            if((i+1) % 20 != 0) // before last column
                if([map objectAtIndex: (i+1)] == [NSNumber numberWithInt:9]) // right
                    accumulator++;

            if(accumulator > 0)
                [[map objectAtIndex: i] replaceObjectAtIndex: i withObject:[NSNumber numberWithInt:accumulator]];
        }
    }


    // output map of 400 char elements
    // build string rows

    NSMutableString* string_row = [[NSMutableString alloc] init];
    for(int row = 0; row < 20; row++)
    {
        for(int s = 0; s < 20; s++)
        {
            [string_row insertString:[map objectAtIndex: s] atIndex:s];
        }
        NSLog(@"%@", string_row);
        [string_row setString:@""]; // clear string_row for next row pass
    }
}

3 Answers 3

1

Your program might hang on

while([map objectAtIndex: mine_placement] == [NSNumber numberWithInt:9])

You're comparing pointers (addresses) here, but you want to compare values. Try this:

while ([[map objectAtIndex:mine_placement] isEqualTo:[NSNumber numberWithInt:9]])

BTW, now you can use @9 instead of [NSNumber numberWithInt:9] if you're on Xcode 4.5. You can also use map[mine_placement] if you're compiling with iOS 6 SDK, so it should shorten your code a bit.

Sign up to request clarification or add additional context in comments.

3 Comments

and map[i] = @9; instead of [map replaceObjectAtIndex:] :) --- I dont think == [NSNumber numberWithInt:9] is the problem, as all NSNumbers until 13(?) or so return the same Pointer (cached Singletons).
I didn't know about these cache singletons, but better safe than sorry... [map[i] isEqualTo:@9] is short enough.
I agree with you, counting on the cache behaviour is more than bad coding.
0

Your program also hangs on:

[[map objectAtIndex: i] replaceObjectAtIndex: i withObject:[NSNumber numberWithInt:accumulator]];

Because [map objectAtIndex: i] is NSNumber, which has no replaceObjectAtIndex method. You probably should check your array's logic. That's the problem.

Comments

0

I dropped your code into the applicationDidFinishLaunching method of a test app, and I didn't get any hang where you said -- I don't know why it would behave differently there than it would in main. I did get an error on this line:

[[map objectAtIndex: i] replaceObjectAtIndex: i withObject:[NSNumber numberWithInt:accumulator]];

I think you want that to be:

[map replaceObjectAtIndex: i withObject:[NSNumber numberWithInt:accumulator]];

Also the last part of your code where you create the mutable string doesn't work because you're trying to insert an NSNumber into a string. That can be fixed like this:

[string_row insertString:[NSString stringWithFormat:@"%@",[map objectAtIndex: s]] atIndex:s];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.