2013-08-31 50 views
0

它給錯誤「主題1:EXC_BREAKPOINT(代碼= EXC_I386_BPT,子碼=爲0x0) 我的代碼:線程1:EXC_BREAKPOINT(代碼= EXC_I386_BPT,子碼=爲0x0)

@synthesize s1; 
@synthesize s2; 
@synthesize s3; 
@synthesize s4; 
@synthesize s5; 
@synthesize s6; 
@synthesize s7; 
@synthesize s8; 
@synthesize s9; 
@synthesize oImg,xImg,theImg,whoseTurn,board; 
@synthesize resetButton, myAlertView; 


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { 
     // Custom initalization 
    } 
    return self; 
} 

- (void)viewDidLoad { 
    oImg = [UIImage imageNamed:@"O copy.jpg"]; 
    xImg = [UIImage imageNamed:@"X copy.jpg"]; 

    playerToken = 1; 

    whoseTurn.text = @"X can go"; 

    numberOfPlays = 0; 


    [super viewDidLoad]; 
} 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 

    switch(playerToken){ 
     case 1:theImg = xImg; 
      break; 
     case 2: 
      theImg = oImg; 
      break; 
    } 

    UITouch *touch = [[event allTouches]anyObject]; 
    cellWasUsed = NO; 

    if (CGRectContainsPoint([s1 frame], [touch locationInView:self.view])&(s1.image == NULL)) { 
     cellWasUsed = YES; 
     s1.image = theImg; 
    } 
    if (CGRectContainsPoint([s2 frame], [touch locationInView:self.view])&(s2.image == NULL)) { 
     cellWasUsed = YES; 
     s2.image = theImg; 
    } 
    if (CGRectContainsPoint([s3 frame], [touch locationInView:self.view])&(s3.image == NULL)) { 
     cellWasUsed = YES; 
     s3.image = theImg; 
    } 
    if (CGRectContainsPoint([s4 frame], [touch locationInView:self.view])&(s4.image == NULL)) { 
     cellWasUsed = YES; 
     s4.image = theImg; 
    } 
    if (CGRectContainsPoint([s5 frame], [touch locationInView:self.view])&(s5.image == NULL)) { 
     cellWasUsed = YES; 
     s5.image = theImg; 
    } 
    if (CGRectContainsPoint([s6 frame], [touch locationInView:self.view])&(s6.image == NULL)) { 
     cellWasUsed = YES; 
     s6.image = theImg; 
    } 
    if (CGRectContainsPoint([s7 frame], [touch locationInView:self.view])&(s7.image == NULL)) { 
     cellWasUsed = YES; 
     s7.image = theImg; 
    } 
    if (CGRectContainsPoint([s8 frame], [touch locationInView:self.view])&(s8.image == NULL)) { 
     cellWasUsed = YES; 
     s8.image = theImg; 
    } 
    if (CGRectContainsPoint([s9 frame], [touch locationInView:self.view])&(s9.image == NULL)) { 
     cellWasUsed = YES; 
     s9.image = theImg; 
    } 
    [self processLogic]; 
    if (cellWasUsed) { 
     [self updatePlayerInfo]; 
    } 
} 

-(void)processLogic{ 

    if ([self checkForWin]){ 
     if(playerToken ==1){ 
      myAlertView = [[UIAlertView alloc] initWithTitle:@"winner" message:@"X won" delegate:self 
              cancelButtonTitle:@"ok" otherButtonTitles:nil, nil]; 
      [myAlertView show]; 
      [self resetBoard]; 
     } 
     else if(playerToken ==2){ 
      myAlertView = [[UIAlertView alloc] initWithTitle:@"winner" message:@"O won" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil]; 
      [myAlertView show]; 
      [self resetBoard]; 
     } 
     if(numberOfPlays ==9){ 
      myAlertView = [[ UIAlertView alloc]initWithTitle:@"No Winner" message:@"Tie" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil]; 
      [myAlertView show]; 

      [self resetBoard]; 
     } 



    } 

} 

