Example #1
0
        public void Reset(ArrayList originalData)
        {
            Init();

            ThreeTupleOfIntegers[] items = new ThreeTupleOfIntegers[originalData.Count];

            for (int i = 0; i < originalData.Count; i++)
            {
                items[i] = (ThreeTupleOfIntegers)originalData[i];
            }

            Set(items);
        }
Example #2
0
        public ThreeTupleOfIntegers ReturnItem(Random r, SudokuPossibleToSetItem sudokuPossibleToSetItem, out string debugString)
        {
            int                  minNumberOfPossibleItemsToSet = int.MaxValue;
            StringBuilder        sb;
            ThreeTupleOfIntegers item;

            _numberOfCallsToReturnItem++;

            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    if (sudokuPossibleToSetItem.numberOfPossibleItemsRows[i, j] >= 2)
                    {
                        if (sudokuPossibleToSetItem.numberOfPossibleItemsRows[i, j] < minNumberOfPossibleItemsToSet)
                        {
                            minNumberOfPossibleItemsToSet = sudokuPossibleToSetItem.numberOfPossibleItemsRows[i, j];
                        }

                        _collectionSudokuPossibleHolder[sudokuPossibleToSetItem.numberOfPossibleItemsRows[i, j] - 2].Add(i, j, ReturnIntArray(sudokuPossibleToSetItem.rows, i, j));
                    }
                }
            }

            if (minNumberOfPossibleItemsToSet <= 9)
            {
                item = _collectionSudokuPossibleHolder[minNumberOfPossibleItemsToSet - 2].ReturnItem(r);
            }
            else
            {
                item = new ThreeTupleOfIntegers(-1, -1, 0);
            }

            sb = new StringBuilder(string.Format("Simulate item {0}\r\n\r\n", item.ToString()));

            for (int i = 0; i < 8; i++)
            {
                sb.Append(_collectionSudokuPossibleHolder[i].ToString() + "\r\n");
            }

            debugString = sb.ToString().TrimEnd();

            return(item);
        }
