public void Assign(BoardConstraints inBoardConstraints,Board inBoard)
        {
            mBoard = inBoard;
            for (int i = 0; i < 9; ++i)
            {
                RowsConstraints[i].Assign(inBoardConstraints.RowsConstraints[i]);
                ColumnsConstraints[i].Assign(inBoardConstraints.ColumnsConstraints[i]);
                ThreeByTreeBlockConstraints[i % 3, i / 3].Assign(
                    inBoardConstraints.ThreeByTreeBlockConstraints[i % 3, i / 3]
                );
                for (int j = 0; j < 9; ++j)
                    CellsConstraints[i, j].Assign(inBoardConstraints.CellsConstraints[i, j]);

                if (inBoardConstraints.TopToBottomDiagonalConstraint != null)
                {
                    TopToBottomDiagonalConstraint = new DiagonalConstraint(true);
                    TopToBottomDiagonalConstraint.Assign(inBoardConstraints.TopToBottomDiagonalConstraint);
                }
                else
                {
                    TopToBottomDiagonalConstraint = null;
                }

                if (inBoardConstraints.mBottomToTopDiagonalConstraint != null)
                {
                    mBottomToTopDiagonalConstraint = new DiagonalConstraint(false);
                    mBottomToTopDiagonalConstraint.Assign(inBoardConstraints.mBottomToTopDiagonalConstraint);
                }
                else
                {
                    mBottomToTopDiagonalConstraint = null;
                }
            }
        }
 public bool Init(Board inBoard)
 {
     bool ok = true;
     FillValuesWithRange();
     for (int i = 0; i < 9 && ok; ++i)
         if (!inBoard.IsAvailable(i, mRowIndex))
             ok = AssignValue(inBoard.Get(i, mRowIndex));
     return ok;
 }
 public bool Write(string inPath,Board inBoard)
 {
     FileStream fsOut = new FileStream(inPath, FileMode.OpenOrCreate,
         FileAccess.Write, FileShare.Write);
     StreamWriter sr = new StreamWriter(fsOut);
     FillDataWithBoard(sr,inBoard);
     sr.Close();
     return true;
 }
 public bool Init(Board inBoard)
 {
     bool ok = true;
     FillValuesWithRange();
     for (int i = 0; i < 3 && ok; ++i)
         for (int j = 0; j < 3 && ok; ++j)
             if (!inBoard.IsAvailable(i + mLeftColumn, j + mTopRow))
                 ok = AssignValue(inBoard.Get(i + mLeftColumn, j + mTopRow));
     return ok;
 }
 private bool FillDataWithBoard(StreamWriter inData,Board inBoard)
 {
     for(int i=0;i<9;++i)
     {
         for(int j=0;j<9;++j)
         {
             inData.Write(inBoard.Get(i,j).ToString());
         }
         inData.Write(inData.NewLine);
     }
     return true;
 }
 private bool FillBoardWithData(StreamReader inData,Board inBoard)
 {
     int number;
     for(int i=0;i<9;++i)
         for(int j=0;j<9;++j)
         {
             number = ReadNextDigit(inData);
             if(number != Board.EMPTY_CELL_VALUE)
                 inBoard.Set(i,j,number);
         }
     return true;
 }
 public bool Read(string inPath,Board inBoard)
 {
     if (!File.Exists(inPath))
     {
         return false;
     }
     FileStream fsIn = new FileStream(inPath, FileMode.Open,
         FileAccess.Read, FileShare.Read);
     StreamReader sr = new StreamReader(fsIn);
     FillBoardWithData(sr,inBoard);
     sr.Close();
     return true;
 }
