Skip to content
Browse files

The game is now playable.

  • Loading branch information...
1 parent 6fe175e commit d00584d39e117646cdc73dbf26ff94939f43fc0f Brian Zwahr committed
View
2 SlideGame/SlideGame.h
@@ -14,6 +14,8 @@
NSArray *correctArray;
}
+@property bool active;
+
- (id) init;
- (void) correctTiles;
- (void) setPosition: (NSUInteger)x : (NSUInteger)y : (NSNumber*)value;
View
71 SlideGame/SlideGame.m
@@ -10,6 +10,8 @@
@implementation SlideGame
+@synthesize active;
+
- (id) init
{
gameArray = [NSMutableArray arrayWithCapacity:4];
@@ -46,6 +48,8 @@ - (id) init
[NSNumber numberWithInt:0],
nil],
nil];
+
+ self.active = false;
return self;
}
@@ -97,13 +101,16 @@ - (NSArray*) findTilePosition: (NSInteger)value
break;
}
}
- return [NSArray arrayWithObjects:[NSNumber numberWithInt:x],
- [NSNumber numberWithInt:y],
- nil];
- }
+ NSArray* positionArray = [NSArray arrayWithObjects:[NSNumber numberWithInt:x],
+ [NSNumber numberWithInt:y],
+ nil];
+ return positionArray;
+}
- (void) randomizeTiles
{
+ bool isActive = self.active;
+ self.active = true;
[self correctTiles];
int numMoves = 100;
NSArray* zeroPosition;
@@ -163,43 +170,37 @@ - (void) randomizeTiles
lastX = x;
lastY = y;
}
+ self.active = isActive;
}
- (void) moveTile: (NSUInteger)x : (NSUInteger) y
{
- NSNumber *tileValue = [self getPosition:x :y];
- NSNumber *zero = [NSNumber numberWithInt:0];
- if (tileValue > 0) {
- // move up
- if (x > 0 && [[self getPosition:x - 1 :y] intValue] == 0) {
- [self setPosition:x - 1 :y :tileValue];
- [self setPosition:x :y :zero];
- }
- // move down
- if (x < 3 && [[self getPosition:x + 1 :y] intValue] == 0) {
- [self setPosition:x + 1 :y :tileValue];
- [self setPosition:x :y :zero];
- }
- // move left
- if (y > 0 && [[self getPosition:x :y - 1] intValue] == 0) {
- [self setPosition:x :y - 1 :tileValue];
- [self setPosition:x :y :zero];
- }
- // move right
- if (y < 3 && [[self getPosition:x :y + 1] intValue] == 0) {
- [self setPosition:x :y + 1 :tileValue];
- [self setPosition:x :y :zero];
+ if (self.active) {
+ NSNumber *tileValue = [self getPosition:x :y];
+ NSNumber *zero = [NSNumber numberWithInt:0];
+ if (tileValue > 0) {
+ // move up
+ if (x > 0 && [[self getPosition:x - 1 :y] intValue] == 0) {
+ [self setPosition:x - 1 :y :tileValue];
+ [self setPosition:x :y :zero];
+ }
+ // move down
+ if (x < 3 && [[self getPosition:x + 1 :y] intValue] == 0) {
+ [self setPosition:x + 1 :y :tileValue];
+ [self setPosition:x :y :zero];
+ }
+ // move left
+ if (y > 0 && [[self getPosition:x :y - 1] intValue] == 0) {
+ [self setPosition:x :y - 1 :tileValue];
+ [self setPosition:x :y :zero];
+ }
+ // move right
+ if (y < 3 && [[self getPosition:x :y + 1] intValue] == 0) {
+ [self setPosition:x :y + 1 :tileValue];
+ [self setPosition:x :y :zero];
+ }
}
}
-// if ([self isAWin]) {
-// UIAlertView *someError = [[UIAlertView alloc] initWithTitle: @"Good job!"
-// message: @"You won!"
-// delegate: self
-// cancelButtonTitle: @"Ok"
-// otherButtonTitles: nil];
-//
-// [someError show];
-// }
}
- (bool) isAWin
View
1 SlideGame/ViewController.h
@@ -10,5 +10,6 @@
@interface ViewController : UIViewController
- (IBAction)newGame:(id)sender;
+- (IBAction)pushTile:(id)sender;
@end
View
40 SlideGame/ViewController.m
@@ -11,6 +11,8 @@
@implementation ViewController
+SlideGame* game;
+
- (void)viewDidLoad
{
[super viewDidLoad];
@@ -34,24 +36,32 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface
- (IBAction)newGame:(id)sender
{
-// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"New Game!"
-// message:@"Start a new game"
-// delegate:nil
-// cancelButtonTitle:@"OK"
-// otherButtonTitles:nil];
-// [alert show];
-
- SlideGame* game = [[SlideGame alloc] init];
+ game = [[SlideGame alloc] init];
[game randomizeTiles];
- [self printBoard:game];
-// UIButton* button = (UIButton *)[self.view viewWithTag:15];
-// int top = 3 * 70 + 90;
-// int left = 3 * 70 + 20;
-// CGRect rect = CGRectMake(left, top, 70, 70);
-// [button setFrame:rect];
+ [self printBoard];
+ game.active = true;
+}
+
+- (IBAction)pushTile:(id)sender
+{
+ NSArray* position = [game findTilePosition:[sender tag]];
+ [game moveTile:[[position objectAtIndex:0] intValue]
+ :[[position objectAtIndex:1] intValue]];
+ [self printBoard];
+
+ if ([game isAWin]) {
+ UIAlertView *win = [[UIAlertView alloc] initWithTitle: @"Good job!"
+ message: @"You won!"
+ delegate: self
+ cancelButtonTitle: @"Ok"
+ otherButtonTitles: nil];
+
+ [win show];
+ game.active = false;
+ }
}
-- (void)printBoard:(SlideGame*)game
+- (void)printBoard
{
int tile;
UIButton* button;
View
46 SlideGame/en.lproj/MainStoryboard_iPhone.storyboard
@@ -43,6 +43,9 @@
<state key="highlighted">
<color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</state>
+ <connections>
+ <action selector="pushTile:" destination="2" eventType="touchUpInside" id="aOW-TV-MM1"/>
+ </connections>
</button>
<button opaque="NO" tag="2" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" id="elq-At-hSl" userLabel="Button2">
<rect key="frame" x="90" y="90" width="70" height="70"/>
@@ -55,6 +58,9 @@
<state key="highlighted">
<color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</state>
+ <connections>
+ <action selector="pushTile:" destination="2" eventType="touchUpInside" id="SlC-DQ-3Aa"/>
+ </connections>
</button>
<button opaque="NO" tag="3" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" id="SLf-m4-E5e" userLabel="Button3">
<rect key="frame" x="160" y="90" width="70" height="70"/>
@@ -67,6 +73,9 @@
<state key="highlighted">
<color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</state>
+ <connections>
+ <action selector="pushTile:" destination="2" eventType="touchUpInside" id="c4I-Jb-bl2"/>
+ </connections>
</button>
<button opaque="NO" tag="4" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" id="iYL-ye-FA8" userLabel="Button4">
<rect key="frame" x="230" y="90" width="70" height="70"/>
@@ -79,6 +88,9 @@
<state key="highlighted">
<color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</state>
+ <connections>
+ <action selector="pushTile:" destination="2" eventType="touchUpInside" id="UgD-di-s4h"/>
+ </connections>
</button>
<button opaque="NO" tag="5" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" id="l48-l7-OB7" userLabel="Button5">
<rect key="frame" x="20" y="160" width="70" height="70"/>
@@ -91,6 +103,9 @@
<state key="highlighted">
<color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</state>
+ <connections>
+ <action selector="pushTile:" destination="2" eventType="touchUpInside" id="SEf-re-KF7"/>
+ </connections>
</button>
<button opaque="NO" tag="6" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" id="mVy-iR-vEd" userLabel="Button6">
<rect key="frame" x="90" y="160" width="70" height="70"/>
@@ -103,6 +118,9 @@
<state key="highlighted">
<color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</state>
+ <connections>
+ <action selector="pushTile:" destination="2" eventType="touchUpInside" id="jq6-QV-Ack"/>
+ </connections>
</button>
<button opaque="NO" tag="7" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" id="sUp-OS-Y2R" userLabel="Button7">
<rect key="frame" x="160" y="160" width="70" height="70"/>
@@ -115,6 +133,9 @@
<state key="highlighted">
<color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</state>
+ <connections>
+ <action selector="pushTile:" destination="2" eventType="touchUpInside" id="W3O-Tc-z9B"/>
+ </connections>
</button>
<button opaque="NO" tag="8" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" id="vyh-aX-yNs" userLabel="Button8">
<rect key="frame" x="230" y="160" width="70" height="70"/>
@@ -127,6 +148,9 @@
<state key="highlighted">
<color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</state>
+ <connections>
+ <action selector="pushTile:" destination="2" eventType="touchUpInside" id="bhS-4N-YbC"/>
+ </connections>
</button>
<button opaque="NO" tag="9" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" id="OBF-0A-hUR" userLabel="Button9">
<rect key="frame" x="20" y="230" width="70" height="70"/>
@@ -139,6 +163,9 @@
<state key="highlighted">
<color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</state>
+ <connections>
+ <action selector="pushTile:" destination="2" eventType="touchUpInside" id="bEY-gC-SMQ"/>
+ </connections>
</button>
<button opaque="NO" tag="10" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" id="Pql-ow-NIz" userLabel="Button10">
<rect key="frame" x="90" y="230" width="70" height="70"/>
@@ -151,6 +178,9 @@
<state key="highlighted">
<color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</state>
+ <connections>
+ <action selector="pushTile:" destination="2" eventType="touchUpInside" id="UvY-Yb-FgS"/>
+ </connections>
</button>
<button opaque="NO" tag="11" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" id="LVs-ln-3Bv" userLabel="Button11">
<rect key="frame" x="160" y="230" width="70" height="70"/>
@@ -163,6 +193,9 @@
<state key="highlighted">
<color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</state>
+ <connections>
+ <action selector="pushTile:" destination="2" eventType="touchUpInside" id="Naw-xn-ru1"/>
+ </connections>
</button>
<button opaque="NO" tag="12" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" id="QdD-Qo-oA4" userLabel="Button12">
<rect key="frame" x="230" y="230" width="70" height="70"/>
@@ -175,6 +208,9 @@
<state key="highlighted">
<color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</state>
+ <connections>
+ <action selector="pushTile:" destination="2" eventType="touchUpInside" id="Gv1-nc-L3o"/>
+ </connections>
</button>
<button opaque="NO" tag="13" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" id="WKO-qc-b94" userLabel="Button13">
<rect key="frame" x="20" y="300" width="70" height="70"/>
@@ -187,6 +223,9 @@
<state key="highlighted">
<color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</state>
+ <connections>
+ <action selector="pushTile:" destination="2" eventType="touchUpInside" id="gGi-3M-ElY"/>
+ </connections>
</button>
<button opaque="NO" tag="14" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" id="oad-ef-DmU" userLabel="Button14">
<rect key="frame" x="90" y="300" width="70" height="70"/>
@@ -199,6 +238,9 @@
<state key="highlighted">
<color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</state>
+ <connections>
+ <action selector="pushTile:" destination="2" eventType="touchUpInside" id="U6G-kx-4qW"/>
+ </connections>
</button>
<button opaque="NO" tag="15" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" id="Fhk-2Y-jRM" userLabel="Button15">
<rect key="frame" x="160" y="300" width="70" height="70"/>
@@ -211,6 +253,9 @@
<state key="highlighted">
<color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</state>
+ <connections>
+ <action selector="pushTile:" destination="2" eventType="touchUpInside" id="axk-05-hyT"/>
+ </connections>
</button>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
@@ -224,6 +269,7 @@
<source key="sourceIdentifier" type="project" relativePath="./Classes/ViewController.h"/>
<relationships>
<relationship kind="action" name="newGame:"/>
+ <relationship kind="action" name="pushTile:"/>
</relationships>
</class>
</classes>
View
30 SlideGameTests/SlideGameTests.m
@@ -11,10 +11,12 @@
@implementation SlideGameTests
+SlideGame* game;
+
- (void)setUp
{
[super setUp];
-
+ game = [[SlideGame alloc] init];
// Set-up code here.
}
@@ -27,7 +29,6 @@ - (void)tearDown
- (void)testSettingBoardPositions
{
- SlideGame* game = [[SlideGame alloc] init];
[game setPosition:0 :0 :[NSNumber numberWithInt:100]];
[game setPosition:1 :0 :[NSNumber numberWithInt:101]];
[game setPosition:2 :1 :[NSNumber numberWithInt:102]];
@@ -41,8 +42,7 @@ - (void)testSettingBoardPositions
- (void)testMovingTiles
{
- SlideGame* game = [[SlideGame alloc] init];
-
+ game.active = true;
[game moveTile:0 :0];
STAssertEquals(1, [[game getPosition:0 :0] intValue], @"Wrong value");
@@ -82,9 +82,27 @@ - (void)testMovingTiles
STAssertEquals(12, [[game getPosition:2 :3] intValue], @"Wrong value");
}
+- (void)testFindingBoardPositions
+{
+ NSArray* position;
+
+ position = [game findTilePosition:1];
+ STAssertEquals(0, [[position objectAtIndex:0] intValue], @"Checking x position of 0");
+ STAssertEquals(0, [[position objectAtIndex:1] intValue], @"Checking y position of 0");
+
+ position = [game findTilePosition:15];
+ STAssertEquals(3, [[position objectAtIndex:0] intValue], @"Checking x position of 0");
+ STAssertEquals(2, [[position objectAtIndex:1] intValue], @"Checking y position of 0");
+
+ position = [game findTilePosition:0];
+ STAssertEquals(3, [[position objectAtIndex:0] intValue], @"Checking x position of 0");
+ STAssertEquals(3, [[position objectAtIndex:1] intValue], @"Checking y position of 0");
+
+}
+
- (void)testWinningBoard
{
- SlideGame* game = [[SlideGame alloc] init];
+ game.active = true;
STAssertTrue([game isAWin], @"Testing game is initially a win.");
[game moveTile:3 :2];
STAssertFalse([game isAWin], @"Testing game is not a win.");
@@ -94,7 +112,7 @@ - (void)testWinningBoard
- (void)testRandomBoard
{
- SlideGame* game = [[SlideGame alloc] init];
+ game.active = true;
[game randomizeTiles];
STAssertFalse([game isAWin], @"Random game should not be a win.");
}

0 comments on commit d00584d

Please sign in to comment.
Something went wrong with that request. Please try again.