Example #3
0
        public ThreeTupleOfIntegers[] Process(
            SudokuBoard sudokuBoard,
            Random r,
            bool isDebug,
            out int numberOfItemsSetDueToAloneInCell,
            out int numberOfItemsSetDueToAlonePossibleInRowColumnAndOrSquare,
            out int totalNumberOfItemsPossibleToSetWithoutCausingConflict,
            out int numberOfErrorsNotPossibleToSetAnyItemInCell,
            out int numberOfErrorsNotUniqueItemAlonePossible,
            out bool simulated,
            out string debugString)
        {
            int           i, j, k, n, numberOfItemsSetInIteration, squareIndex;
            string        str = "", commaSeparatedStringOfIntegers, sudokuPossibleToSetItemStr;
            StringBuilder sb1, sb2, tmpStringBuilder;
            ArrayList     itemsSet = new ArrayList();
            ArrayList     itemsPossibleToSetDueToAlonePossibleInRowColumnAndOrSquare = new ArrayList();

            ThreeTupleOfIntegers[] ArrayOfThreeTupleOfIntegers;
            ThreeTupleOfIntegers   threeTupleOfIntegers;

            int[] tmpIntArray = new int[9];
            numberOfItemsSetInIteration      = 0;
            numberOfItemsSetDueToAloneInCell = 0;
            numberOfItemsSetDueToAlonePossibleInRowColumnAndOrSquare = 0;
            numberOfErrorsNotPossibleToSetAnyItemInCell = 0;
            numberOfErrorsNotUniqueItemAlonePossible    = 0;
            simulated = false;

            totalNumberOfItemsPossibleToSetWithoutCausingConflict = _sudokuPossibleToSetItem.totalNumberOfItemsPossibleToSetWithoutCausingConflict;
            sudokuPossibleToSetItemStr = _sudokuPossibleToSetItem.ToString();


            if (totalNumberOfItemsPossibleToSetWithoutCausingConflict == 0)
            {
                debugString = "Not possible to set anything!!";
                return(null);
            }

            sb1 = new StringBuilder();
            sb2 = new StringBuilder("Result: The following ####REPLACE_NUMBER_OF_ITEMS##### item(s) are possible to set, {row, column, item}:\r\n\r\n");
            tmpStringBuilder = new StringBuilder();

            sb1.Append("\r\n------------------------Find cells to set------------------------\r\n\r\n");

            for (i = 0; i < 9; i++)
            {
                for (j = 0; j < 9; j++)
                {
                    threeTupleOfIntegers = null;
                    sb1.Append(string.Format("[{0}, {1}]: ", i + 1, j + 1));
                    squareIndex = (3 * (i / 3)) + (j / 3);

                    if (sudokuBoard.ItemIsSet(i, j))
                    {
                        sb1.Append(string.Format("Cell already set with item {0}.\r\n", sudokuBoard.ReturnItem(i, j).ToString()));
                    }
                    else
                    {
                        if (_sudokuPossibleToSetItem.numberOfPossibleItemsRows[i, j] == 0)
                        {
                            sb1.Append("ERROR!! Not possible to set any item in cell that does not cause conflict!\r\n");
                            numberOfErrorsNotPossibleToSetAnyItemInCell++;
                        }
                        else if (_sudokuPossibleToSetItem.numberOfPossibleItemsRows[i, j] == 1)
                        {
                            numberOfItemsSetDueToAloneInCell++;
                            threeTupleOfIntegers = new ThreeTupleOfIntegers(i, j, _sudokuPossibleToSetItem.rows[i, j, 0]);
                            itemsSet.Add(threeTupleOfIntegers);
                            numberOfItemsSetInIteration++;

                            sb1.Append(string.Format("CAN SET ITEM {0}. The item is alone in cell.\r\n", _sudokuPossibleToSetItem.rows[i, j, 0].ToString()));
                            sb2.Append("{" + string.Format("{0}, {1}, {2}", i + 1, j + 1, _sudokuPossibleToSetItem.rows[i, j, 0]) + "}\r\n");
                        }
                        else
                        {
                            tmpStringBuilder.Clear();
                            itemsPossibleToSetDueToAlonePossibleInRowColumnAndOrSquare.Clear();

                            for (k = 0; k < _sudokuPossibleToSetItem.numberOfPossibleItemsRows[i, j]; k++)
                            {
                                n = _sudokuPossibleToSetItem.rows[i, j, k];

                                if (IsItemAloneInRow(i, n))
                                {
                                    Utility.AddIfNotExistsAlready(itemsPossibleToSetDueToAlonePossibleInRowColumnAndOrSquare, _sudokuPossibleToSetItem.rows[i, j, k]);
                                    tmpStringBuilder.Append(string.Format("Item {0} alone possible in row. ", n.ToString()));
                                }

                                if (IsItemAloneInColumn(j, n))
                                {
                                    Utility.AddIfNotExistsAlready(itemsPossibleToSetDueToAlonePossibleInRowColumnAndOrSquare, _sudokuPossibleToSetItem.rows[i, j, k]);
                                    str = string.IsNullOrEmpty(tmpStringBuilder.ToString()) ? "" : ", ";
                                    tmpStringBuilder.Append(string.Format("Item {0} alone possible in column. ", n.ToString()));
                                }

                                if (IsItemAloneInSquare(squareIndex, n))
                                {
                                    Utility.AddIfNotExistsAlready(itemsPossibleToSetDueToAlonePossibleInRowColumnAndOrSquare, _sudokuPossibleToSetItem.rows[i, j, k]);
                                    tmpStringBuilder.Append(string.Format("Item {0} alone possible in squre. ", n.ToString()));
                                }
                            }

                            if (itemsPossibleToSetDueToAlonePossibleInRowColumnAndOrSquare.Count == 0)
                            {
                                FillTempIntArray(_sudokuPossibleToSetItem.rows, i, j, _sudokuPossibleToSetItem.numberOfPossibleItemsRows[i, j], tmpIntArray);
                                sb1.Append(string.Format("Unable to set item. Item(s) not causing conflict: {0}.\r\n", Utility.ReturnString(tmpIntArray, _sudokuPossibleToSetItem.numberOfPossibleItemsRows[i, j])));
                            }
                            else
                            {
                                n = (int)itemsPossibleToSetDueToAlonePossibleInRowColumnAndOrSquare[0];

                                if (itemsPossibleToSetDueToAlonePossibleInRowColumnAndOrSquare.Count == 1)
                                {
                                    numberOfItemsSetDueToAlonePossibleInRowColumnAndOrSquare++;
                                }
                                else
                                {
                                    numberOfErrorsNotUniqueItemAlonePossible++;
                                }

                                threeTupleOfIntegers = new ThreeTupleOfIntegers(i, j, n);
                                itemsSet.Add(threeTupleOfIntegers);

                                numberOfItemsSetInIteration++;

                                sb2.Append("{" + string.Format("{0}, {1}, {2}", i + 1, j + 1, n) + "}\r\n");

                                if (itemsPossibleToSetDueToAlonePossibleInRowColumnAndOrSquare.Count == 1)
                                {
                                    sb1.Append(string.Format("CAN SET ITEM {0}. The item is alone possible (in row, column and/or cell). {1} \r\n", n.ToString(), tmpStringBuilder.ToString()));
                                }
                                else
                                {
                                    commaSeparatedStringOfIntegers = Utility.ReturnCommaSeparatedStringOfIntegers(itemsPossibleToSetDueToAlonePossibleInRowColumnAndOrSquare);
                                    sb1.Append(string.Format("ERROR!! Not unique alone possible. The following items are possible to set with that rule: {0}. Anyway, set item {1}. {2}\r\n", commaSeparatedStringOfIntegers, n.ToString(), tmpStringBuilder.ToString()));
                                }
                            }
                        }
                    }

                    if (threeTupleOfIntegers != null)
                    {
                        sudokuBoard.Set(threeTupleOfIntegers);
                        _sudokuPossibleToSetItem.Update(sudokuBoard, threeTupleOfIntegers.rowIndex, threeTupleOfIntegers.columnIndex);
                    }
                }

                sb1.Append("\r\n");
            }

            if (numberOfItemsSetInIteration == 0)
            {
                simulated = true;
                ArrayOfThreeTupleOfIntegers = new ThreeTupleOfIntegers[1];
                SudokuSimulateItem sudokuSimulateItem = new SudokuSimulateItem();
                ArrayOfThreeTupleOfIntegers[0] = sudokuSimulateItem.ReturnItem(r, _sudokuPossibleToSetItem, out str);
                sudokuBoard.Set(ArrayOfThreeTupleOfIntegers[0]);
                _sudokuPossibleToSetItem.Update(sudokuBoard, ArrayOfThreeTupleOfIntegers[0].rowIndex, ArrayOfThreeTupleOfIntegers[0].columnIndex);
            }
            else
            {
                ArrayOfThreeTupleOfIntegers = new ThreeTupleOfIntegers[numberOfItemsSetInIteration];

                for (i = 0; i < numberOfItemsSetInIteration; i++)
                {
                    ArrayOfThreeTupleOfIntegers[i] = (ThreeTupleOfIntegers)itemsSet[i];
                }

                if (isDebug)
                {
                    SudokuSimulateItem   sudokuSimulateItem      = new SudokuSimulateItem();
                    ThreeTupleOfIntegers tmpThreeTupleOfIntegers = sudokuSimulateItem.ReturnItem(r, _sudokuPossibleToSetItem, out str);
                }
            }

            if (simulated)
            {
                debugString = sudokuPossibleToSetItemStr + sb1.ToString() + "\r\n\r\n----------------------------------- Simulate one item -----------------------------------\r\n\r\n" + str + "\r\n\r\n---------- Sudoku board after iteration ----------\r\n\r\n" + sudokuBoard.SudokuBoardString;
            }
            else
            {
                if (!isDebug)
                {
                    debugString = sudokuPossibleToSetItemStr + sb1.ToString() + sb2.ToString().Replace("####REPLACE_NUMBER_OF_ITEMS#####", numberOfItemsSetInIteration.ToString()).TrimEnd() + "\r\n\r\n---------- Sudoku board after iteration ----------\r\n\r\n" + sudokuBoard.SudokuBoardString;
                }
                else
                {
                    debugString = sudokuPossibleToSetItemStr + sb1.ToString() + sb2.ToString().Replace("####REPLACE_NUMBER_OF_ITEMS#####", numberOfItemsSetInIteration.ToString()).TrimEnd() + "\r\n\r\n---------- Sudoku board after iteration ----------\r\n\r\n" + sudokuBoard.SudokuBoardString + "\r\n\r\n----------------------------------- DEBUG Simulate one item -----------------------------------\r\n\r\n" + str;
                }
            }

            return(ArrayOfThreeTupleOfIntegers);
        }
Example #4
0
 public void Set(ThreeTupleOfIntegers items)
 {
     this.SetItem(items.rowIndex, items.columnIndex, items.item);
 }