/***
*
* @brief 随机产生一个数独
*
* @param int uLevel 产生的数独的难度
*
* @return true
*
*/
T_BOOL VSHUDUGenerateShudu(T_U32 uLevel)
{
T_U32 i;
T_U32 j;
T_U32 uCount = 0;
T_U32 uFirstNum;
g_pSDParam->count = 0;
//用户填入的数组为0
//初始化原来数组
for(i = 0; i < T_VSD_LEN; i ++)
{
for(j = 0; j < T_VSD_LEN; j ++)
{
g_Array[i][j] = 0;
g_UserArray[i][j] = 0;
}
}
AkBmpDrawPartFromString(LCD_0, rtDialogue.left, rtDialogue.top, &rtDialogue, g_pSDParam->pBackgroud, /
AK_NULL, AK_FALSE);
Fwl_InvalidateRect(LCD_0, rtDialogue.left, rtDialogue.top, rtDialogue.width, rtDialogue.height);
//随机产生一个数字,并赋值给数组的第一个元素
uFirstNum = Fwl_GetRand(T_VSD_LEN) + 1;
g_Array[0][0] = uFirstNum;
if(VSHUDUJudge(0, 1))
{
//记录初始数独
for(i = 0; i < T_VSD_LEN; i ++)
{
for(j = 0; j < T_VSD_LEN; j ++)
{
g_IsOriArray[i][j] = g_Array[i][j];
}
}
VSHUDURandomEmpty(uLevel);
//记录生成数独之后,挖空的数据
for(i = 0; i < T_VSD_LEN; i ++)
{
for(j = 0; j < T_VSD_LEN; j ++)
{
if(!g_Array[i][j])
{
g_EmptyArray[i][j] = 0;
g_pSDParam->count ++;//记录空格的数量
AK_DEBUG_OUTPUT("The g_pSDParam->count is================== %d ", g_pSDParam->count);
}
else
{
g_EmptyArray[i][j] = g_Array[i][j];
}
}
}
uCountEmptyNum = g_pSDParam->count;
AkBmpDrawPartFromString(LCD_0, rtPicBkFresh.left, rtPicBkFresh.top, &rtPicBkFresh, g_pSDParam->pBackgroud, AK_NULL, AK_FALSE);
//记录上一次单击圆环上的数字置0
g_pSDParam->rtLastPoint.left = 0;
g_pSDParam->rtLastPoint.top = 0;
g_pSDParam->rtLastPoint.width = 0;
g_pSDParam->rtLastPoint.height = 0;
//对数组内的数字进行画图,画到数独里面
for(i = 0; i < T_VSD_LEN; i ++)
{
for(j = 0; j < T_VSD_LEN; j ++)
{
if(!g_EmptyArray[i][j])
{
VSHUDUDISPNUM(i, j, 0);
}
else
{
VSHUDUDISPNUM(i, j, g_EmptyArray[i][j]);
//VSHUDUDISPNUM(i, j, g_Array[i][j]);
}
}
}
cirque_is_playing = AK_FALSE;
Fwl_AudioStop();
if(ERROR_TIMER != shudu_timer)
{
Fwl_StopTimer(shudu_timer);
shudu_timer = ERROR_TIMER;
}
shudu_timer = Fwl_SetTimerMilliSecond(T_VSD_MINITIME, AK_TRUE);
play_spend_time = 0;
Fwl_InvalidateRect( LCD_0, rtPicBkFresh.left, rtPicBkFresh.top, rtPicBkFresh.width, rtPicBkFresh.height);
return AK_TRUE;
}
return AK_FALSE;
}
/***
*
* @brief 判断能否继续添加数以完成数独
*
* @param int nRow 要添加下一个数的行
* @param int nCol 要添加下一个数的列
*
* @return bool 如果在当行位置无法放置1-LEN之间的数,返回false;否则返回true;
* nRandomNum[i] = rand() % T_VSD_LEN + 1; //产生1-9,随机地放在数组里
*
* if (nRandomNum[j] == nRandomNum[i]) //判断第i个数与前面j个数有无相同的随机数。
* //若有,则i-1,即返回前一个数,跳出,进入下次循环。
*/
T_BOOL VSHUDUJudge(T_U32 uRow, T_U32 uCol)
{
T_U32 i;
T_U32 j;
T_U32 g_TempArray[T_VSD_LEN] = {0};
if(T_VSD_LEN == uRow)
{
return AK_TRUE;
}
if((uRow > T_VSD_LEN) || (uCol > T_VSD_LEN))
{
return AK_FALSE;
}
//检查每一行是否有重复数字
for(i = 0; i < T_VSD_LEN; i ++)
{
g_TempArray[i] = Fwl_GetRand(T_VSD_LEN) + 1;
for(j = 0; j < i; j ++)
{
if(g_TempArray[j] == g_TempArray[i])
{
i --;
break;
}
}
}
//delete[] g_TempArray;
for(i = 0; i < T_VSD_LEN; i ++)
{
g_Array[uRow][uCol] = g_TempArray[i];
if(VSHUDUXJudge(uRow, uCol) && VSHUDUYJudge(uRow, uCol) && VSHUDURectJudge(uRow, uCol))
{
if((T_VSD_LEN - 1) == uCol && VSHUDUJudge(uRow + 1, 0))
{
return AK_TRUE;
}
else if(VSHUDUJudge(uRow, uCol + 1))
{
return AK_TRUE;
}
}
g_Array[uRow][uCol] = T_VSD_EMPTY;
}
return AK_FALSE;
}
/***
*
* @brief 判断同一行是否有冲突
*
* @param int g_pSDParam->uRow 要判断的行
* @param int g_pSDParam->uCol 要判断的列
*
* @return bool 没有冲突返回true,否则返回false;
*
*/
T_BOOL VSHUDUXJudge(T_U32 uRow, T_U32 uCol)
{
T_U32 i;
for(i = 0; i < T_VSD_LEN; i ++)
{
if(g_Array[uRow][uCol] == g_Array[uRow][i] && (i != uCol))
{
return AK_FALSE;
}
}
return AK_TRUE;
}
/***
*
* @brief 判断同一列是否有冲突
*
* @param int g_pSDParam->uRow 要判断的行
* @param int g_pSDParam->uCol 要判断的列
*
* @return bool 没有冲突返回true,否则返回false;
*
*/
T_BOOL VSHUDUYJudge(T_U32 uRow, T_U32 uCol)
{
T_U32 j;
for(j = 0; j < T_VSD_LEN; j ++)
{
if(g_Array[uRow][uCol] == g_Array[j][uCol] && j != uRow)
{
return AK_FALSE;
}
}
return AK_TRUE;
}
/***
*
* @brief 判断小的九宫格里是否有冲突
*
* @param int g_pSDParam->uRow = {0, 1, 2, 3, 4, 5, 6, 7, 8} 要判断的行
* @param int g_pSDParam->uCol = {0, 1, 2, 3, 4, 5, 6, 7, 8} 要判断的列
*
* @return bool 如果在当前位置的数在九宫格内有冲突,返回false;否则返回true;
*
*/
T_BOOL VSHUDURectJudge(T_U32 uRow, T_U32 uCol)
{
T_U32 i;
T_U32 j;
for(i = uRow / T_VSD_RECTANGLE * T_VSD_RECTANGLE; i < uRow / T_VSD_RECTANGLE * T_VSD_RECTANGLE + T_VSD_RECTANGLE; i ++)
{
for(j = uCol / T_VSD_RECTANGLE * T_VSD_RECTANGLE; j < uCol / T_VSD_RECTANGLE * T_VSD_RECTANGLE + T_VSD_RECTANGLE; j ++)
{
if(g_Array[uRow][uCol] == g_Array[i][j] && !(i == uRow && j == uCol))
{
return AK_FALSE;
}
}
}
return AK_TRUE;
}