Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit d87180fa2e15fcf5e086ed3d338b1f50c43239f4 @lxcid committed Jul 12, 2011
@@ -0,0 +1,4 @@
+.DS_Store
+build
+*.mode1v3
+*.pbxuser
@@ -0,0 +1,56 @@
+//
+// MyViewController.h
+// TicTacToe
+//
+// Created by Stan Chang Khin Boon on 4/10/10.
+// Copyright 2010 __MyCompanyName__. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+
+
+@interface MyViewController : UIViewController
+{
+ UIButton * grid00;
+ UIButton * grid01;
+ UIButton * grid02;
+ UIButton * grid10;
+ UIButton * grid11;
+ UIButton * grid12;
+ UIButton * grid20;
+ UIButton * grid21;
+ UIButton * grid22;
+ UILabel * message;
+ NSMutableArray * grids;
+ NSString * who;
+ BOOL won;
+}
+
+@property ( nonatomic, retain ) IBOutlet UIButton * grid00;
+@property ( nonatomic, retain ) IBOutlet UIButton * grid01;
+@property ( nonatomic, retain ) IBOutlet UIButton * grid02;
+@property ( nonatomic, retain ) IBOutlet UIButton * grid10;
+@property ( nonatomic, retain ) IBOutlet UIButton * grid11;
+@property ( nonatomic, retain ) IBOutlet UIButton * grid12;
+@property ( nonatomic, retain ) IBOutlet UIButton * grid20;
+@property ( nonatomic, retain ) IBOutlet UIButton * grid21;
+@property ( nonatomic, retain ) IBOutlet UIButton * grid22;
+@property ( nonatomic, retain ) IBOutlet UILabel * message;
+@property ( nonatomic, copy ) NSString * who;
+
+- ( IBAction ) selectGrid00: ( id ) sender;
+- ( IBAction ) selectGrid01: ( id ) sender;
+- ( IBAction ) selectGrid02: ( id ) sender;
+- ( IBAction ) selectGrid10: ( id ) sender;
+- ( IBAction ) selectGrid11: ( id ) sender;
+- ( IBAction ) selectGrid12: ( id ) sender;
+- ( IBAction ) selectGrid20: ( id ) sender;
+- ( IBAction ) selectGrid21: ( id ) sender;
+- ( IBAction ) selectGrid22: ( id ) sender;
+
+- ( void ) changeTurn;
+- ( void ) updateGrids;
+- ( BOOL ) winLiao;
+- ( BOOL ) checkFirst: ( NSString * ) first second: ( NSString * ) second third: ( NSString * ) third;
+
+@end
@@ -0,0 +1,234 @@
+//
+// MyViewController.m
+// TicTacToe
+//
+// Created by Stan Chang Khin Boon on 4/10/10.
+// Copyright 2010 __MyCompanyName__. All rights reserved.
+//
+
+#import "MyViewController.h"
+
+
+@implementation MyViewController
+
+@synthesize grid00;
+@synthesize grid01;
+@synthesize grid02;
+@synthesize grid10;
+@synthesize grid11;
+@synthesize grid12;
+@synthesize grid20;
+@synthesize grid21;
+@synthesize grid22;
+@synthesize message;
+@synthesize who;
+
+- ( void ) viewDidLoad
+{
+ [super viewDidLoad];
+
+ grids = [[NSMutableArray alloc] initWithObjects: @"", @"", @"", @"", @"", @"", @"", @"", @"", nil];
+ [self changeTurn];
+ won = FALSE;
+}
+
+- ( IBAction ) selectGrid00: ( id ) sender
+{
+ if ( [[grids objectAtIndex: 0] isEqual: @""] )
+ {
+ [grids removeObjectAtIndex: 0];
+ [grids insertObject: self.who atIndex: 0];
+ [self updateGrids];
+ [self changeTurn];
+ }
+}
+
+- ( IBAction ) selectGrid01: ( id ) sender
+{
+ if ( [[grids objectAtIndex: 1] isEqual: @""] )
+ {
+ [grids removeObjectAtIndex: 1];
+ [grids insertObject: self.who atIndex: 1];
+ [self updateGrids];
+ [self changeTurn];
+ }
+}
+
+- ( IBAction ) selectGrid02: ( id ) sender
+{
+ if ( [[grids objectAtIndex: 2] isEqual: @""] )
+ {
+ [grids removeObjectAtIndex: 2];
+ [grids insertObject: self.who atIndex: 2];
+ [self updateGrids];
+ [self changeTurn];
+ }
+}
+
+- ( IBAction ) selectGrid10: ( id ) sender
+{
+ if ( [[grids objectAtIndex: 3] isEqual: @""] )
+ {
+ [grids removeObjectAtIndex: 3];
+ [grids insertObject: self.who atIndex: 3];
+ [self updateGrids];
+ [self changeTurn];
+ }
+}
+
+- ( IBAction ) selectGrid11: ( id ) sender
+{
+ if ( [[grids objectAtIndex: 4] isEqual: @""] )
+ {
+ [grids removeObjectAtIndex: 4];
+ [grids insertObject: self.who atIndex: 4];
+ [self updateGrids];
+ [self changeTurn];
+ }
+}
+
+- ( IBAction ) selectGrid12: ( id ) sender
+{
+ if ( [[grids objectAtIndex: 5] isEqual: @""] )
+ {
+ [grids removeObjectAtIndex: 5];
+ [grids insertObject: self.who atIndex: 5];
+ [self updateGrids];
+ [self changeTurn];
+ }
+}
+
+- ( IBAction ) selectGrid20: ( id ) sender
+{
+ if ( [[grids objectAtIndex: 6] isEqual: @""] )
+ {
+ [grids removeObjectAtIndex: 6];
+ [grids insertObject: self.who atIndex: 6];
+ [self updateGrids];
+ [self changeTurn];
+ }
+}
+
+- ( IBAction ) selectGrid21: ( id ) sender
+{
+ if ( [[grids objectAtIndex: 7] isEqual: @""] )
+ {
+ [grids removeObjectAtIndex: 7];
+ [grids insertObject: self.who atIndex: 7];
+ [self updateGrids];
+ [self changeTurn];
+ }
+}
+
+- ( IBAction ) selectGrid22: ( id ) sender
+{
+ if ( [[grids objectAtIndex: 8] isEqual: @""] )
+ {
+ [grids removeObjectAtIndex: 8];
+ [grids insertObject: self.who atIndex: 8];
+ [self updateGrids];
+ [self changeTurn];
+ }
+}
+
+- ( void ) changeTurn
+{
+ if ( [self.who isEqual: @"X"] )
+ {
+ self.who = @"O";
+ }
+ else
+ {
+ self.who = @"X";
+ }
+
+}
+
+- ( void ) updateGrids
+{
+ [grid00 setTitle: [grids objectAtIndex: 0] forState: UIControlStateNormal ];
+ [grid01 setTitle: [grids objectAtIndex: 1] forState: UIControlStateNormal ];
+ [grid02 setTitle: [grids objectAtIndex: 2] forState: UIControlStateNormal ];
+ [grid10 setTitle: [grids objectAtIndex: 3] forState: UIControlStateNormal ];
+ [grid11 setTitle: [grids objectAtIndex: 4] forState: UIControlStateNormal ];
+ [grid12 setTitle: [grids objectAtIndex: 5] forState: UIControlStateNormal ];
+ [grid20 setTitle: [grids objectAtIndex: 6] forState: UIControlStateNormal ];
+ [grid21 setTitle: [grids objectAtIndex: 7] forState: UIControlStateNormal ];
+ [grid22 setTitle: [grids objectAtIndex: 8] forState: UIControlStateNormal ];
+
+ if ( [self winLiao] )
+ {
+ message.text = [NSString stringWithFormat: @"%@ win!!!", who];
+ }
+}
+
+- ( BOOL ) winLiao
+{
+ if ( !won )
+ {
+ if ( [self checkFirst: [grids objectAtIndex: 0] second: [grids objectAtIndex: 1] third: [grids objectAtIndex: 2]] )
+ {
+ won = TRUE;
+ return TRUE;
+ }
+ else if ( [self checkFirst: [grids objectAtIndex: 3] second: [grids objectAtIndex: 4] third: [grids objectAtIndex: 5]] )
+ {
+ won = TRUE;
+ return TRUE;
+ }
+ else if ( [self checkFirst: [grids objectAtIndex: 6] second: [grids objectAtIndex: 7] third: [grids objectAtIndex: 8]] )
+ {
+ won = TRUE;
+ return TRUE;
+ }
+ else if ( [self checkFirst: [grids objectAtIndex: 0] second: [grids objectAtIndex: 3] third: [grids objectAtIndex: 6]] )
+ {
+ won = TRUE;
+ return TRUE;
+ }
+ else if ( [self checkFirst: [grids objectAtIndex: 1] second: [grids objectAtIndex: 4] third: [grids objectAtIndex: 7]] )
+ {
+ won = TRUE;
+ return TRUE;
+ }
+ else if ( [self checkFirst: [grids objectAtIndex: 2] second: [grids objectAtIndex: 5] third: [grids objectAtIndex: 8]] )
+ {
+ won = TRUE;
+ return TRUE;
+ }
+ else if ( [self checkFirst: [grids objectAtIndex: 0] second: [grids objectAtIndex: 4] third: [grids objectAtIndex: 8]] )
+ {
+ won = TRUE;
+ return TRUE;
+ }
+ else if ( [self checkFirst: [grids objectAtIndex: 2] second: [grids objectAtIndex: 4] third: [grids objectAtIndex: 6]] )
+ {
+ won = TRUE;
+ return TRUE;
+ }
+ else
+ {
+ return FALSE;
+ }
+
+
+ }
+ else
+ {
+ return FALSE;
+ }
+}
+
+- ( BOOL ) checkFirst: ( NSString * ) first second: ( NSString * ) second third: ( NSString * ) third;
+{
+ return ( [who isEqual: first] && [who isEqual: second] && [who isEqual: third] );
+}
+
+- (void)dealloc
+{
+ [grids release];
+ [super dealloc];
+}
+
+
+@end
@@ -0,0 +1,22 @@
+//
+// TicTacToeAppDelegate.h
+// TicTacToe
+//
+// Created by Stan Chang Khin Boon on 4/10/10.
+// Copyright __MyCompanyName__ 2010. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+#import "MyViewController.h"
+
+@interface TicTacToeAppDelegate : NSObject <UIApplicationDelegate>
+{
+ UIWindow * window;
+ MyViewController * myViewController;
+}
+
+@property ( nonatomic, retain ) IBOutlet UIWindow * window;
+@property ( nonatomic, retain ) MyViewController * myViewController;
+
+@end
+
@@ -0,0 +1,37 @@
+//
+// TicTacToeAppDelegate.m
+// TicTacToe
+//
+// Created by Stan Chang Khin Boon on 4/10/10.
+// Copyright __MyCompanyName__ 2010. All rights reserved.
+//
+
+#import "TicTacToeAppDelegate.h"
+
+@implementation TicTacToeAppDelegate
+
+@synthesize window;
+@synthesize myViewController;
+
+- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
+{
+ MyViewController * aViewController = [[MyViewController alloc] initWithNibName: @"MyViewController" bundle: [NSBundle mainBundle]];
+ [self setMyViewController: aViewController];
+ [aViewController release];
+ UIView * controllerView = [myViewController view];
+ [window addSubview: controllerView];
+ [window makeKeyAndVisible];
+
+ return YES;
+}
+
+
+- (void)dealloc
+{
+ [myViewController release];
+ [window release];
+ [super dealloc];
+}
+
+
+@end
Oops, something went wrong.

0 comments on commit d87180f

Please sign in to comment.