예제 #1
0
 public void GivenAnExampleEulerBoard()
 {
     var input = new string[]
                     {
                         "..3.2.6..",
                         "9..3.5..1",
                         "..18.64..",
                         "..81.29..",
                         "7.......8",
                         "..67.82..",
                         "..26.95..",
                         "8..2.3..9",
                         "..5.1.3..",
                     };
     _board = FromStringArray(input);
 }
예제 #2
0
        public void GivenADifficultBoard()
        {
            var input = new string[]
                            {
                                "...5...6.",
                                "8.9....1.",
                                "16..87...",
                                "3...26...",
                                "..7.1.6..",
                                "...85...3",
                                "...47..21",
                                ".4....9.8",
                                ".8...3..."
                            };

            _board = FromStringArray(input);
        }
예제 #3
0
        /// <summary>
        /// Checks if the move is valid. A move is NOT valid if:
        /// * the square is read only
        /// * the row already contains the number
        /// * the column already contains the number
        /// * the 3x3 group already contains the number
        /// </summary>
        /// <returns>Returns true if it is a valid move.</returns>
        private bool IsAValidMove(Board board, int row, int col, int number)
        {
            //Not valid move if readonly
            if (board.Squares[row, col].ReadOnly)
                return false;
            //Not valid move if row contains number
            for (var i = 0; i < board.Size; i++)
            {
                if (i != col && board.Squares[row, i].Number == number)
                {
                    return false;
                }
            }
            //Not valid move if col contains number
            for (var i = 0; i < board.Size; i++)
            {
                if (i != row && board.Squares[i, col].Number == number)
                {
                    return false;
                }
            }
            //Not valid move if group contains number
            var box = GetBox(row, col);
            var rows = 0;
            var cols = 0;
            if (box >= 3 && box < 6)
                rows = 3;
            else if (box >= 6 && box < 9)
                rows = 6;
            if (box == 1 || box == 4 || box == 7)
                cols = 3;
            else if (box == 2 || box == 5 || box == 8)
                cols = 6;

            for (var i = rows; i < rows + 3; i++)
            {
                for (var j = cols; j < cols + 3; j++)
                {
                    if (board.Squares[i, j].Number == number)
                        return false;
                }
            }

            return true;
        }
예제 #4
0
 /// <summary>
 /// Finds an emtpy square of the board, of any.
 /// </summary>
 /// <param name="board">The board.</param>
 /// <param name="row">Output parameter row of the empty square. -1 if no empty square left.</param>
 /// <param name="col">Output parameter col of the empty square. -1 if no empty square left.</param>
 private void FindEmptySquare(Board board, out int row, out int col)
 {
     row = -1;
     col = -1;
     for (int i = 0; i < board.Size; i++)
     {
         if (row > -1)
             break;
         for (int j = 0; j < board.Size; j++)
         {
             if (board.Squares[i, j].Number == 0)
             {
                 row = i;
                 col = j;
                 break;
             }
         }
     }
 }
예제 #5
0
        /// <summary>
        /// Solves the sudoku.
        /// </summary>
        /// <returns>Returns true if the game is solvable.</returns>
        public bool Solve(Board board)
        {
            int row;
            int col;
            FindEmptySquare(board, out row, out col);
            if (row < 0 && col < 0)
                return true;

            for (int i = 1; i < 10; i++)
            {
                if (Move(board, row, col, i))
                {
                    if (Solve(board))
                        return true;

                    // Can't find a valid number, reset number to zero and continue.
                    board.Squares[row, col].SetNumber(0);
                }
            }

            return false;
        }
예제 #6
0
        /// <summary>
        /// Makes a move on given board.
        /// </summary>
        /// <returns>Returns true if the move was made. Returns false if the move
        /// was not made (i.e. invalid move).</returns>
        public bool Move(Board board, int row, int col, int number)
        {
            // Check if index is outside board
            if (row < 0 || row >= board.Size || col < 0 || col >= board.Size)
            {
                return false;
            }
            // Check if valid move
            if (!IsAValidMove(board, row, col, number))
                return false;

            board.Squares[row, col].SetNumber(number);
            return true;
        }
예제 #7
0
 public void SetUp()
 {
     _board = new Board(Content);
 }
예제 #8
0
 public void Board_should_have_medium_classification()
 {
     var mediumBoard = new Board(MediumContent);
     Assert.AreEqual(Classification.Medium, mediumBoard.Classification);
 }
예제 #9
0
 public void Board_should_have_easy_classification()
 {
     var easyBoard = new Board(EasyContent);
     Assert.AreEqual(Classification.Easy, easyBoard.Classification);
 }
예제 #10
0
 public void SetUp()
 {
     var content = "510000083800416005000000000098504610000901000064203570000000000600157004780000096";
     _board = new Board(content);
     _gameEngine = new GameEngine();
 }
예제 #11
0
 public void GivenThisBoard(string input)
 {
     _board = ParseInput(input);
 }
예제 #12
0
 public void GivenAnEmptyBoard()
 {
     _board = Board.Empty;
 }
예제 #13
0
 public void WhenDigitPlacedAtRowCol(int digit, int row, int col)
 {
     //TODO: implement act (action) logic
     _board = _board.Play(digit, row, col);
 }
예제 #14
0
        public void TheSolverMakesTheRequiredMoves()
        {
            _solver = _solver.MakeRequiredMoves();
            _board = _solver.Board;

            Debug.WriteLine("Board after required moves made:\n" + Printer.Print(_board));
        }