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

int tempSaw[81];
int nowSaw;
int indexColor;
int colorBoardSaw[81];

void moveSaw(char from, char to)
{
	int i;
	if(from == '0' && to == 'l')
	{
		for(i=0; i<144; i++)
		{
			leftSaw[i] = 0;
		}
	}
	else if(from == '0' && to == 'r')
	{
		for(i=0; i<144; i++)
		{
			rightSaw[i] = 0;
		}
	}
	else if(from == 'l' && to == 'r')
	{
		for(i=0; i<144; i++)
		{
			rightSaw[i] = leftSaw[i];
		}
	}
	else if(from == 'r' && to == 'l')
	{
		for(i=0; i<144; i++)
		{
			leftSaw[i] = rightSaw[i];
		}
	}
}

void moveColorSaw(char from, char to)
{
	int i, j;
	if(from == 'l' && to == 'r')
	{
		for(i=0; i<81; i++)
		{
			for(j=0; j<4; j++)
			{
				colorSawRight[i][j] = colorSaw[i][j];
			}
		}
	}
	if(from == 'r' && to == 'l')
	{
		for(i=0; i<81; i++)
		{
			for(j=0; j<4; j++)
			{
				colorSaw[i][j] = colorSawRight[i][j];
			}
		}
	}
}

void initialJigsaw()
{
	if(gameKind == 0 || gameKind == 1 || gameKind == 2 || gameKind == 5)
	{
		setInitSaw();
	}
	getColorSaw();
	movePare('0', 'l'); // 0 to leftPare
	moveKilpare('0', 'l'); // 0 to killerPare
	moveSum('0', 'l'); // 0 to leftSum
	moveIdx('0', 'l'); // 0 to leftIdx
	moveHead('0', 'l'); // 0 to headIdx
	moveUniq('0', 'l'); // 0 to uniqIdx
	moveExtra('0', 'l'); // 0 to leftExtra
	editKind = 2;
	gameKind = 4;
}

void getIndexColor()
{
	int count = 0;	
	// 根据headSaw取得colorSaw
	if(selectSaw == nowSaw)
	{
		indexColor &= 0x2FF; // 第8位置0
		indexColor |= 0x200; // 第9位置1
		return;
	}
	for(int i=0; i<81; i++)
	{
		if(headSaw[i] == nowSaw)
		{
			count++;
			if(count == 1)
			{
				indexColor &= 0x1FF; // 第9位置0
				indexColor |= 0x100; // 第8位置1
			}
			else
			{
				indexColor = (indexColor&0xFF)+1;
				break;
			}
		}
	}
}

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

void getHeadSaw()
{
	int i;
	// get headSaw based on leftSaw
	for(i=0; i<81; i++)
	{
		tempSaw[i] = leftSaw[i];
		headSaw[i] = 0;
	}
	for(i=0; i<81; i++)
	{
		if(tempSaw[i] != 0)
		{
			nowSaw = tempSaw[i];
			headSaw[i] = nowSaw;
			clearNabour(i);
		}
	}
}

void getColorBoard()
{
	int i, j;
	int temp;
	indexColor = 0;
	for(i=0; i<81; i++)
	{
		colorBoardSaw[i] = MaxColorCircle;
	}
	// 根据返回的颜色索引取得colorBoardSaw[81]
	for(i=0; i<81; i++)
	{
		if(headSaw[i] != 0)
		{
			temp = headSaw[i];
			nowSaw = temp;
			getIndexColor();
			for(j=0; j<81; j++)
			{
				if(leftSaw[j] == temp)
				{
					if((indexColor&0x200) == 0x200)
					{
						colorBoardSaw[j] = 9;
					}
					else if((indexColor&0x100) == 0x100)
					{
						colorBoardSaw[j] = 0;
					}
					else
					{
						colorBoardSaw[j] = indexColor;
					}
				}
			}
			for(j=0; j<81; j++)
			{
				if(headSaw[j] == temp)
				{
					headSaw[j] = 0;
				}
			}
		}
	}
}

