private void EnterPuzzle() { int[] oneDimensionalArray = new int[81]; for (int i = 1; i <= 81; i++) { if (this.Controls.ContainsKey("tb" + i.ToString())) { TextBox tb = this.Controls["tb" + i.ToString()] as TextBox; int result; if (tb.Text != "" && Int32.TryParse(tb.Text, out result)) { oneDimensionalArray[i - 1] = result; } else { oneDimensionalArray[i - 1] = 0; } } } int[,] twoDimensionalArray = new int[9, 9]; for (int x = 0; x < 9; x++) { for (int y = 0; y < 9; y++) { twoDimensionalArray[x, y] = oneDimensionalArray[9 * x + y]; } } sudokuGrid = new SudokuGrid(twoDimensionalArray); fc = new FourCube(sudokuGrid); }
public int Difference(FourCube fc, FourCube fc2) { int diff = 0; for (int CubeNr = 0; CubeNr < 4; CubeNr++) { for (int azimuthal = 0; azimuthal < 9; azimuthal++) { for (int column = 0; column < 9; column++) { for (int row = 0; row < 9; row++) { if (fc.cubeCells[CubeNr, azimuthal, column, row].confidence != fc2.cubeCells[CubeNr, azimuthal, column, row].confidence) { diff += 1; } else { continue; } } } } } return(diff); }
public void StepPrinting(FourCube fc) { for (int CubeNr = 0; CubeNr < 4; CubeNr++) { Console.WriteLine("*** cube {0}: ***", CubeNr); for (int azimuthal = 0; azimuthal < 9; azimuthal++) { Console.WriteLine("\n"); Console.WriteLine("az {0}:", azimuthal); Console.WriteLine("\n"); for (int row = 0; row < 9; row++) { Console.WriteLine("\n"); for (int column = 0; column < 9; column++) { if (fc.cubeCells[CubeNr, azimuthal, column, row].isClue) { Console.Write("[{0}]", fc.cubeCells[CubeNr, azimuthal, column, row].confidence); } else { Console.Write(" {0} ", fc.cubeCells[CubeNr, azimuthal, column, row].confidence); } } } } } }
}//end of mult public bool Comparator(FourCube Ps, FourCube Pe) { while (true) { for (int CubeNr = 0; CubeNr < 4; CubeNr++) { for (int azimuthal = 0; azimuthal < 9; azimuthal++) { for (int column = 0; column < 9; column++) { for (int row = 0; row < 9; row++) { if (Ps.cubeCells[CubeNr, azimuthal, column, row].confidence == Pe.cubeCells[CubeNr, azimuthal, column, row].confidence) { continue; } else { return(false); } } } } } return(true); } }
private void ClearSudokuGrid() { for (int i = 1; i <= 81; i++) { if (this.Controls.ContainsKey("tb" + i.ToString())) { TextBox tb = this.Controls["tb" + i.ToString()] as TextBox; tb.Text = ""; } } nrOfSteps = 0; sudokuGrid = null; fc = null; foreach (var series in chartDifference.Series) { series.Points.Clear(); } foreach (var series in chartConfidence.Series) { series.Points.Clear(); } lbDigitSelection.Items.Clear(); lbRowSelection.Items.Clear(); lbColumnSelection.Items.Clear(); lbBlockSelection.Items.Clear(); }
//minus 2 four-cube public FourCube Minus(FourCube fc) { CubeCell[,,,] newCubeCells = new CubeCell[4, 9, 9, 9]; for (int cube = 0; cube < 4; cube++) { for (int row = 0; row < 9; row++) { for (int col = 0; col < 9; col++) { for (int azimuth = 0; azimuth < 9; azimuth++) { if (cubeCells[cube, row, col, azimuth].GetIsClue() != true && fc.GetCubeCells()[cube, row, col, azimuth].GetIsClue() != true) { newCubeCells[cube, row, col, azimuth] = new CubeCell(); newCubeCells[cube, row, col, azimuth].SetConfidence(cubeCells[cube, row, col, azimuth].GetConfidence() - fc.GetCubeCells()[cube, row, col, azimuth].GetConfidence()); } else { newCubeCells[cube, row, col, azimuth] = new CubeCell(cubeCells[cube, row, col, azimuth].GetConfidence(), true); } } } } } return(new FourCube(newCubeCells)); }
//compare if two 4-cubes are equal public bool Equals(FourCube fc) { if (this.GetNrOfTwoCellsWithDifferentConfidence(fc) != 0) { return(false); } else { return(true); } }
}//end of Plus public FourCube Subtract(FourCube fc) { FourCube fcc; fcc = setClue(); for (int i = 0; i < 4; i++) { for (int j = 0; j < 9; j++) { for (int k = 0; k < 9; k++) { for (int h = 0; h < 9; h++) { fcc.cubeCells[i, j, k, h].confidence = fc.cubeCells[i, j, k, h].confidence - this.cubeCells[i, j, k, h].confidence; } } } } return(fcc); }//end of Minus
private FourCube setClue() { int[,] Clue = new int[9, 9]; FourCube fc = new FourCube(Clue) { }; for (int CubeNr = 0; CubeNr < 4; CubeNr++) { for (int azimuthal = 0; azimuthal < 9; azimuthal++) { for (int column = 0; column < 9; column++) { for (int row = 0; row < 9; row++) { fc.cubeCells[CubeNr, azimuthal, column, row].isClue = this.cubeCells[CubeNr, azimuthal, column, row].isClue; } } } } return(fc); }
public int GetNrOfTwoCellsWithDifferentConfidence(FourCube fc) { int nrOfCellsWithDifferentConfidence = 0; for (int cube = 0; cube < 4; cube++) { for (int row = 0; row < 9; row++) { for (int col = 0; col < 9; col++) { for (int azimuth = 0; azimuth < 9; azimuth++) { if (fc.GetCubeCells()[cube, row, col, azimuth].GetConfidence() != this.cubeCells[cube, row, col, azimuth].GetConfidence()) { nrOfCellsWithDifferentConfidence++; } } } } } return(nrOfCellsWithDifferentConfidence); }
public int[,] SudokuAnswer(FourCube fc) { int[,] array = new int[9, 9]; for (int column = 0; column < 9; column++) { for (int row = 0; row < 9; row++) { double maxValue = double.MinValue; int positionOfMax = -1; bool isClueAzimiuthal = false; int number = 0; for (int azimuthal = 0; azimuthal < 9; azimuthal++) { if (fc.cubeCells[2, azimuthal, column, row].isClue == true && fc.cubeCells[2, azimuthal, column, row].confidence == 1) { isClueAzimiuthal = true; number = azimuthal + 1; array[row, column] = number; break; } else { if (fc.cubeCells[2, azimuthal, column, row].confidence > maxValue && fc.cubeCells[2, azimuthal, column, row].isClue != true) { maxValue = fc.cubeCells[2, azimuthal, column, row].confidence; positionOfMax = azimuthal; number = azimuthal + 1; } } } if (positionOfMax != -1 && isClueAzimiuthal != true) { array[row, column] = number; } } } return(array); }
//public FourCube GetSolution() //{ // return solution; //} public Rrr(FourCube fc) { this.fc = fc; this.fc2 = fc.Plus(fc.Equalizer().Multiply(2).Minus(fc).Selector()).Minus(fc.Equalizer()); }
private void Step() { Rrr rrr = new Rrr(fc); Rrr intermediate = rrr.GetRrrByANumberOfCycles(rrr, nrOfSteps); if (intermediate.IsReady() == false) { lbDigitSelection.Items.Clear(); lbRowSelection.Items.Clear(); lbColumnSelection.Items.Clear(); lbBlockSelection.Items.Clear(); FourCube equalized4Cube = intermediate.GetFourCubeAfter1RRRCycle().Equalizer(); FourCube selector4Cube = equalized4Cube.Selector(); int[,] azimuth2D = new int[9, 9]; int[,] row2D = new int[9, 9]; int[,] column2D = new int[9, 9]; int[,] block2D = new int[9, 9]; double[,] confidences2D = new double[9, 9]; int[] azimuth1D = new int[81]; double[] confidences1D = new double[81]; int confidenceSmaller02 = 0; int confidenceSmaller04 = 0; int confidenceSmaller06 = 0; int confidenceSmaller08 = 0; int confidenceSmaller10 = 0; for (int row = 0; row < 9; row++) { for (int col = 0; col < 9; col++) { for (int azimuth = 0; azimuth < 9; azimuth++) { if (selector4Cube.GetCubeCells()[3, row, col, azimuth].GetConfidence() == 1) { azimuth2D[row, col] = azimuth + 1; confidences2D[row, col] = equalized4Cube.GetCubeCells()[3, row, col, azimuth].GetConfidence(); } if (selector4Cube.GetCubeCells()[0, row, col, azimuth].GetConfidence() == 1) { column2D[row, col] = azimuth + 1; } if (selector4Cube.GetCubeCells()[1, row, col, azimuth].GetConfidence() == 1) { row2D[row, col] = azimuth + 1; } if (selector4Cube.GetCubeCells()[2, row, col, azimuth].GetConfidence() == 1) { block2D[row, col] = azimuth + 1; } } } } for (int x = 0; x < 9; x++) { for (int y = 0; y < 9; y++) { azimuth1D[9 * x + y] = azimuth2D[x, y]; confidences1D[9 * x + y] = confidences2D[x, y]; } } for (int i = 1; i <= 81; i++) { if (this.Controls.ContainsKey("tb" + (i).ToString())) { TextBox tb = this.Controls["tb" + i.ToString()] as TextBox; tb.Text = azimuth1D[i - 1].ToString(); if (confidences1D[i - 1] > 0 && confidences1D[i - 1] <= 0.2) { tb.ForeColor = Color.Pink; confidenceSmaller02++; } else if (confidences1D[i - 1] > 0.2 && confidences1D[i - 1] <= 0.4) { tb.ForeColor = Color.Red; confidenceSmaller04++; } else if (confidences1D[i - 1] > 0.4 && confidences1D[i - 1] <= 0.6) { tb.ForeColor = Color.Green; confidenceSmaller06++; } else if (confidences1D[i - 1] > 0.6 && confidences1D[i - 1] <= 0.8) { tb.ForeColor = Color.Blue; confidenceSmaller08++; } else if (confidences1D[i - 1] > 0.8 && confidences1D[i - 1] <= 1) { tb.ForeColor = Color.Black; confidenceSmaller10++; } } } //chart chartDifference.Series["Differences from the previous step"].Points.Add(intermediate.DifferenceCellsFromLastStep()); //chartDifference.Series["Differences from the solution"].Points.Add(intermediate.DifferenceCellsFromSolution()); chartConfidence.Series["0-0.2"].Points.Add(confidenceSmaller02); chartConfidence.Series["0.2-0.4"].Points.Add(confidenceSmaller04); chartConfidence.Series["0.4-0.6"].Points.Add(confidenceSmaller06); chartConfidence.Series["0.6-0.8"].Points.Add(confidenceSmaller08); chartConfidence.Series["0.8-1"].Points.Add(confidenceSmaller10); //tables for (int x = 0; x < 9; x++) { string s = ""; string row = ""; string col = ""; string block = ""; for (int y = 0; y < 9; y++) { s += azimuth2D[x, y] + ", "; row += row2D[x, y] + ", "; col += column2D[x, y] + ", "; block += block2D[x, y] + ", "; } lbDigitSelection.Items.Add(s); lbRowSelection.Items.Add(row); lbColumnSelection.Items.Add(col); lbBlockSelection.Items.Add(block); } } }
private void btnPreviousStep_Click(object sender, EventArgs e) { if (fc != null || sudokuGrid != null) { if (nrOfSteps > 0) { nrOfSteps--; lbDigitSelection.Items.Clear(); lbRowSelection.Items.Clear(); lbColumnSelection.Items.Clear(); lbBlockSelection.Items.Clear(); Rrr rrr = new Rrr(fc); Rrr intermediate = rrr.GetRrrByANumberOfCycles(rrr, nrOfSteps); FourCube equalized4Cube = intermediate.GetFourCubeAfter1RRRCycle().Equalizer(); FourCube selector4Cube = equalized4Cube.Selector(); int[,] row2D = new int[9, 9]; int[,] column2D = new int[9, 9]; int[,] block2D = new int[9, 9]; int[,] azimuth2D = new int[9, 9]; double[,] confidences2D = new double[9, 9]; int[] azimuth1D = new int[81]; double[] confidences1D = new double[81]; for (int row = 0; row < 9; row++) { for (int col = 0; col < 9; col++) { for (int azimuth = 0; azimuth < 9; azimuth++) { if (selector4Cube.GetCubeCells()[3, row, col, azimuth].GetConfidence() == 1) { azimuth2D[row, col] = azimuth + 1; confidences2D[row, col] = equalized4Cube.GetCubeCells()[3, row, col, azimuth].GetConfidence(); } if (selector4Cube.GetCubeCells()[0, row, col, azimuth].GetConfidence() == 1) { column2D[row, col] = azimuth + 1; } if (selector4Cube.GetCubeCells()[1, row, col, azimuth].GetConfidence() == 1) { row2D[row, col] = azimuth + 1; } if (selector4Cube.GetCubeCells()[2, row, col, azimuth].GetConfidence() == 1) { block2D[row, col] = azimuth + 1; } } } } for (int x = 0; x < 9; x++) { for (int y = 0; y < 9; y++) { azimuth1D[9 * x + y] = azimuth2D[x, y]; confidences1D[9 * x + y] = confidences2D[x, y]; } } for (int i = 1; i <= 81; i++) { if (this.Controls.ContainsKey("tb" + (i).ToString())) { TextBox tb = this.Controls["tb" + i.ToString()] as TextBox; tb.Text = azimuth1D[i - 1].ToString(); if (confidences1D[i - 1] > 0 && confidences1D[i - 1] <= 0.2) { tb.ForeColor = Color.Pink; } else if (confidences1D[i - 1] > 0.2 && confidences1D[i - 1] <= 0.4) { tb.ForeColor = Color.Red; } else if (confidences1D[i - 1] > 0.4 && confidences1D[i - 1] <= 0.6) { tb.ForeColor = Color.Green; } else if (confidences1D[i - 1] > 0.6 && confidences1D[i - 1] <= 0.8) { tb.ForeColor = Color.Blue; } else if (confidences1D[i - 1] > 0.8 && confidences1D[i - 1] <= 1) { tb.ForeColor = Color.Black; } } } if (nrOfSteps >= chartDifference.Series["Differences from the previous step"].Points.Count) { chartDifference.Series["Differences from the previous step"].Points.RemoveAt(nrOfSteps - 1); chartConfidence.Series["0-0.2"].Points.RemoveAt(nrOfSteps - 1); chartConfidence.Series["0.2-0.4"].Points.RemoveAt(nrOfSteps - 1); chartConfidence.Series["0.4-0.6"].Points.RemoveAt(nrOfSteps - 1); chartConfidence.Series["0.6-0.8"].Points.RemoveAt(nrOfSteps - 1); chartConfidence.Series["0.8-1"].Points.RemoveAt(nrOfSteps - 1); } else { chartDifference.Series["Differences from the previous step"].Points.RemoveAt(nrOfSteps); chartConfidence.Series["0-0.2"].Points.RemoveAt(nrOfSteps); chartConfidence.Series["0.2-0.4"].Points.RemoveAt(nrOfSteps); chartConfidence.Series["0.4-0.6"].Points.RemoveAt(nrOfSteps); chartConfidence.Series["0.6-0.8"].Points.RemoveAt(nrOfSteps); chartConfidence.Series["0.8-1"].Points.RemoveAt(nrOfSteps); } //tables for (int x = 0; x < 9; x++) { string s = ""; string row = ""; string col = ""; string block = ""; for (int y = 0; y < 9; y++) { s += azimuth2D[x, y] + ", "; row += row2D[x, y] + ", "; col += column2D[x, y] + ", "; block += block2D[x, y] + ", "; } lbDigitSelection.Items.Add(s); lbRowSelection.Items.Add(row); lbColumnSelection.Items.Add(col); lbBlockSelection.Items.Add(block); } } } else { MessageBox.Show("Please enter a puzzle"); } }
private void Start() { bool isDone = false; while (!isDone) { if (fc != null && sudokuGrid != null) { //inCriticalSection = true; nrOfSteps++; Rrr rrr = new Rrr(fc); Rrr intermediate = rrr.GetRrrByANumberOfCycles(rrr, nrOfSteps); if (intermediate.IsReady() == false) { SetEmptyListBox(lbDigitSelection); SetEmptyListBox(lbRowSelection); SetEmptyListBox(lbColumnSelection); SetEmptyListBox(lbBlockSelection); FourCube equalized4Cube = intermediate.GetFourCubeAfter1RRRCycle().Equalizer(); FourCube selector4Cube = equalized4Cube.Selector(); int[,] azimuth2D = new int[9, 9]; int[,] row2D = new int[9, 9]; int[,] column2D = new int[9, 9]; int[,] block2D = new int[9, 9]; double[,] confidences2D = new double[9, 9]; int[] azimuth1D = new int[81]; double[] confidences1D = new double[81]; int confidenceSmaller02 = 0; int confidenceSmaller04 = 0; int confidenceSmaller06 = 0; int confidenceSmaller08 = 0; int confidenceSmaller10 = 0; for (int row = 0; row < 9; row++) { for (int col = 0; col < 9; col++) { for (int azimuth = 0; azimuth < 9; azimuth++) { if (selector4Cube.GetCubeCells()[3, row, col, azimuth].GetConfidence() == 1) { azimuth2D[row, col] = azimuth + 1; confidences2D[row, col] = equalized4Cube.GetCubeCells()[3, row, col, azimuth].GetConfidence(); } if (selector4Cube.GetCubeCells()[0, row, col, azimuth].GetConfidence() == 1) { column2D[row, col] = azimuth + 1; } if (selector4Cube.GetCubeCells()[1, row, col, azimuth].GetConfidence() == 1) { row2D[row, col] = azimuth + 1; } if (selector4Cube.GetCubeCells()[2, row, col, azimuth].GetConfidence() == 1) { block2D[row, col] = azimuth + 1; } } } } for (int x = 0; x < 9; x++) { for (int y = 0; y < 9; y++) { azimuth1D[9 * x + y] = azimuth2D[x, y]; confidences1D[9 * x + y] = confidences2D[x, y]; } } for (int i = 1; i <= 81; i++) { if (this.Controls.ContainsKey("tb" + (i).ToString())) { TextBox tb = this.Controls["tb" + i.ToString()] as TextBox; SetText(tb, azimuth1D[i - 1].ToString()); if (confidences1D[i - 1] > 0 && confidences1D[i - 1] <= 0.2) { SetColor(tb, Color.Pink); confidenceSmaller02++; } else if (confidences1D[i - 1] > 0.2 && confidences1D[i - 1] <= 0.4) { SetColor(tb, Color.Red); confidenceSmaller04++; } else if (confidences1D[i - 1] > 0.4 && confidences1D[i - 1] <= 0.6) { SetColor(tb, Color.Green); confidenceSmaller06++; } else if (confidences1D[i - 1] > 0.6 && confidences1D[i - 1] <= 0.8) { SetColor(tb, Color.Blue); confidenceSmaller08++; } else if (confidences1D[i - 1] > 0.8 && confidences1D[i - 1] <= 1) { SetColor(tb, Color.Black); confidenceSmaller10++; } } } //chart SetChart(chartDifference, "Differences from the previous step", intermediate.DifferenceCellsFromLastStep()); //chartLine.Series["Differences from the solution"].Points.Add(intermediate.DifferenceCellsFromSolution()); SetChart(chartConfidence, "0-0.2", confidenceSmaller02); SetChart(chartConfidence, "0.2-0.4", confidenceSmaller04); SetChart(chartConfidence, "0.4-0.6", confidenceSmaller06); SetChart(chartConfidence, "0.6-0.8", confidenceSmaller08); SetChart(chartConfidence, "0.8-1", confidenceSmaller10); //tables for (int x = 0; x < 9; x++) { string s = ""; string row = ""; string col = ""; string block = ""; for (int y = 0; y < 9; y++) { s += azimuth2D[x, y] + ", "; row += row2D[x, y] + ", "; col += column2D[x, y] + ", "; block += block2D[x, y] + ", "; } SetListBox(lbDigitSelection, s); SetListBox(lbRowSelection, row); SetListBox(lbColumnSelection, col); SetListBox(lbBlockSelection, block); } isDone = intermediate.IsReady(); Thread.Sleep(timeSpan); } //inCriticalSection = false; } else { MessageBox.Show("Please enter a puzzle"); } } }
public Rrr(int[,] Sudoku) { this.fourCube = new FourCube(Sudoku); }