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

void mixupNumbers1()
{
	int i;
	int index;
	int randStack[9];
	int pRandStack;
	// the first row
	for(i=0; i<9; i++)
	{
		randStack[i] = i+1;
	}
	pRandStack = 9;
	for(i=0; i<81; i++)
	{
		board[i] = 0;
	}
	while(pRandStack != 0)
	{
		index = rand() % pRandStack;
		board[9-pRandStack] = randStack[index];
		randStack[index] = randStack[--pRandStack];
	}
	// the first column
	board[9] = board[rand() % 6 + 3];
	for(i=0; i<8; i++)
	{
		randStack[i] = board[i+1];
	}
	for(i=2; i<8; i++)
	{
		if(randStack[i] == board[9])
		{
			randStack[i] = randStack[7];
			break;
		}
	}
	randStack[rand() % 5 + 2] = randStack[6];
	pRandStack = 6;
	while(pRandStack != 0)
	{
		index = rand() % pRandStack;
		board[mult9[pRandStack+2]] = randStack[index];
		randStack[index] = randStack[--pRandStack];
	}
}

void mixupNumbers2()
{
	int i;
	int index;
	int randPoint;
	int swapNumber1, swapNumber2;
	int numberStack[9];
	int randStack[9];
	int pRandStack;
	// 纯随机化
	while(true)
	{
		// 取得randStack
		for(i=0; i<9; i++)
		{
			numberStack[i] = i+1;
		}
		pRandStack = 9;
		while(pRandStack != 0)
		{
			index = rand() % pRandStack;
			randStack[9-pRandStack] = numberStack[index];
			numberStack[index] = numberStack[--pRandStack];
		}
		// 取得randPoint
		do
		{
			randPoint = rand() % 81;
		} while(board[randPoint] != 0);
		moveBoard('b', 'k');
next_number:
		board[randPoint] = randStack[pRandStack++];
		getAnswer();
		if(flagAnswer == 0)
		{
			moveBoard('k', 'b');
			goto next_number;
		}
		else if(flagAnswer == 1)
		{
			break;
		}
		else if(flagAnswer == 2)
		{
			moveBoard('k', 'b');
			board[randPoint] = randStack[pRandStack-1];
		}
	}
	for(i=0; i<20; i++)
	{
		swapNumber1 = rand() % 9 + 1;
		swapNumber2 = rand() % 9 + 1;
		for(int j=0; j<81; j++)
		{
			if(answerBoard[j] == swapNumber1)
			{
				answerBoard[j] = swapNumber2;
			}
			else if(answerBoard[j] == swapNumber2)
			{
				answerBoard[j] = swapNumber1;
			}
		}
	}
	moveBoard('a', 'b');
}

void mixupNumbers()
{
	mixupNumbers1();
	mixupNumbers2();
}

void getQuestion()
{
	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
	for(i=0; i<pIndexDrop; i++)
	{
		moveBoard('b', 'k');
		board[indexDrop[i]] = 0;
		getAnswer();
		if(flagAnswer == 1)
		{
			bakBoard[indexDrop[i]] = 0;
		}
		moveBoard('k', 'b');
	}
}

void getCountQuestion()
{
	countQuestion = 0;
	for(int i=0; i<81; i++)
	{
		if(board[i] != 0)
		{
			countQuestion++;
		}
	}
}

void handleQuestion()
{
	int oldFlagAnswer;
	rightStatus = 3;
	moveBoard('l', 'b');
	getAnswer(); // 只为取得flagAnswer
	oldFlagAnswer = flagAnswer;
	if(flagAnswer == 0 || flagAnswer == 2)
	{
		mixupNumbers();
	}
	else
	{
		moveBoard('l', 'b');
	}
	moveBoard('b', 'a');
	// 注：answerBoard在这下面的循环里起到中间盘的作用
	underQuestion = true;
	do
	{
		moveBoard('a', 'b');
		getQuestion();
		getCountQuestion();
	} while(countQuestion > 23);

	moveBoard('b', 'r');
	flagAnswer = oldFlagAnswer;
	underQuestion = false;
	underThinking = false;
	if(firstIntoSodoku) // 只在第一次变成数独状态时才清右盘
	{
		firstIntoSodoku = false;
		movePare('0', 'r'); // 0 to rightPare
		moveSum('0', 'r'); // 0 to rightSum
		moveIdx('0', 'r'); // 0 to rightIdx
		moveUniq('0', 'r'); // 0 to rightUniq
		moveKilpare('0', 'r');
		moveSaw('0', 'r');
		moveColorSaw('l', 'r');
		moveExtra('0', 'r');
		gameKindRight = 0;
		pRightCircle = 0;
	}
}

void handleQuestions()
{
	mixupNumbers();
	moveBoard('b', 'a');
	// 注：answerBoard在这下面的循环里起到中间盘的作用
	underQuestion = true;
	do
	{
		moveBoard('a', 'b');
		getQuestion();
		getCountQuestion();
	} while(countQuestion > 23);
	underQuestion = false;
	moveBoard('b', 'a');
}
