redribben icon

vc code

redribben | PRO | 11/04/15 06:21:37 AM UTC | 0 ⭐ | 7619 👁️ | Never ⏰ | []
Objective C |

0 B

|

None

|

0 👍

/

0 👎

//
//  ViewController.m
//  tic tac toe
//
//  Created by Bogdan Michalchuk on 10/22/15.
//  Copyright © 2015 PDXRR. All rights reserved.
//
 
#import "ViewController.h"
#import "BMGameDynamics.h"
 
@interface ViewController ()
@property (nonatomic) BOOL oTurn;
@property (weak, nonatomic) IBOutlet UILabel *outCome;
 
@end
 
 
@implementation ViewController {
    BMGameDynamics* gameManager;
}
 
@synthesize oTurn;
 
- (instancetype) initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        gameManager = [[BMGameDynamics alloc] init];
    }
    return self;
}
 
- (IBAction)newGame:(id)sender {
    [gameManager startAnew];
    [self tTTSquareEnabled:TRUE];
    _outCome.text = @"Good Luck! :)";
}
 
- (IBAction)ticTacToeSquares:(id)sender {
    UIButton *button = (UIButton *)sender;
    NSInteger bTag = button.tag;
    if (oTurn) {
        [button setBackgroundImage:[UIImage imageNamed:@"x.png"]
                          forState: UIControlStateNormal];
    }
    else if (!oTurn) {
        [button setBackgroundImage:[UIImage imageNamed:@"o.png"]
                          forState: UIControlStateNormal];
    }
    int winner = [gameManager modifyXOSpotsWithTurn:oTurn withIndex:(int)bTag];
    switch (winner) {
        case 0:
            break;
        case 1:
            [self tTTSquareEnabled:false];
            self.outCome.text = @"O's WON!!!";
            break;
        case 2: {
            [self tTTSquareEnabled:false];
            self.outCome.text = @"X's WON!!!";
            break;
        }
        case 3: {
            self.outCome.text = @"You BOTH LOST!!!";
            break;
        }
        default:
            break;
    }
    button.enabled=FALSE;
    oTurn = !oTurn;
    NSLog(@"%ld", (long)bTag);
}
 
- (void)tTTSquareEnabled:(BOOL)answer {
    for (NSInteger i = [self.view.subviews count] -1; i>=0; i--) {
        if ([[self.view.subviews objectAtIndex:i] isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton*)[self.view.subviews objectAtIndex:i];
            if ((button.tag < 10)) {
                button.enabled=answer;
            }
            if (answer) {
                [button setBackgroundImage:nil
                                  forState: UIControlStateNormal];
            }
        }
    }
}
 
- (void)viewDidLoad {
    [super viewDidLoad];
    [gameManager startAnew];
}
 
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
 
@end

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