-
-
Save beelsebob/2761521 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import <Foundation/Foundation.h> | |
| @interface Coordinate : NSObject | |
| @property (nonatomic, assign) int x; | |
| @property (nonatomic, assign) int y; | |
| - (Coordinate *)initWithX:(int)_x y:(int)_y; | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "Coordinate.h" | |
| @implementation Coordinate | |
| @synthesize x; | |
| @synthesize y; | |
| - (Coordinate *)initWithX:(int)_x y:(int)_y | |
| { | |
| self = [super init]; | |
| if (nil != self) | |
| { | |
| [self setX:_x]; | |
| [self setY:_y]; | |
| } | |
| return self; | |
| } | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import <Foundation/Foundation.h> | |
| #import "Monster.h" | |
| #import "World.h" | |
| int main (int argc, const char * argv[]) | |
| { | |
| @autoreleasepool | |
| { | |
| World *mainWorld = [[World alloc] initWithNibNamed:@"World" bundle:nil]; | |
| BOOL gameLoop = YES; | |
| char userInput[255]; | |
| while (gameLoop) | |
| { | |
| printf("Current position: (%i, %i)\n", [[[mainWorld protagonist] coords] x], [[[mainWorld protagonist] coords] y]); | |
| printf("Movement (w, a, s, d, x [%i Remaining]): ", [[mainWorld protagonist] remainingScans]); | |
| scanf("%s", &userInput); | |
| printf("\n"); | |
| [[mainWorld protagonist] handleInputWithBuffer:*userInput worldObject:mainWorld]; | |
| for (Monster *tempMonster in [mainWorld monsters]) | |
| { | |
| [tempMonster lookForPlayerWithWorld:mainWorld player:[mainWorld protagonist]]; | |
| } | |
| if ([[mainWorld protagonist] isDead]) | |
| { | |
| printf("Game Over\nYou have been eaten!"); | |
| gameLoop = NO; | |
| } | |
| } | |
| [mainWorld release]; | |
| return 0; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import <Foundation/Foundation.h> | |
| #import "Coordinates.h" | |
| #import "World.h" | |
| #import "Player.h" | |
| @class Coordinates; | |
| @class World; | |
| @class Player; | |
| @interface Monster : NSObject | |
| { | |
| NSString *race; | |
| int level; | |
| int health; | |
| int lineOfSight; | |
| bool isAggressive; | |
| Coordinates *coords; | |
| } | |
| @property (nonatomic, assign) int level; | |
| @property (nonatomic, assign) int health; | |
| @property (nonatomic, assign) int lineOfSight; | |
| @property (nonatomic, assign) NSString *race; | |
| @property (nonatomic, assign) bool isAggressive; | |
| @property (nonatomic, assign) Coordinates *coords; | |
| -(Monster*) initWithRace: (NSString*) _race Level: (int) _level isAggressive: (bool) _isAggressive xPosition: (int) _x yPosition: (int) _y; | |
| -(void) printIfFound; | |
| -(void) lookForPlayerWithWorld: (World*) _world player: (Player *)_player; | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "Monster.h" | |
| @implementation Monster | |
| @synthesize level; | |
| @synthesize health; | |
| @synthesize lineOfSight; | |
| @synthesize race; | |
| @synthesize isAggressive; | |
| @synthesize coords; | |
| -(Monster*) initWithRace: (NSString*) _race Level: (int) _level isAggressive: (bool) _isAggressive xPosition: (int) _x yPosition: (int) _y | |
| { | |
| self = [super init]; | |
| if (self) | |
| { | |
| race = _race; | |
| level = _level; | |
| health = 100; | |
| lineOfSight = 2; | |
| isAggressive = _isAggressive; | |
| coords = [[Coordinates alloc] initWithPositionX: _x andY: _y]; | |
| } | |
| return self; | |
| } | |
| -(void) printIfFound | |
| { | |
| NSString *aggressiveness = (isAggressive) ? @"Aggressive" : @"Passive"; | |
| printf("%s level %i %s found at (%i, %i) \n", [aggressiveness UTF8String], level, [race UTF8String], coords.x, coords.y); | |
| } | |
| -(void) lookForPlayerWithWorld: (World *)_world player: (Player *)_player | |
| { | |
| if ([_world checkForOthersWithAimer: self Target: _player]) | |
| { | |
| [_player die]; | |
| } | |
| } | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import <Foundation/Foundation.h> | |
| #import "Coordinates.h" | |
| #import "World.h" | |
| enum direction | |
| { | |
| up = 1, | |
| down, | |
| left, | |
| right | |
| }; | |
| @class Coordinates; | |
| @class World; | |
| @class Monster; | |
| @interface Player : NSObject | |
| { | |
| NSString *race; | |
| int level; | |
| int health; | |
| int lineOfSight; | |
| int remainingScans; | |
| bool isDead; | |
| Coordinates *coords; | |
| } | |
| @property (nonatomic, assign) int level; | |
| @property (nonatomic, assign) int health; | |
| @property (nonatomic, assign) int lineOfSight; | |
| @property (nonatomic, assign) int remainingScans; | |
| @property (nonatomic, assign) bool isDead; | |
| @property (nonatomic, retain) NSString *race; | |
| @property (nonatomic, retain) Coordinates *coords; | |
| -(Player*) initWithRace: (NSString *) _race Level: (int) _level; | |
| -(void) handleInputWithBuffer: (char) _input worldObject: (World*) _world; | |
| -(void) moveWithDirection: (int) _direction worldObject: (World*) _world; | |
| -(int) die; | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "Player.h" | |
| @implementation Player | |
| @synthesize level; | |
| @synthesize health; | |
| @synthesize lineOfSight; | |
| @synthesize remainingScans; | |
| @synthesize isDead; | |
| @synthesize race; | |
| @synthesize coords; | |
| -(Player*) initWithRace: (NSString*) _race Level: (int) _level | |
| { | |
| self = [super init]; | |
| if (self) | |
| { | |
| race = _race; | |
| level = _level; | |
| health = 100; | |
| lineOfSight = 10; | |
| remainingScans = 10; | |
| isDead = false; | |
| coords = [[Coordinates alloc] initWithPositionX: 0 andY: 0]; | |
| } | |
| return self; | |
| } | |
| -(void) handleInputWithBuffer: (char) _input worldObject: (World*) _world | |
| { | |
| if (_input == 'w') | |
| [self moveWithDirection: up worldObject: _world]; | |
| else if (_input == 's') | |
| [self moveWithDirection: down worldObject: _world]; | |
| else if (_input == 'a') | |
| [self moveWithDirection: left worldObject: _world]; | |
| else if (_input == 'd') | |
| [self moveWithDirection: right worldObject: _world]; | |
| else if (_input == 'x') | |
| { | |
| if (remainingScans > 0) | |
| { | |
| bool foundMonster = false; | |
| for (Monster *tempMonster in [_world monsters]) | |
| { | |
| if ([_world checkForOthersWithAimer: self Target: tempMonster]) | |
| { | |
| [tempMonster printIfFound]; | |
| foundMonster = true; | |
| } | |
| } | |
| if (foundMonster == true) | |
| printf("\n"); | |
| else | |
| printf("No monsters found\n"); | |
| remainingScans--; | |
| } | |
| else | |
| printf("No scans remaining [Good Luck!]\n\n"); | |
| } | |
| } | |
| -(void) moveWithDirection: (int) _direction worldObject: (World*) _world | |
| { | |
| switch (_direction) | |
| { | |
| case 1: | |
| if(self.coords.y < _world.mapHeight) | |
| self.coords.y++; | |
| break; | |
| case 2: | |
| if(self.coords.y > 0) | |
| self.coords.y--; | |
| break; | |
| case 3: | |
| if(self.coords.x > 0) | |
| self.coords.x--; | |
| break; | |
| case 4: | |
| if(self.coords.x < _world.mapWidth) | |
| self.coords.x++; | |
| break; | |
| } | |
| } | |
| -(int) die | |
| { | |
| isDead = true; | |
| } | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import <Foundation/Foundation.h> | |
| #import "Monster.h" | |
| #import "Player.h" | |
| #import "Coordinates.h" | |
| @class Monster; | |
| @class Player; | |
| @class Coordinates; | |
| @interface World : NSObject | |
| { | |
| NSMutableArray *monsters; | |
| int mapWidth; | |
| int mapHeight; | |
| } | |
| @property (nonatomic, retain) NSMutableArray *monsters; | |
| @property (nonatomic, assign) int mapWidth; | |
| @property (nonatomic, assign) int mapHeight; | |
| -(int) checkForOthersWithAimer: (id) player Target: (id) monster; | |
| -(World*) initWithWidth: (int) _width Height: (int) _height; | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "World.h" | |
| @implementation World | |
| @synthesize monsters; | |
| @synthesize mapWidth; | |
| @synthesize mapHeight; | |
| -(World*) initWithWidth: (int) _width Height: (int) _height | |
| { | |
| self = [super init]; | |
| if (self) | |
| { | |
| monsters = [[NSMutableArray alloc] init]; | |
| mapWidth = _width; | |
| mapHeight = _height; | |
| } | |
| return self; | |
| } | |
| -(int) checkForOthersWithAimer: (id) aimer Target: (id) target | |
| { | |
| int lineOfSight = [aimer lineOfSight]; | |
| Coordinates *aimerCoords = [aimer coords]; | |
| Coordinates *targetCoords = [target coords]; | |
| int distance = sqrt(pow((targetCoords.x - aimerCoords.x), 2) + pow((targetCoords.y - aimerCoords.y), 2)); | |
| if (distance <= lineOfSight) | |
| return true; | |
| return false; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment