// indent:4  tabsize:4  font:fixed-width
//
// The Cribbage Solitaire module.
//
// Copyright 1994 by David Fedchenko.  All rights reserved.
//

#import "CribLayoutDelegate.h"
#import "CribDrawDelegate.h"
#import "../Solitaire/Solitaire.h"
#import "Cribbage.h"

@implementation CribLayoutDelegate

//--------------------------------------------------------------------------
//	- setStrict:
//
//	returns: (id) self
//
//	Sets the flag which limits card placement.
//--------------------------------------------------------------------------
- setStrict:(BOOL)flag
	{
	fStrict = flag;
	return self;
	}

//--------------------------------------------------------------------------
//	- setDrawPileView:
//
//	returns: (id) self
//
//	Sets a pointer to the Draw pile.
//--------------------------------------------------------------------------
- setDrawPileView:idView
	{
	idDrawPileView = idView;
	return self;
	}

//--------------------------------------------------------------------------
//	- clickedCard:in:
//
//	returns: (id) self
//
//	Takes care of the speed mode of play.  This allows the empty positions
//	to be clicked and have the card jump into them.
//--------------------------------------------------------------------------
- clickedCard:aCard in:aCardPileView
	{
	id	idCard;
	
	if (aCard || ![self checkValidity:aCardPileView])
		{
		return self;
		}
	
	idCard = [[idDrawPileView cardPile] cardAt:CS_TOP];
	[[aCardPileView cardPile] addCard:idCard];
	[[idDrawPileView cardPile] removeCard:idCard];
	[[[idDrawPileView cardPile] cardAt:CS_TOP] flip];
	
	[[SolGameController() gameWindow] display];
	
    [SolGameController() checkForWin];
	
	return self;
	}

//--------------------------------------------------------------------------
//	- acceptPile:in:
//
//	returns: (id) self
//
//	Does nothing, but makes sure the tableau pile gets the card.
//--------------------------------------------------------------------------
- acceptPile:aCardPile in:aCardPileView
	{
	return self;
	}

//--------------------------------------------------------------------------
//	- canAcceptPile:from:in:
//
//	returns: (BOOL) flag
//
//	Checks, depending on the strict mode flag, whether a card can legally
//	be placed in the given pile.
//--------------------------------------------------------------------------
-(BOOL) canAcceptPile:aCardPile from:sender in:aCardPileView
	{
	if ([[aCardPileView cardPile] cardCount])
		{
		return NO;
		}
	
	return [self checkValidity:aCardPileView];
	}

//--------------------------------------------------------------------------
//	- checkValidity:
//
//	returns: (BOOL) flag
//
//	When in strict mode, checks to make sure that the pile being queried has
//	at least one neighbor.
//--------------------------------------------------------------------------
-(BOOL) checkValidity:aCardPileView
	{
	id	idGrid[16];
	int	i;
	int	j;
	int	x;
	int	y;
	int	x1;
	int	y1;
	int	c;
	int	which = -1;
	
	if (!fStrict)
		{
		return YES;
		}
	
	[SolGameController() loadCardArray:idGrid];
	
	c = 0;
	for (i = 0; i < 16; i++)
		{
		c += [[idGrid[i] cardPile] cardCount];
		if (idGrid[i] == aCardPileView)
			{
			which = i;
			}
		}
	
	if (c && (which != -1))
		{
		c = 0;
		y = which % 4;
		x = which / 4;
		
		for (i = -1; i <= 1; i++)
			{
			x1 = x + i;
			for (j = -1; j <= 1; j++)
				{
				y1 = y + j;
				if (x1 > -1 && x1 < 4 && y1 > -1 && y1 < 4)
					{
					c += [[idGrid[x1 * 4 + y1] cardPile] cardCount];
					}
				}
			}
		
		if (!c)
			{
			return NO;
			}
		}
	
	return YES;
	}

@end
