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

static int countReserve = 0;
void addAllPare()
{
	int i, j;
	int random;
	// 产生所有的不等号
	for(i=0; i<9; i++)
	{
		for(j=0; j<8; j++)
		{
			pare[(i<<3)+j] = board[mult9[i]+j]>board[mult9[i]+j+1]?1:2;
			pare[72+mult9[j]+i] = board[mult9[j]+i]>board[mult9[j]+i+9]?1:2;
		}
	}
	// 随机去掉(81-指定保留数)个数
	countQuestion = 81;
	while(countQuestion > countReserve)
	{
		random = rand() % 81;
		while(board[random] == 0)
		{
			random = (random+1) % 81;
		}
		board[random] = 0;
		countQuestion--;
	}
}

void getQuestionPare()
{
	int i;
	int randStack[144];
	int pRandStack = 0;
	int indexDrop[144];
	int pIndexDrop = 0;
	int point;
	int oldPare;
	for(i=0; i<144; i++)
	{
		if(pare[i] != 0)
		{
			randStack[pRandStack++] = i;
		}
	}
	while(pRandStack != 0)
	{
		point = rand() % pRandStack;
		indexDrop[pIndexDrop++] = randStack[point];
		randStack[point] = randStack[--pRandStack];
	}
	// now we get indexDrop[pIndexDrop] and pIndexDrop
	for(i=0; i<pIndexDrop; i++)
	{
		moveBoard('t', 'b');
		oldPare = pare[indexDrop[i]]; // 备份
		pare[indexDrop[i]] = 0;
		getAnswerPare();
		if(flagAnswer != 1)
		{
			pare[indexDrop[i]] = oldPare; // 还原
		}
	}
}

void getQuestionPare2()
{
	int i;
	int randStack[81];
	int pRandStack = 0;
	int indexDrop[81];
	int pIndexDrop = 0;
	int point;
	for(i=0; i<81; i++)
	{
		if(board[i] != 0)
		{
			randStack[pRandStack++] = i;
		}
	}
	while(pRandStack != 0)
	{
		point = rand() % pRandStack;
		indexDrop[pIndexDrop++] = randStack[point];
		randStack[point] = randStack[--pRandStack];
	}
	// now we get indexDrop[pIndexDrop] and pIndexDrop
	getCountQuestion();
	for(i=0; i<pIndexDrop && countQuestion>countReserve; i++)
	{
		moveBoard('b', 't');
		board[indexDrop[i]] = 0;
		getAnswerPare();
		if(flagAnswer == 1)
		{
			tempBoard[indexDrop[i]] = 0;
			countQuestion--;
		}
		moveBoard('t', 'b');
	}
}

void handleQuestionPare()
{
	int oldFlagAnswer;
	moveBoard('l', 'b');
	movePare('l', 'p');
	rightStatus = 3;
	getAnswerPare(); // 只为取得flagAnswer
	oldFlagAnswer = flagAnswer;
	if(flagAnswer == 0 || flagAnswer == 2)
	{
		while(flagAnswer != 1)
		{
			mixupNumbers(); // 得到整个的盘面
			addAllPare(); // 添加所有的><，去掉多余的数
			moveBoard('b', 't');
			getAnswerPare();
		}
		moveBoard('t', 'b');
		underQuestion = true;
		getQuestionPare();

		moveBoard('t', 'r');
		movePare('p', 'r');
		flagAnswer = oldFlagAnswer;
		underQuestion = false;
		underThinking = false;
	}
	else
	{
		moveBoard('l', 'b');
		// 注：answerBoard在这下面的循环里起到中间盘的作用
		underQuestion = true;
		getQuestionPare2();
		getQuestionPare();
		moveBoard('t', 'r');
		movePare('p', 'r');
		flagAnswer = oldFlagAnswer;
		underQuestion = false;
		underThinking = false;
	}
	if(firstIntoSodoku) // 只在第一次变成数比状态时才清右盘
	{
		firstIntoSodoku = false;
		moveSum('0', 'r');
		moveIdx('0', 'r');
		moveUniq('0', 'r');
		moveKilpare('0', 'r');
		moveSaw('0', 'r');
		moveColorSaw('l', 'r');
		moveExtra('0', 'r');
		gameKindRight = 2;
		pRightCircle = 0;
	}
}

void handleQuestionPares()
{
	flagAnswer = 2;
	while(flagAnswer != 1)
	{
		mixupNumbers(); // 得到整个的盘面
		addAllPare(); // 添加所有的><，去掉多余的数
		moveBoard('b', 't');
		getAnswerPare();
	}
	moveBoard('t', 'b');
	underQuestion = true;
	getQuestionPare();
	moveBoard('t', 'a');
	movePare('p', 'a');
	underQuestion = false;
}
