public void Perform(Puzzle P) { switch (m_Type) { case eActionType.eAT_EliminateIcon: P.EliminateIcon(m_iRow, m_iCol, m_iIcon); break; case eActionType.eAT_SetFinalIcon: P.SetFinalIcon(m_iRow, m_iCol, m_iIcon); break; case eActionType.eAT_RestoreIcon: P.m_Rows[m_iRow].m_Cells[m_iCol].m_bValues[m_iIcon] = true; P.m_Rows[m_iRow].m_Cells[m_iCol].m_iFinalIcon = P.m_Rows[m_iRow].m_Cells[m_iCol].GetRemainingIcon(); break; } }
private void AnalyzeVerticalThreeMidNot(Puzzle P) { int iIcon1 = P.m_Solution[m_iRow, m_iCol]; int iIcon2 = m_iNotCell; int iIcon3 = P.m_Solution[m_iRow3, m_iCol]; for (int i = 0; i < P.m_iSize; i++) { if (P.m_Rows[m_iRow2].m_Cells[i].m_iFinalIcon == iIcon2) { P.EliminateIcon(this, m_iRow, i, iIcon1); P.EliminateIcon(this, m_iRow3, i, iIcon3); } else if (P.m_Rows[m_iRow].m_Cells[i].m_iFinalIcon == iIcon1) { P.EliminateIcon(this, m_iRow2, i, iIcon2); P.SetFinalIcon(this, m_iRow3, i, iIcon3); return; } else if (P.m_Rows[m_iRow3].m_Cells[i].m_iFinalIcon == iIcon3) { P.EliminateIcon(this, m_iRow2, i, iIcon2); P.SetFinalIcon(this, m_iRow, i, iIcon1); return; } else if (P.m_Rows[m_iRow].m_Cells[i].m_iFinalIcon >= 0) { P.EliminateIcon(this, m_iRow3, i, iIcon3); } else if (P.m_Rows[m_iRow3].m_Cells[i].m_iFinalIcon >= 0) { P.EliminateIcon(this, m_iRow, i, iIcon1); } } }
private void AnalyzeVerticalTwo(Puzzle P) { int iIcon1 = P.m_Solution[m_iRow, m_iCol]; int iIcon2 = P.m_Solution[m_iRow2, m_iCol]; for (int i = 0; i < P.m_iSize; i++) { if (!P.m_Rows[m_iRow].m_Cells[i].m_bValues[iIcon1]) { P.EliminateIcon(this, m_iRow2, i, iIcon2); } else if (!P.m_Rows[m_iRow2].m_Cells[i].m_bValues[iIcon2]) { P.EliminateIcon(this, m_iRow, i, iIcon1); } if (P.m_Rows[m_iRow].m_Cells[i].m_iFinalIcon == iIcon1) { P.SetFinalIcon(this, m_iRow2, i, iIcon2); return; } else if (P.m_Rows[m_iRow2].m_Cells[i].m_iFinalIcon == iIcon2) { P.SetFinalIcon(this, m_iRow, i, iIcon1); return; } } }
private void AnalyzeVerticalEitherOr(Puzzle P) { int iIcon1 = P.m_Solution[m_iRow, m_iCol]; int iIcon2 = (m_iRow2 == m_iNotCell) ? m_iHorizontal1 : P.m_Solution[m_iRow2, m_iCol]; int iIcon3 = (m_iRow3 == m_iNotCell) ? m_iHorizontal1 : P.m_Solution[m_iRow3, m_iCol]; int iIcon2Col = -1; int iIcon3Col = -1; for( int i = 0; i < P.m_iSize; i++ ) { if (P.m_Rows[m_iRow2].m_Cells[i].m_iFinalIcon == iIcon2) { P.EliminateIcon(this, m_iRow3, i, iIcon3); iIcon2Col = i; } if (P.m_Rows[m_iRow3].m_Cells[i].m_iFinalIcon == iIcon3) { P.EliminateIcon(this, m_iRow2, i, iIcon2); iIcon3Col = i; } if (!P.m_Rows[m_iRow2].m_Cells[i].m_bValues[iIcon2] && !P.m_Rows[m_iRow3].m_Cells[i].m_bValues[iIcon3]) { // If neither icon is in this column, icon1 cant be here either P.EliminateIcon(this, m_iRow, i, iIcon1); } if (P.m_Rows[m_iRow].m_Cells[i].m_iFinalIcon == iIcon1) { for (int j = 0; j < P.m_iSize; j++) { if( j == i ) continue; if (P.m_Rows[m_iRow2].m_Cells[j].m_iFinalIcon == iIcon2) { P.SetFinalIcon(this, m_iRow3, i, iIcon3); } else if (P.m_Rows[m_iRow3].m_Cells[j].m_iFinalIcon == iIcon3) { P.SetFinalIcon(this, m_iRow2, i, iIcon2); } } } } if (iIcon2Col >= 0 && iIcon3Col >= 0) { for (int i = 0; i < P.m_iSize; i++) { if (i != iIcon2Col && i != iIcon3Col) { P.EliminateIcon(this, m_iRow, i, iIcon1); } } } }
private void AnalyzeHorizontalNextTo(Puzzle P) { int iIcon1 = P.m_Solution[m_iRow, m_iCol]; int iIcon2 = P.m_Solution[m_iRow2, m_iCol2]; for (int i = 0; i < P.m_iSize; i++) { if (P.m_Rows[m_iRow].m_Cells[i].m_iFinalIcon == iIcon1) { if (i == 0) { P.SetFinalIcon(this, m_iRow2, 1, iIcon2); } else if (i == P.m_iSize - 1) { P.SetFinalIcon(this, m_iRow2, i - 1, iIcon2); } else { for (int j = 0; j < P.m_iSize; j++) { if (j == (i - 1) || j == (i + 1)) continue; P.EliminateIcon(this, m_iRow2, j, iIcon2); } } break; } else if (P.m_Rows[m_iRow2].m_Cells[i].m_iFinalIcon == iIcon2) { if (i == 0) { P.SetFinalIcon(this, m_iRow, 1, iIcon1); } else if (i == P.m_iSize - 1) { P.SetFinalIcon(this, m_iRow, i - 1, iIcon1); } else { for (int j = 0; j < P.m_iSize; j++) { if (j == (i - 1) || j == (i + 1)) continue; P.EliminateIcon(this, m_iRow, j, iIcon1); } } break; } else { if (i == 0) { if (!P.m_Rows[m_iRow2].m_Cells[i + 1].m_bValues[iIcon2]) { P.EliminateIcon(this, m_iRow, i, iIcon1); } if (!P.m_Rows[m_iRow].m_Cells[i + 1].m_bValues[iIcon1]) { P.EliminateIcon(this, m_iRow2, i, iIcon2); } } else if (i == P.m_iSize - 1) { if (!P.m_Rows[m_iRow2].m_Cells[i - 1].m_bValues[iIcon2]) { P.EliminateIcon(this, m_iRow, i, iIcon1); } if (!P.m_Rows[m_iRow].m_Cells[i - 1].m_bValues[iIcon1]) { P.EliminateIcon(this, m_iRow2, i, iIcon2); } } else { if (!P.m_Rows[m_iRow2].m_Cells[i + 1].m_bValues[iIcon2] && !P.m_Rows[m_iRow2].m_Cells[i - 1].m_bValues[iIcon2]) { P.EliminateIcon(this, m_iRow, i, iIcon1); } if (!P.m_Rows[m_iRow].m_Cells[i + 1].m_bValues[iIcon1] && !P.m_Rows[m_iRow].m_Cells[i - 1].m_bValues[iIcon1]) { P.EliminateIcon(this, m_iRow2, i, iIcon2); } } } } }
private void AnalyzeHorizontalLeftOf(Puzzle P) { int iIcon1 = P.m_Solution[m_iRow, m_iCol]; int iIcon2 = P.m_Solution[m_iRow2, m_iCol2]; int iFirstPossibleLeft = 0; for( iFirstPossibleLeft = 0; iFirstPossibleLeft < P.m_iSize; iFirstPossibleLeft++ ) { if( P.m_Rows[m_iRow].m_Cells[iFirstPossibleLeft].m_bValues[iIcon1] ) break; } int iFirstPossibleRight = P.m_iSize - 1; for (iFirstPossibleRight = P.m_iSize - 1; iFirstPossibleRight >= 0; iFirstPossibleRight--) { if (P.m_Rows[m_iRow2].m_Cells[iFirstPossibleRight].m_bValues[iIcon2]) break; } if (iFirstPossibleLeft + 1 == iFirstPossibleRight) { // we have a solution for this clue P.SetFinalIcon(this, m_iRow, iFirstPossibleLeft, iIcon1); P.SetFinalIcon(this, m_iRow2, iFirstPossibleRight, iIcon2); } else { // Remove all icon2's from the left side of the first possible left for (int i = 0; i <= iFirstPossibleLeft; i++) { P.EliminateIcon(this, m_iRow2, i, iIcon2); } // Remove all the icon1's from the right side of the first possible right for (int i = iFirstPossibleRight; i < P.m_iSize; i++) { P.EliminateIcon(this, m_iRow, i, iIcon1); } } }
private bool SolveSpan(int iCol, int iRow1, int iIcon1, bool bNot1, int iRow2, int iIcon2, bool bNot2, int iRow3, int iIcon3, bool bNot3, Puzzle P) { int iFinal1 = P.m_Rows[iRow1].m_Cells[iCol].m_iFinalIcon; if (iFinal1 == iIcon1) { if (iCol < 2) { if (bNot1) { int iFinal2Right = P.m_Rows[iRow2].m_Cells[iCol + 1].m_iFinalIcon; if (iFinal2Right == iIcon2) { P.SetFinalIcon(this, iRow3, iCol, iIcon3); } } else { if (bNot2) { P.EliminateIcon(this, iRow2, iCol + 1, iIcon2); } else { P.SetFinalIcon(this, iRow2, iCol + 1, iIcon2); } if (bNot3) { P.EliminateIcon(this, iRow3, iCol + 2, iIcon3); } else { P.SetFinalIcon(this, iRow3, iCol + 2, iIcon3); } return true; } } else if (iCol > P.m_iSize - 3) { if (bNot1) { int iFinal2Left = P.m_Rows[iRow2].m_Cells[iCol - 1].m_iFinalIcon; if (iFinal2Left == iIcon2) { P.SetFinalIcon(this, iRow3, iCol, iIcon3); } } else { if (bNot2) P.EliminateIcon(this, iRow2, iCol - 1, iIcon2); else P.SetFinalIcon(this, iRow2, iCol - 1, iIcon2); if (bNot3) P.EliminateIcon(this, iRow3, iCol - 2, iIcon3); else P.SetFinalIcon(this, iRow3, iCol - 2, iIcon3); return true; } } else { int iFinal2Left = P.m_Rows[iRow2].m_Cells[iCol - 1].m_iFinalIcon; int iFinal2Right = P.m_Rows[iRow2].m_Cells[iCol + 1].m_iFinalIcon; if (iFinal2Left == iIcon2) { if (bNot1) { P.SetFinalIcon(this, iRow3, iCol, iIcon3); } else if (bNot2) { P.SetFinalIcon(this, iRow3, iCol + 2, iIcon3); } else if (bNot3) { P.EliminateIcon(this, iRow3, iCol - 2, iIcon3); } else { P.SetFinalIcon(this, iRow3, iCol - 2, iIcon3); } return true; } else if (iFinal2Right == iIcon2) { if (bNot1) { P.SetFinalIcon(this, iRow3, iCol, iIcon3); } else if (bNot2) { P.SetFinalIcon(this, iRow3, iCol - 2, iIcon3); } else if (bNot3) { P.EliminateIcon(this, iRow3, iCol + 2, iIcon3); } else { P.SetFinalIcon(this, iRow3, iCol + 2, iIcon3); } return true; } else if (!P.m_Rows[iRow2].m_Cells[iCol - 1].m_bValues[iIcon2] && !bNot1 && !bNot2) { P.SetFinalIcon(this, iRow2, iCol + 1, iIcon2); if (bNot3) P.EliminateIcon(this, iRow3, iCol + 2, iIcon3); else P.SetFinalIcon(this, iRow3, iCol + 2, iIcon3); return true; } else if (!P.m_Rows[iRow2].m_Cells[iCol + 1].m_bValues[iIcon2] && !bNot1 && !bNot2) { P.SetFinalIcon(this, iRow2, iCol - 1, iIcon2); if (bNot3) P.EliminateIcon(this, iRow3, iCol - 2, iIcon3); else P.SetFinalIcon(this, iRow3, iCol - 2, iIcon3); return true; } else if (!bNot3) { int iFinal3Left = P.m_Rows[iRow3].m_Cells[iCol - 2].m_iFinalIcon; int iFinal3Right = P.m_Rows[iRow3].m_Cells[iCol + 2].m_iFinalIcon; if (iFinal3Left == iIcon3) { if (bNot1) { P.SetFinalIcon(this, iRow2, iCol - 3, iIcon2); return true; } else if (bNot2) { P.EliminateIcon(this, iRow2, iCol - 1, iIcon2); } else { P.SetFinalIcon(this, iRow2, iCol - 1, iIcon2); return true; } } else if (iFinal3Right == iIcon3) { if (bNot1) { P.SetFinalIcon(this, iRow2, iCol + 3, iIcon2); return true; } else if (bNot2) { P.EliminateIcon(this, iRow2, iCol + 1, iIcon2); } else { P.SetFinalIcon(this, iRow2, iCol + 1, iIcon2); return true; } } else if (!P.m_Rows[iRow3].m_Cells[iCol - 2].m_bValues[iIcon3] && !bNot1 && !bNot2) { P.SetFinalIcon(this, iRow2, iCol + 1, iIcon2); P.SetFinalIcon(this, iRow3, iCol + 2, iIcon3); return true; } else if (!P.m_Rows[iRow3].m_Cells[iCol + 2].m_bValues[iIcon3] && !bNot1 && !bNot2) { P.SetFinalIcon(this, iRow2, iCol - 1, iIcon2); P.SetFinalIcon(this, iRow3, iCol - 2, iIcon3); return true; } else if( !bNot1 ) { for (int j = 0; j < P.m_iSize; j++) { if (!bNot2 && j != (iCol - 1) && j != (iCol + 1)) { P.EliminateIcon(this, iRow2, j, iIcon2); } if (j != (iCol - 2) && j != (iCol + 2)) { P.EliminateIcon(this, iRow3, j, iIcon3); } } } } } } else if (iFinal1 >= 0 && !bNot1 && !bNot2 && !bNot3) { int iFinal3 = P.m_Rows[iRow3].m_Cells[iCol].m_iFinalIcon; if (iFinal3 != iIcon3 && iFinal3 >= 0) { if (iCol == 0) { P.EliminateIcon(this, iRow2, iCol + 1, iIcon2); } else if (iCol == P.m_iSize - 1) { P.EliminateIcon(this, iRow2, iCol - 1, iIcon2); } else if (iCol == P.m_iSize - 3) { P.EliminateIcon(this, iRow1, iCol + 2, iIcon1); P.EliminateIcon(this, iRow2, iCol + 1, iIcon2); P.EliminateIcon(this, iRow3, iCol + 2, iIcon3); } else if (iCol == 2) { P.EliminateIcon(this, iRow1, iCol - 2, iIcon1); P.EliminateIcon(this, iRow2, iCol - 1, iIcon2); P.EliminateIcon(this, iRow3, iCol - 2, iIcon3); } } } if (!P.m_Rows[iRow1].m_Cells[iCol].m_bValues[iIcon1] && !bNot1) { if (!bNot3) { if (iCol + 4 < P.m_iSize) { if (!P.m_Rows[iRow1].m_Cells[iCol + 4].m_bValues[iIcon1]) { P.EliminateIcon(this, iRow3, iCol + 2, iIcon3); } } else if (iCol + 2 < P.m_iSize) { P.EliminateIcon(this, iRow3, iCol + 2, iIcon3); } if (iCol - 4 >= 0) { if (!P.m_Rows[iRow1].m_Cells[iCol - 4].m_bValues[iIcon1]) { P.EliminateIcon(this, iRow3, iCol - 2, iIcon3); } } else if (iCol - 2 >= 0) { P.EliminateIcon(this, iRow3, iCol - 2, iIcon3); } } if (!bNot2) { if (iCol + 2 < P.m_iSize) { if (!P.m_Rows[iRow1].m_Cells[iCol + 2].m_bValues[iIcon1]) { P.EliminateIcon(this, iRow2, iCol + 1, iIcon2); } } if (iCol - 2 >= 0) { if (!P.m_Rows[iRow1].m_Cells[iCol - 2].m_bValues[iIcon1]) { P.EliminateIcon(this, iRow2, iCol - 1, iIcon2); } } } } if (P.m_Rows[iRow2].m_Cells[iCol].m_iFinalIcon == iIcon2 && !bNot2) { // Middle icon is known, eliminate impossible end icons for (int i = 0; i < P.m_iSize; i++) { if (i != iCol - 1 && i != iCol + 1) { if (!bNot1) P.EliminateIcon(this, iRow1, i, iIcon1); if (!bNot3) P.EliminateIcon(this, iRow3, i, iIcon3); } } } return false; }
private void AnalyzeGiven(Puzzle P) { P.SetFinalIcon(this, m_iRow, m_iCol, P.m_Solution[m_iRow, m_iCol]); m_iUseCount += 25; }