This code currently works, but I'm new to Objective-C from a Python/PHP background. How can I improve it/make it more Objective-Cesque?
Header File/Interface
#import <Foundation/Foundation.h>
@interface Monster : NSObject
@property NSString* name;
@property NSNumber* health;
@property NSNumber* strength;
@property NSNumber* dexterity;
@property NSNumber* magic;
//
- (id) initWithName: (NSString*) nameToBeConstructedWithMethodConstructorInitWithName;
- (void) attack: (Monster*) sentMonster blocking:(BOOL)block isWithMagic:(BOOL)magicYes andMagicType:(NSString*)whatMagic andBurn:(NSInteger)burning;
- (bool) isDead;
- (void) printInfo;
@end
Implementation File
#import "Monster.h"
@implementation Monster
- (id) initWithName: (NSString*) nameToBeConstructedWithMethodConstructorInitWithName{
self = [super init];
if (self){
_name = nameToBeConstructedWithMethodConstructorInitWithName;
_dexterity = @((arc4random() % 15) + 3);
_strength = @((arc4random() % 15) + 3);
_magic = @((arc4random() % 15) + 3);
_health = @((arc4random() % 40) + 60);
}
return self;
}
- (void) attack: (Monster*) sentMonster blocking:(BOOL)block isWithMagic:(BOOL)magicYes andMagicType:(NSString*)whatMagic andBurn:(NSInteger)burning{
NSInteger health = [sentMonster.health intValue];
NSInteger modifier = [self.strength intValue];
if (modifier > 5) modifier = 1;
if (modifier > 10) modifier = 2;
if (modifier > 15) modifier = 5;
NSInteger hit = 0;
if (magicYes){
if ([whatMagic characterAtIndex:0] == 'i'){
NSInteger baseHit = ((arc4random() % 15) + 5);
NSLog(@"%@'s health: %ld", sentMonster.name, health);
hit = baseHit + modifier;
health -= hit;
sentMonster.health = @(health);
NSLog(@"%@ was hit for %ld magical damage and has a new health of %ld", sentMonster.name, hit, health);
}
if ([whatMagic characterAtIndex:0] == 'f'){
NSInteger baseHit = ((arc4random() % 25) + 10);
NSLog(@"%@'s health: %ld", sentMonster.name, health);
hit = baseHit + modifier;
if (burning) health -= ((arc4random()% 7) + 3);
health -= hit;
sentMonster.health = @(health);
NSLog(@"%@ was hit for %ld magical damage and has a new health of %ld", sentMonster.name, hit, health);
}
}
else if (((arc4random() % 20) + [sentMonster.dexterity intValue]) >= 11){
NSInteger baseHit = ((arc4random() % 3)+2);
NSLog(@"%@'s health: %ld", sentMonster.name, health);
NSLog(@"Basehit: %ld",baseHit);
NSLog(@"Modifier: %ld:",modifier);
if (block) hit = baseHit;
else hit = baseHit + modifier;
NSLog(@"Hit: %ld", hit);
health -= hit;
sentMonster.health = @(health);
NSLog(@"%@ was hit for %ld physical damage and has a new health of %ld", sentMonster.name, hit, health);
}
else {
NSLog(@"%@ successfully dodged an attack", sentMonster.name);
}
}
- (bool) isDead{
if ([self.health intValue] <= 0)return YES;
return NO;
}
- (void) printInfo{
NSLog(@"\nName: %@\nHealth: %@\nStrength: %@\nDexterity: %@\nMagic: %@\n",self.name, self.health, self.strength, self.dexterity, self.magic);
}
@end
Main
#import <Foundation/Foundation.h>
#import "Monster.h"
NSString* getNSString(NSString *prompt);
int main()
{
@autoreleasepool {
Monster* player = [[Monster alloc]initWithName:getNSString(@"What is the player's name:\n")];
Monster* monster = [[Monster alloc]initWithName:@"Brad"];
BOOL whichMonsterWon,skipMonsterAttack,isCast;
NSInteger chanceSkip, chanceBurn;
NSString* magicType = getNSString(@"Do you use (I)ce or (F)ire magic");
magicType = [magicType lowercaseString];
NSLog(@"\n\n\n");
[player printInfo];
[monster printInfo];
while (YES){
chanceSkip = 0;
skipMonsterAttack = NO;
if ([monster isDead]){
whichMonsterWon = YES;
break;
}
if ([player isDead]){
whichMonsterWon = NO;
break;
}
NSString* playerChoice = getNSString(@"\n(A)ttack\n(B)lock\n(M)agic\n");
playerChoice = [playerChoice lowercaseString];
if ((isCast) && ([playerChoice characterAtIndex:0] == 'm')) NSLog(@"You have already cast your magic!");
else if ([playerChoice characterAtIndex:0] == 'a') [player attack:monster blocking:NO isWithMagic:NO andMagicType:magicType andBurn:chanceBurn];
else if ([playerChoice characterAtIndex:0] == 'b'){
[monster attack:player blocking:YES isWithMagic:NO andMagicType:magicType andBurn:chanceBurn];
skipMonsterAttack = YES;
}
else if (([playerChoice characterAtIndex:0] == 'm') && !isCast){
if ([magicType characterAtIndex:0] == 'i'){
chanceSkip = arc4random() % 2;
if (chanceSkip) NSLog(@"%@ was stunned for one turn",monster.name);
}
if ([magicType characterAtIndex:0] == 'f'){
chanceBurn = arc4random() % 2;
if (chanceBurn) NSLog(@"%@ was burned for one turn",monster.name);
}
[player attack:monster blocking:NO isWithMagic:YES andMagicType:magicType andBurn:chanceBurn];
chanceBurn = 0;
isCast = YES;
}
else NSLog(@"You have entered an unknown command, %@ gets a free turn", monster.name);
if (!(skipMonsterAttack) && !chanceSkip) [monster attack:player blocking:NO isWithMagic:NO andMagicType:magicType andBurn:chanceBurn];
}
[player printInfo];
[monster printInfo];
if (whichMonsterWon == YES) NSLog(@"%@ beat the monster!",player.name);
if (whichMonsterWon == NO) NSLog(@"%@ beat the player!",monster.name);
}
return 0;
}
NSString* getNSString(NSString *prompt){
NSLog(@"%@", prompt);
char cString[100] = "";
scanf("%s", cString);
NSString* oString = [NSString stringWithUTF8String:cString];
return oString;
}