-(BOOL)checkForWin{ 

    // HORIZONTAL WINS 
    if((s1.image == s2.image) & (s2.image == s3.image) & (s1.image != NULL)) 
    { 
     return YES; 
    } 
    if((s4.image == s5.image) & (s5.image == s6.image) & (s4.image != NULL)) 
    { 
     return YES; 
    } 
    if((s7.image == s8.image) & (s8.image == s9.image) & (s7.image != NULL)) 
    { 
     return YES; 
    } 
    // VERTICAL WINS 
    if((s1.image == s4.image) & (s4.image == s7.image) & (s1.image != NULL)) 
    { 
     return YES; 
    } 
    if((s2.image == s5.image) & (s5.image == s8.image) & (s2.image != NULL)) 
    { 
     return YES; 
    } 
    if((s3.image == s6.image) & (s6.image == s9.image) & (s3.image != NULL)) 
    { 
     return YES; 
    } 
    // DIAGONAL WINS 
    if((s1.image == s5.image) & (s5.image == s9.image) & (s1.image != NULL)) 
    { 
     return YES; 
    } 
    if((s3.image == s5.image) & (s5.image == s7.image) & (s3.image != NULL)) 
    { 
     return YES; 
    } 
    //right now return 1 becuase we havn't implemented this yet 
    return NO; 
} 

-(void)displayWinner{ 
    if([self checkForWin]==YES){ 
     if(playerToken ==1){ 
      whoseTurn.text [email protected]"X is the winner!"; 

     } 
     else{whoseTurn.text = @"O is the winner!"; 

     } 
    } 

} 

-(IBAction)buttonReset{ 

    [self resetBoard]; 

} 

-(void)resetBoard{ 

    s1.image =NULL; 
    s2.image =NULL; 
    s3.image =NULL; 
    s4.image =NULL; 
    s5.image =NULL; 
    s6.image =NULL; 
    s7.image =NULL; 
    s8.image =NULL; 
    s9.image =NULL; 

    playerToken =1; 
    whoseTurn.text = @"X can go"; 

    numberOfPlays = 0; 






} 

-(void)updatePlayerInfo{ 
    numberOfPlays++; 
    if(numberOfPlays ==9){ 

     [self resetBoard]; 
    } 
    if (playerToken ==1){ 

     playerToken =2; 
     whoseTurn.text = @"O can go"; 

    } else { 
     playerToken = 1; 
     whoseTurn.text = @"X can go"; 
    } 
} 

- (void)dealloc { 
    [s1 release]; 
    [s2 release]; 
    [s3 release]; 
    [s4 release]; 
    [s5 release]; 
    [s6 release]; 
    [s7 release]; 
    [s8 release]; 
    [s9 release]; 
    [theImg release]; 
    [resetButton release]; 
    [board release]; 
    [oImg release]; 
    [xImg release]; 
    [whoseTurn release]; 
    [myAlertView release]; 



    [super dealloc]; 


} 


@end ` 

有人可以幫我找出什麼是錯的?

+1

而不是單: IF((S3 ((s3.image == s5.image)&&(s3.image == s7.image)&(s3.image!= NULL)) 使用double &&: if (s5.image == s7.image)&&(s3.image!= NULL)) – 3lvis

+0

你不能發表一堆代碼並說「有什麼問題?」。至少提供有關完整錯誤消息的更多詳細信息,並指出代碼中的位置(基於堆棧跟蹤)它正在崩潰。 – rmaddy

回答

2

,因爲你正在使用[super dealloc]我猜你的項目是不可ARC。在這種情況下,當你創建xImgoImg-imageNamed你不留住他們(和-dealloc你釋放他們。也許,你應該更正確地閱讀iOS中的內存管理?)您正在訪問解除分配的對象,這會導致錯誤。改爲使用

oImg = [[UIImage imageNamed:@"O copy.jpg"] retain]; 

您也可以通過編輯 - >重構 - >轉換爲ARC將您的項目轉換爲ARC。

此外,我強烈建議使用按鈕來趕接觸,儘量避免複製粘貼代碼,並且,作爲NSElvis提到的,使用的& &代替&

+0

是正確的。 – bbum

+0

@medvedNick非常感謝! – user1673797

相關問題