/*

File BackjumpSquare.m

These are the squares used for backjumping search.  When a letter has no remaining possibilities, the words that intersect at that letter are added to the current nogood.  The current nogood is cleared whenever a letter is successfully filled.

*/

#import <appkit/appkit.h>

#import "Backjump.h"
#import "Puzzle.h"


/* 行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行  */


#define position(i)		((wordPosition *) [words  elementAt: i])


/* 行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行  */


@implementation BackjumpSquare


- startOver
{
	id		state;
	
	if ([state = [puzzle  getState]  inNogood]) [state  isok];
	return self;
}


- erase
{
	[self  chooseLetter];
	[[[self  clear]  setLetter: WILDCARD]  show];
	
	return self;
}


- clear
{
	id		state;
	int		i;
	
	if (!injump)
	{
		state = [puzzle  getState];
		i = [words  count];
		while (i--) [state  mustChange: position(i)->word];
	}
	
	return [super  clear];
}


- setLetter: (char) theLetter
{
	[super  setLetter: theLetter];
	if (letter != WILDCARD) [[puzzle  getState]  isok];
			
	return self;
}


- (char) chooseLetter
{
	char	best;
	int		i;
	
	if (![[puzzle  getState]  inNogood]) injump = NO;
	else
	{
		injump = YES;
		i = [words  count];
		while (i--) injump &= ![position(i)->word  mustChange];
	}
		
	if (!injump) best = [super  chooseLetter];
	else best = WILDCARD;
	
	if (injump)
			[self  setStatus: JUMPED];		else
			[self  clearStatus: JUMPED];
	
	return best;
}


@end