Skip to content

Commit c47b1aa

Browse files
committed
Merge pull request #1 from mariancerny/master
Added #18 Game of Life in Objective C.
2 parents 028bb2f + b937e0f commit c47b1aa

File tree

5 files changed

+172
-0
lines changed

5 files changed

+172
-0
lines changed

018-objective-c/DLCell.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// DLCell.h
3+
// CodingDojo18
4+
//
5+
// Created by Marián Černý on 31.07.14.
6+
// Copyright (c) 2014 Pematon, s.r.o. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
@interface DLCell : NSObject
12+
13+
@property (assign, nonatomic) BOOL alive;
14+
15+
- (instancetype)initWithAlive:(BOOL)alive; // designated initializer
16+
17+
- (BOOL)shouldBeDeadInNextGenerationWithNeighboursCount:(NSInteger)neighboursCount;
18+
19+
@end

018-objective-c/DLCell.m

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// DLCell.m
3+
// CodingDojo18
4+
//
5+
// Created by Marián Černý on 31.07.14.
6+
// Copyright (c) 2014 Pematon, s.r.o. All rights reserved.
7+
//
8+
9+
#import "DLCell.h"
10+
11+
@implementation DLCell
12+
13+
- (instancetype)initWithAlive:(BOOL)alive
14+
{
15+
self = [super init];
16+
if (self) {
17+
_alive = alive;
18+
}
19+
return self;
20+
}
21+
22+
- (BOOL)shouldBeDeadInNextGenerationWithNeighboursCount:(NSInteger)neighboursCount
23+
{
24+
if (self.alive)
25+
{
26+
if (neighboursCount > 1 && neighboursCount <= 3) {
27+
return NO;
28+
}
29+
30+
return YES;
31+
}
32+
33+
return neighboursCount != 3;
34+
}
35+
36+
@end

018-objective-c/DLWorld.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// DLWorld.h
3+
// CodingDojo18
4+
//
5+
// Created by Marián Černý on 31.07.14.
6+
// Copyright (c) 2014 Pematon, s.r.o. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
@interface DLWorld : NSObject
12+
13+
@end

018-objective-c/DLWorld.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// DLWorld.m
3+
// CodingDojo18
4+
//
5+
// Created by Marián Černý on 31.07.14.
6+
// Copyright (c) 2014 Pematon, s.r.o. All rights reserved.
7+
//
8+
9+
#import "DLWorld.h"
10+
11+
@implementation DLWorld
12+
13+
@end

018-objective-c/GameOfLifeTests.m

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
//
2+
// CodingDojo18Tests.m
3+
// CodingDojo18Tests
4+
//
5+
// Created by Marián Černý on 31.07.14.
6+
// Copyright (c) 2014 Pematon, s.r.o. All rights reserved.
7+
//
8+
9+
#import <XCTest/XCTest.h>
10+
11+
#import "DLWorld.h"
12+
#import "DLCell.h"
13+
14+
@interface CodingDojo18Tests : XCTestCase
15+
16+
@end
17+
18+
@implementation CodingDojo18Tests
19+
20+
// Any live cell with fewer than two live neighbours dies, as if caused by under-population.
21+
// Any live cell with two or three live neighbours lives on to the next generation.
22+
// Any live cell with more than three live neighbours dies, as if by overcrowding.
23+
// Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
24+
25+
26+
- (void)cellThatIsAlive:(BOOL)alive withNeighbours:(NSInteger)neighbours shouldDie:(BOOL)shouldDie
27+
{
28+
DLCell *cell = [[DLCell alloc] initWithAlive:alive];
29+
XCTAssertEqual([cell shouldBeDeadInNextGenerationWithNeighboursCount:neighbours], shouldDie, @"Alive cell %d with %d neighbours should die %d", (int)alive, (int)neighbours, (int)shouldDie);
30+
}
31+
32+
- (void)aliveCellWithNeighbours:(NSInteger)neighbours shouldDie:(BOOL)shouldDie
33+
{
34+
[self cellThatIsAlive:YES withNeighbours:neighbours shouldDie:shouldDie];
35+
}
36+
37+
- (void)deadCellWithNeighbours:(NSInteger)neighbours shouldDie:(BOOL)shouldDie
38+
{
39+
[self cellThatIsAlive:NO withNeighbours:neighbours shouldDie:shouldDie];
40+
}
41+
42+
// Any live cell with fewer than two live neighbours dies, as if caused by under-population.
43+
- (void)testUnderPopulation
44+
{
45+
[self aliveCellWithNeighbours:0 shouldDie:YES];
46+
[self aliveCellWithNeighbours:1 shouldDie:YES];
47+
}
48+
49+
// Any live cell with two or three live neighbours lives on to the next generation.
50+
- (void)testSurvival
51+
{
52+
[self aliveCellWithNeighbours:2 shouldDie:NO];
53+
[self aliveCellWithNeighbours:3 shouldDie:NO];
54+
}
55+
56+
// Any live cell with more than three live neighbours dies, as if by overcrowding.
57+
- (void)testOvercrowding
58+
{
59+
[self aliveCellWithNeighbours:4 shouldDie:YES];
60+
[self aliveCellWithNeighbours:5 shouldDie:YES];
61+
[self aliveCellWithNeighbours:6 shouldDie:YES];
62+
[self aliveCellWithNeighbours:7 shouldDie:YES];
63+
[self aliveCellWithNeighbours:8 shouldDie:YES];
64+
}
65+
66+
// Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
67+
- (void)testReproduction
68+
{
69+
[self deadCellWithNeighbours:3 shouldDie:NO];
70+
71+
[self deadCellWithNeighbours:2 shouldDie:YES];
72+
[self deadCellWithNeighbours:4 shouldDie:YES];
73+
}
74+
75+
- (void)testCanCreateWorld
76+
{
77+
DLWorld *world = [[DLWorld alloc] init];
78+
79+
XCTAssertNotNil(world, @"there is world");
80+
}
81+
82+
- (void)testWorldHasDeadCellsByDefault
83+
{
84+
DLWorld *world = [[DLWorld alloc] init];
85+
86+
DLCell *cell = [world cellAtX:0 andY:0];
87+
88+
XCTAssertFalse(cell.alive, @"cell should be dead");
89+
}
90+
91+
@end

0 commit comments

Comments
 (0)