
/* Generated by Interface Builder */

#import "CardsTracker.h"
#import	"GameCoordinator.h"
#import	<assert.h>
#import	<libc.h>
#import	<strings.h>


@implementation CardsTracker

- cardPlayed:aCard
{

	TextFieldCell	*tCell = [ cardsPlayedMatrix findCellWithTag:[ aCard tag ]];
	char			*dataField = alloca( strlen([ tCell stringValue ]) + 8 );
	
	
	sprintf( dataField, "(%d) %s", ([ self numPlayed:[ aCard tag ]] + 1 ), strstr([ tCell stringValue ], "of" ));
	[ tCell setStringValue:dataField ];
	
    return self;
}

- newHand
{

	int		i;
	
	
	for( i = 0; cardDeck[ i ].cardClassName; ++i ) {
		char	*fieldData = alloca( 13 + strlen( cardDeck[ i ].cardClassName ) + 8 );
		
		sprintf( fieldData, "(0) of (%d), %s", cardDeck[ i ].numCards, cardDeck[ i ].cardClassName );
		[[ cardsPlayedMatrix findCellWithTag:cardDeck[ i ].cardType ] setStringValue:fieldData ];
	}
	
    return self;
}


- ( int )numPlayed:( int )aCardTag
{

	TextFieldCell	*tCell = [ cardsPlayedMatrix findCellWithTag:aCardTag ];
	int				numPlayed;
	
	
	assert( tCell );
	sscanf([ tCell stringValue ], "(%d)", &numPlayed );

	return numPlayed;
}


- ( int )numInDeck:( int )aCardTag
{

	int		i;
	
	
	for( i = 0; cardDeck[ i ].cardClassName; ++i )
		if( aCardTag == cardDeck[ i ].cardType )
			return cardDeck[ i ].numCards;

	assert( 0 /* unable to find card in deck */ );

}


- ( BOOL )allPlayed:( int )aCardTag
{


	return [ self numPlayed:aCardTag ] == [ self numInDeck:aCardTag ];
}



- ( BOOL )allSafetiesPlayed
{

	
	return [ self numSafetiesPlayed ] == NUMBER_OF_SAFETY_CARDS_IN_DECK ;
}


- ( int )numSafetiesPlayed
{

	int		cardTypes[] = {	C_RIGHT_OF_WAY_SAFETY, C_PUNCTURE_PROOF_SAFETY, 
							C_DRIVING_ACE_SAFETY, C_EXTRA_TANK_SAFETY },
			i, cnt;
			
	
	for( i = 0, cnt = 0; i < ( sizeof( cardTypes ) / sizeof( int )); ++i )
		if([ self allPlayed:cardTypes[ i ]])
			++cnt;
	
	return cnt;
}


@end
