display.cpp
来自「一个在VISUAL C++下编程的soduku系统 里面有界面 有出盘 也比较简」· C++ 代码 · 共 1,214 行 · 第 1/3 页
CPP
1,214 行
unsigned elim=0, possbl, bm, elim_rc;
for (row=0; row<9; row+=3){
for (col=0; col<9; col+=3){
for (bm=0x1; bm<=0x100; bm<<=1){
possbl = 0;
for (j=row; j<row+3; j++){
for (k=col; k<col+3; k++){
if (all[j][k] & bm){
last_j[possbl] = j;
last_k[possbl] = k;
cell[possbl] = all[j][k];
if (!QuickSolve)
RB[possbl] = RedBits[j][k];
possbl++;
if (possbl >= 3)
break;
}
}
if (possbl >= 3)
break;
}
if ( (possbl > 1) && (possbl < 3) ){
for (i=1; i<possbl; i++)
if ( last_j[i] != last_j[0] )
break;
if (i == possbl){
// we are dealing with possible row
if ( (elim_rc=eliminate_row_dup(last_j[0], bm)) ){
for (i=0; i<possbl; i++){
all[last_j[i]][last_k[i]] = cell[i];
if (!QuickSolve)
RedBits[last_j[i]][last_k[i]] = RB[i];
}
if (elim_rc > possbl) {
if (!QuickSolve) {
SetRowColor(last_j[0]);
for (i=1; i<possbl; i++)
BlueBits[last_j[i]][last_k[i]] = bm;
tmpstr.Format("In 3x3 of (%d, %d), eliminated values of ", last_j[0], last_k[0]);
msgstr = tmpstr;
dump_cell(bm);
tmpstr.Format("from row %d", last_j[0]);
msgstr += tmpstr;
SetMsg();
//pMsg->SetRTF(msgstr);
return 1;
}else
elim++;
}
}
}else{
for (i=1; i<possbl; i++)
if ( last_k[i] != last_k[0] )
break;
if (i == possbl){
// we are dealing with possible col
if ( (elim_rc=eliminate_col_dup(last_k[0], bm)) ){
for (i=0; i<possbl; i++){
all[last_j[i]][last_k[i]] = cell[i];
if (!QuickSolve)
RedBits[last_j[i]][last_k[i]] = RB[i];
}
if (elim_rc > possbl) {
if (!QuickSolve){
SetColColor(last_k[0]);
for (i=1; i<possbl; i++)
BlueBits[last_j[i]][last_k[i]] = bm;
tmpstr.Format("In 3x3 of (%d, %d), eliminated values of ", last_j[0], last_k[0]);
msgstr = tmpstr;
dump_cell(bm);
tmpstr.Format("from col %d", last_k[0]);
msgstr += tmpstr;
SetMsg();
//pMsg->SetRTF(msgstr);
return 1;
}else
elim++;
}
}
}
}
}
}
}
}
return elim;
}
/*------------------------------------------------------------
Set the value to the only cell with the posslibitiy in a row
------------------------------------------------------------*/
unsigned CDisplay::eliminate_lonely_row()
{
unsigned j, k,last_k;
unsigned elim=0, possbl, bm;
for (j=0; j<9; j++){
for (bm=0x1; bm<=0x100; bm<<=1){
possbl = 0;
for (k=0; k<9; k++){
if (all[j][k] & bm){
possbl++;
last_k = k;
if (possbl > 1)
break;
}
}
if (possbl == 1 && NoOfPoss(all[j][last_k]) > 1){
all[j][last_k] = bm;
elim++;
if (!QuickSolve) {
SetRowColor(j);
RedBits[j][last_k] = all[j][last_k] & ~bm;
BlueBits[j][last_k] = bm;
msgstr.Format("In it's row (%d, %d) is the only possibility for ", j, last_k);
dump_cell(bm);
SetMsg();
return 1;
}
}
}
}
return elim;
}
/*------------------------------------------------------------
Set the value to the only cell with the posslibitiy in a col
------------------------------------------------------------*/
unsigned CDisplay::eliminate_lonely_col()
{
unsigned j, k,last_j;
unsigned elim=0, possbl, bm;
for (k=0; k<9; k++){
for (bm=0x1; bm<=0x100; bm<<=1){
possbl = 0;
for (j=0; j<9; j++){
if (all[j][k] & bm){
possbl++;
last_j = j;
if (possbl > 1)
break;
}
}
if (possbl == 1 && NoOfPoss(all[last_j][k]) > 1){
all[last_j][k] = bm;
elim++;
if (!QuickSolve) {
SetColColor(k);
RedBits[last_j][k] = all[last_j][k] & ~bm;
BlueBits[last_j][k] = bm;
msgstr.Format("In it's col (%d, %d) is the only possibility for ", last_j, k);
dump_cell(bm);
SetMsg();
return 1;
}
}
}
}
return elim;
}
/*------------------------------------------------------------
Set the value to the only cell with the posslibitiy in a 3x3
------------------------------------------------------------*/
unsigned CDisplay::eliminate_lonely_3x3()
{
unsigned j, k,last_j, last_k, row, col;
unsigned elim=0, possbl, bm;
for (row=0; row<9; row+=3){
for (col=0; col<9; col+=3){
for (bm=0x1; bm<=0x100; bm<<=1){
possbl = 0;
for (j=row; j<row+3; j++){
for (k=col; k<col+3; k++){
if (all[j][k] & bm){
possbl++;
last_j = j;
last_k = k;
if (possbl > 1)
break;
}
}
if (possbl > 1)
break;
}
if (possbl == 1 && NoOfPoss(all[last_j][last_k]) > 1){
all[last_j][last_k] = bm;
elim++;
if (!QuickSolve) {
Set3x3Color(last_j, last_k);
RedBits[last_j][last_k] = all[last_j][last_k] & ~bm;
BlueBits[last_j][last_k] = bm;
tmpstr.Format("In it's 3x3 (%d, %d) is the only possibility for ", last_j, last_k);
msgstr = tmpstr;
//sprintf(buf, "In it's 3x3 (%d, %d) is the only possibility for ", last_j, last_k);
dump_cell(bm);
SetMsg();
//pMsg->SetRTF(msgstr);
return 1;
//printf("\n");
}
}
}
}
}
return elim;
}
/*------------------------------------------------------------
Remove duplicate having (no) possibilities
------------------------------------------------------------*/
int CDisplay::remove_dup(unsigned no)
{
unsigned elim=0;
unsigned j, k, no_of_cell;
unsigned cell;
for (j=0; j<9; j++){
for (k=0; k<9; k++){
// Look for a cell with (no) possibilities
if ( NoOfPoss(all[j][k]) == no){
cell = all[j][k];
// Now check how many cells in the row have these same possibilities
no_of_cell = check_row(j, cell);
if (no_of_cell > no){
if (!QuickSolve) {
check_row(j, cell, 1);
SetRowColor(j);
tmpstr.Format("FATAL ERROR: There is a problem with the puzzle\n, I found %d possibilites of ", no_of_cell);
msgstr = tmpstr;
dump_cell( cell );
tmpstr.Format(" in the row of (%d, %d)\n", j, k);
msgstr += tmpstr;
tmpstr.Format("There should only be %d such possibilities\n\n", no);
msgstr += tmpstr;
ErrorMsg(msgstr);
}
return -1;
}
if (no_of_cell == no){
if (eliminate_row_dup(j, cell)){
if (!QuickSolve) {
check_row(j, cell, 1);
SetRowColor(j);
tmpstr.Format("Found %d duplicates of ", no_of_cell);
msgstr = tmpstr;
dump_cell(cell);
tmpstr.Format(" in row of (%d, %d)", j, k);
msgstr += tmpstr;
SetMsg();
//pMsg->SetRTF(msgstr);
return 1;
}else
elim++;
}
}
// Check how many cells in the col have these same possibilities
no_of_cell = check_col(k, cell);
if (no_of_cell > no){
if (!QuickSolve) {
SetColColor(k);
check_col(k, cell, 1);
tmpstr.Format("FATAL ERROR: There is a problem with the puzzle\n, I found %d possibilites of ", no_of_cell);
msgstr = tmpstr;
dump_cell( cell );
tmpstr.Format(" in the col of (%d, %d)\n", j, k);
msgstr += tmpstr;
tmpstr.Format("There should only be %d such possibilities\n\n", no);
msgstr += tmpstr;
ErrorMsg(msgstr);
}
return -1;
}
if (no_of_cell == no){
if (eliminate_col_dup(k, cell)){
if (!QuickSolve) {
SetColColor(k);
check_col(k, cell, 1);
tmpstr.Format("Found %d duplicates of ", no_of_cell);
msgstr = tmpstr;
dump_cell(cell);
tmpstr.Format(" in col of (%d, %d)", j, k);
msgstr += tmpstr;
SetMsg();
//pMsg->SetRTF(msgstr);
return 1;
}
elim++;
}
}
// Check how many cells in the 3x3 have these same possibilities
no_of_cell = check_3x3(j, k, cell);
if (no_of_cell > no){
if (!QuickSolve) {
Set3x3Color(j, k);
check_3x3(j, k, cell, 1);
tmpstr.Format("FATAL ERROR: There is a problem with the puzzle\n, I found %d possibilites of ", no_of_cell);
msgstr = tmpstr;
dump_cell( cell );
tmpstr.Format(" in the 3x3 of (%d, %d)\n", j, k);
msgstr += tmpstr;
tmpstr.Format("There should only be %d such possibilities\n\n", no);
msgstr += tmpstr;
ErrorMsg(msgstr);
}
return -1;
}
if (no_of_cell == no){
if (eliminate_3x3_dup(j, k, cell)){
if (!QuickSolve) {
Set3x3Color(j, k);
check_3x3(j, k, cell, 1);
tmpstr.Format("Found %d duplicates of ", no_of_cell);
msgstr = tmpstr;
dump_cell(cell);
tmpstr.Format(" in 3x3 of (%d, %d)", j, k);
msgstr += tmpstr;
SetMsg();
//pMsg->SetRTF(msgstr);
return 1;
}
elim++;
}
}
}
}
}
return elim;
}
/*------------------------------------------------------------
Checks for no of possibilities for cell in a row
------------------------------------------------------------*/
unsigned CDisplay::check_row(unsigned row, unsigned cell, unsigned setBlue)
{
unsigned poss, found, i;
found = 0;
poss = NoOfPoss(cell);
for (i=0; i<9; i++){
if (NoOfPoss(all[row][i]) <= poss){
if ( (all[row][i] & ~cell) == 0 ){
found++;
if (!QuickSolve && setBlue)
BlueBits[row][i] = cell;
}
}
}
return found;
}
/*------------------------------------------------------------
Checks for no of possibilities for cell in a col
------------------------------------------------------------*/
unsigned CDisplay::check_col(unsigned col, unsigned cell, unsigned setBlue)
{
unsigned poss, found, i;
found = 0;
poss = NoOfPoss(cell);
for (i=0; i<9; i++){
if (NoOfPoss(all[i][col]) <= poss){
if ( (all[i][col] & ~cell) == 0 ){
found++;
if (!QuickSolve && setBlue)
BlueBits[i][col] = cell;
}
}
}
return found;
}
/*------------------------------------------------------------
Checks for no of possibilities for cell in a 3x3
------------------------------------------------------------*/
unsigned CDisplay::check_3x3(unsigned row, unsigned col, unsigned cell, unsigned setBlue)
{
unsigned poss, found, r, c, j, k;
found = 0;
poss = NoOfPoss(cell);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?