void getColorSaw()
{
	int i;
	getHeadSaw(); // 取得headSaw[81]
	maxSaw = 0;
	for(i=0; i<81; i++)
	{
		if(headSaw[i] > maxSaw)
		{
			maxSaw = headSaw[i];
		}
	}
	getColorBoard(); // 取得colorBoardSaw[81], 同时把headSaw[81]清0, 供下次使用
	for(i=0; i<81; i++)
	{
		colorSaw[i][0] = colorSaw[i][1] = colorSaw[i][2] = colorSaw[i][3] = MaxColorCircle;
	}
	// 根据colorBoardSaw[81]取得colorSaw[81][4]
	for(i=0; i<81; i++)
	{
		if(i < 9 || leftSaw[i-9] != leftSaw[i])
		{
			colorSaw[i][0] = colorBoardSaw[i];
		}
		if(mod9[i] > 7 || leftSaw[i+1] != leftSaw[i])
		{
			colorSaw[i][1] = colorBoardSaw[i];
		}
		if(i > 71 || leftSaw[i+9] != leftSaw[i])
		{
			colorSaw[i][2] = colorBoardSaw[i];
		}
		if(mod9[i] < 1 || leftSaw[i-1] != leftSaw[i])
		{
			colorSaw[i][3] = colorBoardSaw[i];
		}
	}
}

////////////////////////以下与鼠标操作有关////////////////////////
/*****************************************************
 * 按住shift点选已经选中的格点时，取消此格点
 * 点选没有saw的格点时，选中此格点(第一次选和非第一次选)
 * 点选saw的格点时，选中所有saw的格点
 * 窍门：跟着别人跑
 *****************************************************/
void updateSawShift(int grid)
{
	int i;
	int nowSaw;
	int temp;
	// 已知leftSaw, editingSaw
	if(editingSaw[grid])
	{
		editingSaw[grid] = false;
		temp = leftSaw[grid];
		leftSaw[grid] = 0;
	}
	else if(leftSaw[grid] == 0)
	{
		if(selectSaw == 0)
		{
			selectSaw = ++maxSaw;
			editingSaw[grid] = true;
			leftSaw[grid] = selectSaw;
		}
		else
		{
			editingSaw[grid] = true;
			leftSaw[grid] = selectSaw;
		}
		temp = selectSaw;
	}
	else // leftSaw[grid] != 0
	{
		if(selectSaw == 0)
		{
			selectSaw = leftSaw[grid];
			temp = leftSaw[grid];
			nowSaw = selectSaw;
		}
		else
		{
			nowSaw = selectSaw;
			temp = selectSaw;
			selectSaw = leftSaw[grid];
		}
		for(i=0; i<81; i++)
		{
			if(leftSaw[i] == selectSaw)
			{
				editingSaw[i] = true;
			}
			else if(leftSaw[i] == nowSaw) // 相等的时候下面无意义
			{
				leftSaw[i] = selectSaw;
			}
		}
	}
	for(i=0; i<81; i++)
	{
		if(leftSaw[i] == temp)
		{
			break;
		}
	}
	if(i == 81)
	{
		cutEraseSaw(temp);
	}
}

/*****************************************************
 * 按住ctrl点选已经选中的格点时，取消此格点
 * 点选没有saw的格点时，选中此格点(第一次选和非第一次选)
 * 点选saw的格点时，选中saw的格点
 * 窍门：跟着自己跑
 *****************************************************/
void updateSawCtrl(int grid)
{
	int i, temp;
	// 已知leftSaw, editingSaw
	if(editingSaw[grid])
	{
		editingSaw[grid] = false;
		temp = leftSaw[grid];
		leftSaw[grid] = 0;
	}
	else
	{
		if(selectSaw == 0)
		{
			selectSaw = ++maxSaw;
		}
		editingSaw[grid] = true;
		if(leftSaw[grid] == 0)
		{
			temp = selectSaw;
		}
		else
		{
			temp = leftSaw[grid];
		}
		leftSaw[grid] = selectSaw;
	}
	for(i=0; i<81; i++)
	{
		if(leftSaw[i] == temp)
		{
			break;
		}
	}
	if(i == 81)
	{
		cutEraseSaw(temp);
	}
}

void cutEraseSaw(int eraseNo)
{
	int i;
	for(i=0; i<81; i++)
	{
		if(leftSaw[i] > eraseNo)
		{
			leftSaw[i]--;
		}
	}
	if(selectSaw > eraseNo)
	{
		selectSaw--;
	}
	else if(selectSaw == eraseNo)
	{
		selectSaw = 0;
	}
}

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

void flushJigsawLeft()
{
	terminateSawEdit();
	getColorSaw();
}

void setInitSaw()
{
	int i;
	for(i=0; i<81; i++)
	{
		leftSaw[i] = squareNoTbl[i]+1;
	}
}
