ftp.nice.ch/pub/next/games/board/Risk.0.97.s.tar.gz#/RiskSource0.97/Risk/ComputerLogic.m

This is ComputerLogic.m in view mode; [Download] [Up]

// ComputerLogic.m
// Part of Risk by Mike Ferris

#import "ComputerLogic.h"
#import "MapView.h"
#import "Mover.h"
#import "RiskController.h"
#import "GameSetup.h"
#import "Country.h"
#import "Random.h"
#import <objc/List.h>
#import <sys/time.h>
#import <libc.h>
#import <math.h>

@implementation ComputerLogic

+ initialize
{
	if (self == [ComputerLogic class])  {
		[self setVersion:1];
	}
	return self;
}

- init
{
	self = [super init];
	rng = [[Random allocFromZone:[self zone]] init];
	
	return self;
}

- free
{
	[rng free];
	return [super free];
}

- randomCountries:sender
{
	id cl;
	long int i;
	int pl=0;
	
	while ([theGameSetup strategyOfPlayer:pl] == S_NOTPLAYING)  {
		pl = (pl+1)%6;
	}
	cl = [[theMapView countryList] copy];
	for (i=0; i<[cl count]; )  {
		if ([[cl objectAt:i] player] != -1)  {
			// take it out of the list and don't advance the counter
			[cl removeObjectAt:i];
		}  else  {
			// leave it and advance the counter
			i++;
		}
	}
	while ([cl count]>0)  {
		i = [rng randMax:[cl count]-1];
		[[cl removeObjectAt:i] setPlayer:pl andArmies:1];
		pl = (pl+1)%6;
		while ([theGameSetup strategyOfPlayer:pl] == S_NOTPLAYING)  {
			pl = (pl+1)%6;
		}
	}
	[cl free];

    return self;
}

-rollDiceAttacker:(int *)aDice :(int)aNumDice 
		 defender:(int *)dDice :(int)dNumDice
// aDice will be three ints big, dDice will be 2 ints big
{
	int i;
	int temp1=0, temp2=0, temp3=0;
	// fill the arrays
	for (i=0;i<aNumDice;i++)  {
		aDice[i]=[rng rollDie:6];
	}
	for (i=aNumDice;i<3;i++)  {
		aDice[i]=0;
	}
	for (i=0;i<dNumDice;i++)  {
		dDice[i]=[rng rollDie:6];
	}
	for (i=dNumDice;i<2;i++)  {
		dDice[i]=0;
	}
	// sort the arrays
	if ((aDice[0]>=aDice[1]) && (aDice[0]>=aDice[2]))  {
		temp1 = aDice[0];
		if (aDice[1]>=aDice[2])  {
			temp2 = aDice[1];
			temp3 = aDice[2];
		}  else  {
			temp2 = aDice[2];
			temp3 = aDice[1];
		}
	}  else if ((aDice[1]>=aDice[0]) && (aDice[1]>=aDice[2]))  {
		temp1 = aDice[1];
		if (aDice[0]>=aDice[2])  {
			temp2 = aDice[0];
			temp3 = aDice[2];
		}  else  {
			temp2 = aDice[2];
			temp3 = aDice[0];
		}
	}  else if ((aDice[2]>=aDice[0]) && (aDice[2]>=aDice[1]))  {
		temp1 = aDice[2];
		if (aDice[0]>=aDice[1])  {
			temp2 = aDice[0];
			temp3 = aDice[1];
		}  else  {
			temp2 = aDice[1];
			temp3 = aDice[0];
		}
	}
	aDice[0]=temp1; aDice[1]=temp2; aDice[2]=temp3;
	if (dDice[1]>dDice[0])  {
		temp1 = dDice[1];
		dDice[1]=dDice[0];
		dDice[0]=temp1;
	}
	return self;
}

@end

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.