Skip to content

Commit

Permalink
initial armies defined by map;
Browse files Browse the repository at this point in the history
  • Loading branch information
smj10j committed May 1, 2012
1 parent 24d689d commit e46d52e
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 6 deletions.
11 changes: 8 additions & 3 deletions Projectfiles/EasyComputerPlayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ -(void)place {
//pick a random territory
NSArray* ownedTerritories = [map territoriesForPlayer:self];
originTerritory = [ownedTerritories objectAtIndex:(int)(arc4random()%[ownedTerritories count])];
uint ownedNeighborCount = [[originTerritory neighboringTerritoriesWithOwner:self] count];
NSLog(@"There are %d owned neighbors neighboring %@", ownedNeighborCount, originTerritory.name);
if(ownedNeighborCount == [[originTerritory neighboringTerritories] count]) {
return;
}

int armies = ceil(armiesToPlace/5.0f);
originTerritory.armies+= armies;
Expand All @@ -57,8 +62,8 @@ -(void)attack {
short originSelectAttemptCount = 0;
while((originTerritory == nil || originTerritory.armies < 2) && originSelectAttemptCount++ < 7) {
originTerritory = [ownedTerritories objectAtIndex:(int)(arc4random()%[ownedTerritories count])];
uint ownedNeighborCount = [[originTerritory neighboringTerritoriesForPlayer:self] count];
if(ownedNeighborCount == [[originTerritory neighboringTerritories] count]) {
uint attackableNeighborsCount = [[originTerritory neighboringTerritoriesWithoutOwner:self] count];
if(attackableNeighborsCount == 0) {
originTerritory = nil;
}
}
Expand All @@ -68,7 +73,7 @@ -(void)attack {
destinationTerritory = [originTerritory.neighboringTerritories objectAtIndex:(int)(arc4random()%[originTerritory.neighboringTerritories count])];
}

if(destinationTerritory.owner == self) {
if(destinationTerritory.owner == self || originTerritory == nil || destinationTerritory == nil) {
return;
}

Expand Down
2 changes: 2 additions & 0 deletions Projectfiles/Map.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
NSMutableDictionary* locationsWithColor;
NSMutableDictionary* territoryWithColor;
NSDictionary* properties;
int initialArmiesPerTerritory;
int armiesPerTurn;
int territoriesForAdditionalArmyPerTurn;

Expand All @@ -47,6 +48,7 @@
@property (strong, nonatomic) CCNode* HUD;

@property (strong, nonatomic) NSDictionary* properties;
@property (nonatomic) int initialArmiesPerTerritory;
@property (nonatomic) int armiesPerTurn;
@property (nonatomic) int territoriesForAdditionalArmyPerTurn;

Expand Down
2 changes: 2 additions & 0 deletions Projectfiles/Map.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ @implementation Map
@synthesize continents;

@synthesize properties;
@synthesize initialArmiesPerTerritory;
@synthesize armiesPerTurn;
@synthesize territoriesForAdditionalArmyPerTurn;

Expand Down Expand Up @@ -92,6 +93,7 @@ -(void)initializeMapData {
exit(1);
}

initialArmiesPerTerritory = [(NSString*)([properties objectForKey:@"InitialArmiesPerTerritory"]) intValue];
armiesPerTurn = [(NSString*)([properties objectForKey:@"ArmiesPerTurn"]) intValue];
territoriesForAdditionalArmyPerTurn = [(NSString*)([properties objectForKey:@"TerritoriesForAdditionalArmyPerTurn"]) intValue];
NSLog(@"Armies per turn: %d", armiesPerTurn);
Expand Down
1 change: 1 addition & 0 deletions Projectfiles/Resources/Maps/Conquer/Properties.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"InitialArmiesPerTerritory": 4,
"ArmiesPerTurn": 3,
"TerritoriesForAdditionalArmyPerTurn": 3,
"Continents": {
Expand Down
3 changes: 2 additions & 1 deletion Projectfiles/Territory.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
-(NSArray*)locations;
-(NSArray*)borderLocations;

-(NSArray*)neighboringTerritoriesForPlayer:(Player*)player;
-(NSArray*)neighboringTerritoriesWithOwner:(Player*)player;
-(NSArray*)neighboringTerritoriesWithoutOwner:(Player*)player;

-(BOOL)isNeighborTo:(Territory*)territory;

Expand Down
14 changes: 12 additions & 2 deletions Projectfiles/Territory.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ -(id)initWithColor:(UInt32)theColor name:(NSString*)theName onContinent:(Contine
labelArmies = [CCLabelTTF labelWithString:@"" fontName:@"Marker Felt" fontSize:24];
[map.HUD addChild: labelArmies];

[self setArmies:1];
[self setArmies:map.initialArmiesPerTerritory];

NSLog(@"Created territory %@ on continent %@", name, continent.name);
}
Expand Down Expand Up @@ -139,7 +139,17 @@ -(NSArray*)borderLocations {
return borderLocations;
}

-(NSArray*)neighboringTerritoriesForPlayer:(Player*)player {
-(NSArray*)neighboringTerritoriesWithOwner:(Player*)player {
NSMutableArray* territoriesArray = [[NSMutableArray alloc] init];
for(Territory* territory in neighboringTerritories) {
if(territory.owner == owner) {
[territoriesArray addObject:territory];
}
}
return territoriesArray;
}

-(NSArray*)neighboringTerritoriesWithoutOwner:(Player*)player {
NSMutableArray* territoriesArray = [[NSMutableArray alloc] init];
for(Territory* territory in neighboringTerritories) {
if(territory.owner != owner) {
Expand Down

0 comments on commit e46d52e

Please sign in to comment.