You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BlockConstraint.cs 754B

123456789101112131415161718192021222324252627
  1. using System;
  2. using uqac_ia_sudoku_csp.Interfaces;
  3. namespace uqac_ia_sudoku_csp.Solver.Constraints
  4. {
  5. public class BlockConstraint : IConstraint
  6. {
  7. public bool Check(Board board, int x, int y)
  8. {
  9. var bs = Math.Sqrt(board.Size);
  10. var sx = x / 3 * 3;
  11. var sy = y / 3 * 3;
  12. var cell = board.GetNumber(x, y).Value;
  13. for (var xx = sx; xx < sx + bs; ++xx)
  14. {
  15. for (var yy = sy; yy < sy + bs; ++yy)
  16. {
  17. if (cell == board.GetNumber(xx, yy) && (xx != x || yy != y))
  18. {
  19. return false;
  20. }
  21. }
  22. }
  23. return true;
  24. }
  25. }
  26. }