/******************************************************************
 *						Author:	Zhou Yundong
 *						Time: 20090219 20:49
 ******************************************************************/
#include "Stdafx.h"
#include "Think.h"

int tempExtra[81];
int nowExtra;
static int indexColor;
int colorBoardExtra[81];

void moveExtra(char from, char to)
{
	int i;
	if(from == '0' && to == 'l')
	{
		for(i=0; i<81; i++)
		{
			leftExtra[i] = 0;
		}
	}
	else if(from == '0' && to == 'r')
	{
		for(i=0; i<81; i++)
		{
			rightExtra[i] = 0;
		}
	}
	else if(from == 'l' && to == 'r')
	{
		for(i=0; i<81; i++)
		{
			rightExtra[i] = leftExtra[i];
		}
	}
	else if(from == 'r' && to == 'l')
	{
		for(i=0; i<81; i++)
		{
			leftExtra[i] = rightExtra[i];
		}
	}
}

void initialSoextra()
{
	moveSum('0', 'l'); // 0 to leftSum
	moveIdx('0', 'l'); // 0 to leftIdx
	moveHead('0', 'l'); // 0 to headIdx
	moveUniq('0', 'l'); // 0 to uniqIdx
	moveSaw('0', 'l'); // 0 to leftSaw
	movePare('0', 'l'); // 0 to leftPare
	moveKilpare('0', 'l'); // 0 to leftKilpare
	editKind = 3;
	gameKind = 5;
	firstIntoSodoku = true;
}

static void clearNabour(int grid)
{
	tempExtra[grid] = 0;
	if(grid >= 9 && tempExtra[grid-9] == nowExtra)
	{
		clearNabour(grid-9);
	}
	if(mod9[grid] <= 7 && tempExtra[grid+1] == nowExtra)
	{
		clearNabour(grid+1);
	}
	if(grid <= 71 && tempExtra[grid+9] == nowExtra)
	{
		clearNabour(grid+9);
	}
	if(mod9[grid] >= 1 && tempExtra[grid-1] == nowExtra)
	{
		clearNabour(grid-1);
	}
}

void getHeadExtra()
{
	int i;
	// get headExtra based on leftExtra
	for(i=0; i<81; i++)
	{
		tempExtra[i] = leftExtra[i];
		headExtra[i] = 0;
	}
	for(i=0; i<81; i++)
	{
		if(tempExtra[i] != 0)
		{
			nowExtra = tempExtra[i];
			headExtra[i] = nowExtra;
			clearNabour(i);
		}
	}
}

////////////////////////以下与鼠标操作有关////////////////////////
void cutEraseExtra(int eraseNo)
{
	int i;
	for(i=0; i<81; i++)
	{
		if(leftExtra[i] > eraseNo)
		{
			leftExtra[i]--;
		}
	}
	if(selectExtra > eraseNo)
	{
		selectExtra--;
	}
	else if(selectExtra == eraseNo)
	{
		selectExtra = 0;
	}
}

void extraLittleCheck(int temp)
{
	int i;
	for(i=0; i<81; i++)
	{
		if(leftExtra[i] == temp)
		{
			break;
		}
	}
	if(i == 81)
	{
		cutEraseExtra(temp);
	}
	maxExtra = 0;
	for(i=0; i<81; i++)
	{
		if(leftExtra[i] > maxExtra)
		{
			maxExtra = leftExtra[i];
		}
	}
}

/*****************************************************
 * 按住shift点选已经选中的格点时，取消此格点
 * 点选没有extra的格点时，选中此格点(第一次选和非第一次选)
 * 点选extra的格点时，选中所有extra的格点
 * 窍门：跟着别人跑
 *****************************************************/
void updateExtraShift(int grid)
{
	int i;
	int nowExtra;
	int temp;
	// 已知leftExtra, editingExtra
	if(editingExtra[grid])
	{
		editingExtra[grid] = false;
		temp = leftExtra[grid];
		leftExtra[grid] = 0;
	}
	else if(leftExtra[grid] == 0)
	{
		if(selectExtra == 0)
		{
			selectExtra = ++maxExtra;
			editingExtra[grid] = true;
			leftExtra[grid] = selectExtra;
		}
		else
		{
			editingExtra[grid] = true;
			leftExtra[grid] = selectExtra;
		}
		temp = selectExtra;
	}
	else // leftExtra[grid] != 0
	{
		if(selectExtra == 0)
		{
			selectExtra = leftExtra[grid];
			temp = leftExtra[grid];
			nowExtra = selectExtra;
		}
		else
		{
			nowExtra = selectExtra;
			temp = selectExtra;
			selectExtra = leftExtra[grid];
		}
		for(i=0; i<81; i++)
		{
			if(leftExtra[i] == selectExtra)
			{
				editingExtra[i] = true;
			}
			else if(leftExtra[i] == nowExtra) // 相等的时候下面无意义
			{
				leftExtra[i] = selectExtra;
			}
		}
	}
	extraLittleCheck(temp);
}

/*****************************************************
 * 按住ctrl点选已经选中的格点时，取消此格点
 * 点选没有extra的格点时，选中此格点(第一次选和非第一次选)
 * 点选extra的格点时，选中extra的格点
 * 窍门：跟着自己跑
 *****************************************************/
void updateExtraCtrl(int grid)
{
	int temp;
	// 已知leftExtra, editingExtra
	if(editingExtra[grid])
	{
		editingExtra[grid] = false;
		temp = leftExtra[grid];
		leftExtra[grid] = 0;
	}
	else
	{
		if(selectExtra == 0)
		{
			selectExtra = ++maxExtra;
		}
		editingExtra[grid] = true;
		if(leftExtra[grid] == 0)
		{
			temp = selectExtra;
		}
		else
		{
			temp = leftExtra[grid];
		}
		leftExtra[grid] = selectExtra;
	}
	extraLittleCheck(temp);
}

/*****************************************************
 * 处于编辑数超状态时，按下回车或者鼠标左键点一下等
 *****************************************************/
void terminateExtraEdit()
{
	int i;
	selectExtra = 0;
	for(i=0; i<81; i++)
	{
		editingExtra[i] = false;
	}
	underExtraEditing = false;
}
