Skip to content

Instantly share code, notes, and snippets.

Created May 21, 2012 09:26
Show Gist options
  • Select an option

  • Save anonymous/2761469 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/2761469 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
@interface Coordinates : NSObject
{
int x;
int y;
}
@property (nonatomic, assign) int x;
@property (nonatomic, assign) int y;
-(Coordinates*) initWithPositionX: (int) _x andY: (int) _y;
@end
#import "Coordinates.h"
@implementation Coordinates
@synthesize x;
@synthesize y;
-(Coordinates*) initWithPositionX: (int) _x andY: (int) _y
{
self = [super init];
if (self)
{
x = _x;
y = _y;
}
return self;
}
@end
#import <Foundation/Foundation.h>
#import "Monster.h"
#import "World.h"
/*
enum enumRace
{
Dragon,
Ogre,
Human,
Imp,
Spider
};
*/
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
World *mainWorld = [[World alloc] initWithWidth: 20 Height: 20];
Player *protagonist = [[Player alloc] initWithRace: @"Human" Level:1];
Monster *tempMonster = nil;
for (int i = 0; i < 2; i++)
{
tempMonster = [[Monster alloc] initWithRace : @"Dragon" Level: 5 isAggressive: true xPosition: arc4random() % mainWorld.mapWidth yPosition: arc4random() % mainWorld.mapHeight];
[mainWorld.monsters addObject : tempMonster];
tempMonster = [[Monster alloc] initWithRace : @"Ogre" Level: 3 isAggressive: true xPosition: arc4random() % mainWorld.mapWidth yPosition: arc4random() % mainWorld.mapHeight];
[mainWorld.monsters addObject : tempMonster];
tempMonster = [[Monster alloc] initWithRace : @"Human" Level: 7 isAggressive: false xPosition: arc4random() % mainWorld.mapWidth yPosition: arc4random() % mainWorld.mapHeight];
[mainWorld.monsters addObject : tempMonster];
}
bool gameLoop = true;
char userInput[255];
while (gameLoop)
{
printf("Current position: (%i, %i)\n", [[protagonist coords] x], [[protagonist coords] y]);
printf("Movement (w, a, s, d, x [%i Remaining]): ", [protagonist remainingScans]);
scanf("%s", &userInput);
printf("\n");
[protagonist handleInputWithBuffer: *userInput worldObject: mainWorld];
for (Monster *tempMonster in [mainWorld monsters])
{
[tempMonster lookForPlayerWithWorld: mainWorld player: protagonist];
}
if (protagonist.isDead)
{
printf("Game Over\nYou have been eaten!");
return 0;
}
}
[protagonist release];
[tempMonster release];
[mainWorld release];
[pool drain];
return 0;
}
#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
#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
#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
#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
#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
#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