Exemplo n.º 8
0
        private int SearchSollutionWithBackTracking(
            Board inBoard, 
            BoardConstraints inConstraints,
            GuessesGeneration inGuessesGeneration
        )
        {
            bool foundGoodGuess = false;
            BoardConstraints pendingConstraints = new BoardConstraints();
            Board pendingBoard = new Board();
            List<GuessGroup> guesses;
            int status = 1;

            if(mAbort || (DepthLimit == mDepth))
                return status;
            ++mDepth;
            MaxReachedDepth = (MaxReachedDepth > mDepth ? MaxReachedDepth : mDepth);
            guesses = inGuessesGeneration.GetListOfGuesses(inConstraints);
            foreach (GuessGroup guessGroup in guesses)
            {
                if(mAbort)
                    break;
                foreach(Assignment guess in guessGroup.Guesses)
                {
                    if (mAbort)
                        break;
                    ++TotalTriedGuesses;
                    pendingBoard.Assign(inBoard);
                    pendingConstraints.Assign(inConstraints, pendingBoard);
                    pendingConstraints.AssignValueToCell(guess);
                    if (SearchSollution(pendingBoard, pendingConstraints, inGuessesGeneration) == 0)
                    {
                        foundGoodGuess = true;
                        break;
                    }
                    else
                        ++TotalFailedGuesses;
                }
                if(foundGoodGuess)
                {
                    inBoard.Assign(pendingBoard);
                    inConstraints.Assign(pendingConstraints,inBoard);
                    status = 0;
                    break;
                }
            }
            --mDepth;
            return status;
        }
Exemplo n.º 9
0
        public int Solve(Board inBoard,bool inUseCellGuess, bool inEnforceDiagonalsConstraint)
        {
            BoardConstraints constraints = new BoardConstraints();
            GuessesGeneration guessesGeneration = new GuessesGeneration(inUseCellGuess);
            mAbort = false;
            TotalTriedGuesses = 0;
            TotalFailedGuesses = 0;
            MaxReachedDepth = 0;
            mDepth = 0;

            int status = (constraints.Init(inBoard, inEnforceDiagonalsConstraint) ? 0 : -1);
            if (0 == status)
                status = SearchSollution(inBoard, constraints, guessesGeneration);
            if(mAbort)
                return -2;
            else
                return status;
        }
 public bool Init(Board inBoard)
 {
     bool ok = true;
     FillValuesWithRange();
     if (mTopToBottom)
     {
         for (int i = 0; i < 9 && ok; ++i)
             if (!inBoard.IsAvailable(i,i))
                 ok = AssignValue(inBoard.Get(i,i));
     }
     else
     {
         for (int i = 0; i < 9 && ok; ++i)
             if (!inBoard.IsAvailable(i, 8 - i))
                 ok = AssignValue(inBoard.Get(i, 8 - i));
     }
     return ok;
 }
 public bool Init(
     Board inBoard,
     AssignmentConstraint[] inConstraints,
     int inConstraintsCount
 )
 {
     if (inBoard.IsAvailable(mCellColumn, mCellRow))
     {
         if(inConstraintsCount > 0)
         {
             Assign(inConstraints[0]);
             for (int i = 1; i < inConstraintsCount; ++i)
                 IntersectWith(inConstraints[i]);
         }
         return (0 < mAllowedValues.Count);
     }
     else
         return true;
 }
Exemplo n.º 12
0
        private int SearchSollution(
            Board inBoard,
            BoardConstraints inConstraints,
            GuessesGeneration inGuessesGeneration
        )
        {
            if(inConstraints.HasConflicts())
                return -1;

            inConstraints.ImproveFollowingContraints();
            if(inBoard.IsFull())
                return 0;
            else
            {
                if (inConstraints.HasConflicts())
                    return -1;
                else
                    return SearchSollutionWithBackTracking(inBoard, inConstraints, inGuessesGeneration);
            }
        }
        public bool ImproveSingleNumberAssignment(Board inBoard, BoardConstraints inConstraints)
        {
            if (isFull())
                return false;

            int possibleAssisgnmentsCount = 0;
            List<CellConstraint> cellAssignments = new List<CellConstraint>();
            List<int> numberAssignments = new List<int>();
            CellConstraint aPossibleAssignment = null, aCell;

            foreach (int numberValue in mAllowedValues.Keys)
            {
                possibleAssisgnmentsCount = 0;
                for (int i = 0; i < 9; ++i)
                {
                    aCell = GetCellFromIndex(inConstraints, i);
                    if (inBoard.IsAvailable(aCell.GetColumn(), aCell.GetRow()) &&
                        aCell.CanAssignValue(numberValue))
                        {
                            ++possibleAssisgnmentsCount;
                            aPossibleAssignment = aCell;
                        }
                }
                if (1 == possibleAssisgnmentsCount)
                {
                    cellAssignments.Add(aPossibleAssignment);
                    numberAssignments.Add(numberValue);
                }
            }

            for (int i = 0; i < cellAssignments.Count; ++i)
            {
                inConstraints.AssignValueToCell(
                    cellAssignments[i].GetColumn(),
                    cellAssignments[i].GetRow(),
                    numberAssignments[i]
                );
            }

            return (cellAssignments.Count > 0);
        }
        public bool HasNumbersWithNoPossibleAssignments(Board inBoard, BoardConstraints inConstraints)
        {
            if (isFull())
                return false;

            int possibleAssisgnmentsCount = 0;
            CellConstraint aCell;

            foreach (int numberValue in mAllowedValues.Keys)
            {
                possibleAssisgnmentsCount = 0;
                for (int i = 0; i < 9; ++i)
                {
                    aCell = GetCellFromIndex(inConstraints, i);
                    if (inBoard.IsAvailable(aCell.GetColumn(), aCell.GetRow()) &&
                        aCell.CanAssignValue(numberValue))
                            ++possibleAssisgnmentsCount;
                }
                if (0 == possibleAssisgnmentsCount)
                    break;
            }

            return (0 == possibleAssisgnmentsCount);
        }
