Note that there are some explanatory texts on larger screens.

plurals
  1. POfor loop incrementation
    primarykey
    data
    text
    <p>I'm a beginner in iOS development so here is my question,</p> <p>i m trying to implement a simple battle ship game and i manually placed my ships into allCoordinates array, the first element is x coordinate , second element is y coordinate and third one is the size of the ship.</p> <p>The problem is; hit and missed must be increased just once for each try but when user enter 6th ship the hit became 1 and miss became 5 since the loop continue to check each row and increase miss.</p> <p>I can't just put a break after the missed++ so i m out of ideas here.</p> <p>Thank you :</p> <pre><code>for ( int i=0 ;i&lt;19;i++ ){ if ([self.xCoordinate.text isEqualToString:[allCoordinates objectAtIndex:i*3]] &amp;&amp; [self.yCoordinate.text isEqualToString:[allCoordinates objectAtIndex:i*3+1]] ) { NSLog(@"hit"); hit++; result.text = [NSString stringWithFormat:@"You hit %ix1 ship",[allCoordinates objectAtIndex:i*3+2]]; break; }else { NSLog(@"not hit"); result.text = [NSString stringWithFormat:@"You missed"]; missed++; } hitLabel.text = [NSString stringWithFormat:@"%i!",hit]; missed = missed%18; missedLabel.text = [NSString stringWithFormat:@"%i!",missed]; </code></pre> <p>and this is my array</p> <pre><code>allCoordinates = [[NSArray alloc] initWithObjects: @"15",@"2",@"3", @"13",@"2",@"3", @"14",@"2",@"3", @"10",@"7",@"3", @"11",@"7",@"3", @"12",@"7",@"3", @"16",@"8",@"3", @"16",@"9",@"3", @"16",@"10",@"3", @"3",@"4",@"4", @"4",@"4",@"4", @"5",@"4",@"4", @"6",@"4",@"4", @"9",@"10",@"4", @"9",@"11",@"4", @"9",@"12",@"4", @"9",@"13",@"4", @"5",@"14",@"1", @"18",@"17",@"1", nil]; </code></pre> <p>edit:i fixed it by putting an BOOL ishit after the for loop and it fixed my problem</p> <pre><code> if(ishit==YES){ hit++; }else { missed++; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload