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.

Board.cs 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using uqac_ia_sudoku_csp.Interfaces;
  6. namespace uqac_ia_sudoku_csp
  7. {
  8. public class Board
  9. {
  10. public string Characters { get; }
  11. public int Size => Game.Length;
  12. protected int?[][] Game { get; set; }
  13. public Board(string characters)
  14. {
  15. Characters = characters;
  16. InitGame(characters.Length);
  17. Init((x, y) => null);
  18. }
  19. public Board(string characters, Func<int, int, int?> cb)
  20. {
  21. Characters = characters;
  22. InitGame(characters.Length);
  23. Init(cb);
  24. }
  25. public Board(Board other)
  26. {
  27. Characters = other.Characters;
  28. InitGame(other.Size);
  29. Init(other.GetNumber);
  30. }
  31. public Board Clone()
  32. {
  33. return new Board(this);
  34. }
  35. protected void InitGame(int size)
  36. {
  37. Game = new int?[size][];
  38. for (var y = 0; y < size; ++y)
  39. {
  40. Game[y] = new int?[size];
  41. }
  42. }
  43. public void Init(Func<int, int, int?> cb)
  44. {
  45. for (var y = 0; y < Size; ++y)
  46. {
  47. for (var x = 0; x < Size; ++x)
  48. {
  49. SetNumber(x, y, cb(x, y));
  50. }
  51. }
  52. }
  53. public char? GetCharacter(int? v)
  54. {
  55. return v == null ? null : (char?)Characters[v.Value];
  56. }
  57. public char? GetCharacter(int x, int y)
  58. {
  59. return GetCharacter(GetNumber(x, y));
  60. }
  61. public void SetCharacter(int x, int y, char? c)
  62. {
  63. SetNumber(x, y, c == null ? null : (char?)Characters.IndexOf(c.Value));
  64. }
  65. public int? GetNumber(int x, int y)
  66. {
  67. return Game[y][x];
  68. }
  69. public void SetNumber(int x, int y, int? value)
  70. {
  71. Game[y][x] = value;
  72. }
  73. public void ClearNumber(int x, int y)
  74. {
  75. SetNumber(x, y, null);
  76. }
  77. public bool IsComplete()
  78. {
  79. return !Game.Any(line => line.Any(cell => cell == null));
  80. }
  81. public bool IsConsistent(int x, int y, IList<IConstraint> constraints)
  82. {
  83. return constraints.All(constraint => constraint.Check(this, x, y));
  84. }
  85. public void Print(TextWriter stream)
  86. {
  87. stream.WriteLine(Characters);
  88. stream.WriteLine(new string('-', Size + 2));
  89. foreach (var line in Game)
  90. {
  91. stream.Write('|');
  92. foreach (var cell in line)
  93. {
  94. var c = GetCharacter(cell);
  95. stream.Write(c ?? ' ');
  96. }
  97. stream.WriteLine('|');
  98. }
  99. stream.WriteLine(new string('-', Size + 2));
  100. }
  101. }
  102. }