Exemplo n.º 15
0
 private void menuItem3_Click(object sender, System.EventArgs e)
 {
     saveFileDialog.Filter = "Soduko files (*.sod)|*.sod";
     saveFileDialog.AddExtension = true;
     if(saveFileDialog.ShowDialog(this) == DialogResult.OK)
     {
         Board board = new Board();
         SudokuSerializer serializer = new SudokuSerializer();
         InitBoard(board);
         serializer.Write(saveFileDialog.FileName,board);
     }
 }
Exemplo n.º 16
0
 void InitBoard(Board inBoard)
 {
     AddNumber(textBox00,inBoard);
     AddNumber(textBox01,inBoard);
     AddNumber(textBox02,inBoard);
     AddNumber(textBox03,inBoard);
     AddNumber(textBox04,inBoard);
     AddNumber(textBox05,inBoard);
     AddNumber(textBox06,inBoard);
     AddNumber(textBox07,inBoard);
     AddNumber(textBox08,inBoard);
     AddNumber(textBox10,inBoard);
     AddNumber(textBox11,inBoard);
     AddNumber(textBox12,inBoard);
     AddNumber(textBox13,inBoard);
     AddNumber(textBox14,inBoard);
     AddNumber(textBox15,inBoard);
     AddNumber(textBox16,inBoard);
     AddNumber(textBox17,inBoard);
     AddNumber(textBox18,inBoard);
     AddNumber(textBox20,inBoard);
     AddNumber(textBox21,inBoard);
     AddNumber(textBox22,inBoard);
     AddNumber(textBox23,inBoard);
     AddNumber(textBox24,inBoard);
     AddNumber(textBox25,inBoard);
     AddNumber(textBox26,inBoard);
     AddNumber(textBox27,inBoard);
     AddNumber(textBox28,inBoard);
     AddNumber(textBox30,inBoard);
     AddNumber(textBox31,inBoard);
     AddNumber(textBox32,inBoard);
     AddNumber(textBox33,inBoard);
     AddNumber(textBox34,inBoard);
     AddNumber(textBox35,inBoard);
     AddNumber(textBox36,inBoard);
     AddNumber(textBox37,inBoard);
     AddNumber(textBox38,inBoard);
     AddNumber(textBox40,inBoard);
     AddNumber(textBox41,inBoard);
     AddNumber(textBox42,inBoard);
     AddNumber(textBox43,inBoard);
     AddNumber(textBox44,inBoard);
     AddNumber(textBox45,inBoard);
     AddNumber(textBox46,inBoard);
     AddNumber(textBox47,inBoard);
     AddNumber(textBox48,inBoard);
     AddNumber(textBox50,inBoard);
     AddNumber(textBox51,inBoard);
     AddNumber(textBox52,inBoard);
     AddNumber(textBox53,inBoard);
     AddNumber(textBox54,inBoard);
     AddNumber(textBox55,inBoard);
     AddNumber(textBox56,inBoard);
     AddNumber(textBox57,inBoard);
     AddNumber(textBox58,inBoard);
     AddNumber(textBox60,inBoard);
     AddNumber(textBox61,inBoard);
     AddNumber(textBox62,inBoard);
     AddNumber(textBox63,inBoard);
     AddNumber(textBox64,inBoard);
     AddNumber(textBox65,inBoard);
     AddNumber(textBox66,inBoard);
     AddNumber(textBox67,inBoard);
     AddNumber(textBox68,inBoard);
     AddNumber(textBox70,inBoard);
     AddNumber(textBox71,inBoard);
     AddNumber(textBox72,inBoard);
     AddNumber(textBox73,inBoard);
     AddNumber(textBox74,inBoard);
     AddNumber(textBox75,inBoard);
     AddNumber(textBox76,inBoard);
     AddNumber(textBox77,inBoard);
     AddNumber(textBox78,inBoard);
     AddNumber(textBox80,inBoard);
     AddNumber(textBox81,inBoard);
     AddNumber(textBox82,inBoard);
     AddNumber(textBox83,inBoard);
     AddNumber(textBox84,inBoard);
     AddNumber(textBox85,inBoard);
     AddNumber(textBox86,inBoard);
     AddNumber(textBox87,inBoard);
     AddNumber(textBox88,inBoard);
 }
        public bool Init(Board inBoard, bool inEnforceDiagonalsConstraint)
        {
            bool statusOK;

            mBoard = inBoard;
            do
            {
                statusOK = SetupDiagonalConstraints(inEnforceDiagonalsConstraint);
                if (!statusOK)
                    break;

                statusOK = SetupGroupConstraints();
                if (!statusOK)
                    break;

                statusOK = SetupCellConstraints(inEnforceDiagonalsConstraint);
                if (!statusOK)
                    break;

                statusOK = !HasConflicts();
            } while (false);
            return statusOK;
        }
Exemplo n.º 18
0
 private void button1_Click(object sender, System.EventArgs e)
 {
     Board board = new Board();
     ShowBoard(board);
 }
Exemplo n.º 19
0
 void AddNumber(TextBox inTextBox,Board inBoard)
 {
     if(inTextBox.Text.Length == 1)
     {
         try
         {
             int tagNumber = Int32.Parse((string)(inTextBox.Tag));
             inBoard.Set(tagNumber / 9, tagNumber % 9,Int32.Parse(inTextBox.Text));
         }
         catch(Exception)
         {
         }
     }
 }
Exemplo n.º 20
0
 private void WorkMethod()
 {
     Board board = new Board();
     InitBoard(board);
     mSudokuGame.DepthLimit = Int32.Parse(textBox1.Text);
     int didSolve = mSudokuGame.Solve(board, !checkBox1.Checked, chkDiagonals.Checked);
     ShowResult(
         didSolve,
         board,
         mSudokuGame.TotalTriedGuesses,
         mSudokuGame.MaxReachedDepth,
         mSudokuGame.TotalFailedGuesses
     );
 }
Exemplo n.º 21
0
 private void ShowResult(int inDidSolve,Board inBoard,int inTotalGuesses,int inMaximumDepth,int inFailedGuesses)
 {
     switch(inDidSolve)
     {
         case -1:
             MessageBox.Show("Puzzle Contains Conflicts");
             break;
         case 0:
             MessageBox.Show(
                 string.Format(
                 "Puzzle Solved.\ntotal guesses tried : {0}\nmaximum guesses depth reached : {1}\nfailed guesses : {2}",
                 inTotalGuesses,inMaximumDepth,inFailedGuesses
                 )
                 );
             controlInvoker.Invoke(new MethodCallInvoker(ShowBoard), inBoard);
      					break;
         case 1:
             MessageBox.Show("Not Succesfull in Solving Puzzle. try a larger depth maxim");
             controlInvoker.Invoke(new MethodCallInvoker(ShowBoard), inBoard);
             break;
         case -2:
             MessageBox.Show("Aborted");
             break;
     }
 }
Exemplo n.º 22
0
 void ShowNumber(TextBox inTextBox,Board inBoard)
 {
     int tagNumber = Int32.Parse((string)(inTextBox.Tag));
     int result = inBoard.Get(tagNumber / 9, tagNumber % 9);
     if(result != Board.EMPTY_CELL_VALUE)
         inTextBox.Text = result.ToString();
     else
         inTextBox.Text = String.Empty;
 }
Exemplo n.º 23
0
 public void Assign(Board inBoard)
 {
     for(int i=0;i<9;++i)
         for(int j=0;j<9;++j)
             mCells[i,j] = inBoard.mCells[i,j];
 }
Exemplo n.º 24
0
 private void ShowBoardFromFile(string inFilePath)
 {
     Board board = new Board();
     SudokuSerializer serializer = new SudokuSerializer();
     serializer.Read(inFilePath,board);
     ShowBoard(board);
 }