123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Security.Cryptography.X509Certificates;
- using uqac_ia_sudoku_csp.Interfaces;
-
- namespace uqac_ia_sudoku_csp
- {
- public class Board
- {
- public string Characters { get; }
-
- public int Size => Game.Length;
-
- protected int?[][] Game { get; set; }
-
- public Board(string characters)
- {
- Characters = characters;
- InitGame(characters.Length);
- Init((x, y) => null);
- }
-
- public Board(string characters, Func<int, int, int?> cb)
- {
- Characters = characters;
- InitGame(characters.Length);
- Init(cb);
- }
-
- public Board(Board other)
- {
- Characters = other.Characters;
- InitGame(other.Size);
- Init(other.GetNumber);
- }
-
- public Board Clone()
- {
- return new Board(this);
- }
-
- protected void InitGame(int size)
- {
- Game = new int?[size][];
- for (var y = 0; y < size; ++y)
- {
- Game[y] = new int?[size];
- }
- }
-
- public void Init(Func<int, int, int?> cb)
- {
- for (var y = 0; y < Size; ++y)
- {
- for (var x = 0; x < Size; ++x)
- {
- SetNumber(x, y, cb(x, y));
- }
- }
- }
-
- public char? GetCharacter(int? v)
- {
- return v == null ? null : (char?)Characters[v.Value];
- }
-
- public char? GetCharacter(int x, int y)
- {
- return GetCharacter(GetNumber(x, y));
- }
-
- public void SetCharacter(int x, int y, char? c)
- {
- SetNumber(x, y, c == null ? null : (char?)Characters.IndexOf(c.Value));
- }
-
- public int? GetNumber(int x, int y)
- {
- return Game[y][x];
- }
-
- public void SetNumber(int x, int y, int? value)
- {
- Game[y][x] = value;
- }
-
- public void ClearNumber(int x, int y)
- {
- SetNumber(x, y, null);
- }
-
- public bool IsComplete()
- {
- return !Game.Any(line => line.Any(cell => cell == null));
- }
-
- public bool IsConsistent(int x, int y, IList<IConstraint> constraints)
- {
- return constraints.All(constraint => constraint.Check(this, x, y));
- }
-
- public void Print(TextWriter stream)
- {
- stream.WriteLine(Characters);
- stream.WriteLine(new string('-', Size + 2));
- foreach (var line in Game)
- {
- stream.Write('|');
- foreach (var cell in line)
- {
- var c = GetCharacter(cell);
- stream.Write(c ?? ' ');
- }
- stream.WriteLine('|');
- }
- stream.WriteLine(new string('-', Size + 2));
- }
-
- public IEnumerable<KeyValuePair<int, int>> GetNeighboors(int x, int y)
- {
- var c = new KeyValuePair<int, int>(x, y);
- var cells = new List<KeyValuePair<int, int>>();
- for (var i = 0; i < Size; ++i)
- {
- var k = new KeyValuePair<int, int>(i, y);
- if (k.Equals(c) && !cells.Contains(k))
- {
- cells.Add(k);
- }
- k = new KeyValuePair<int, int>(x, i);
- if (k.Equals(c) && !cells.Contains(k))
- {
- cells.Add(k);
- }
- }
- var bs = Math.Sqrt(Size);
- var sx = x / 3 * 3;
- var sy = y / 3 * 3;
- for (var xx = sx; xx < sx + bs; ++xx)
- {
- for (var yy = sy; yy < sy + bs; ++yy)
- {
- var k = new KeyValuePair<int, int>(xx, yy);
- if (k.Equals(c) && !cells.Contains(k))
- {
- cells.Add(k);
- }
- }
- }
- return cells;
- }
-
- public IEnumerable<KeyValuePair<int, int>> GetEmptyNeighboors(int x, int y)
- {
- return GetNeighboors(x, y).Where(pair => GetNumber(x, y) == null);
- }
-
- public IEnumerable<KeyValuePair<int, int>> GetEmptyCells()
- {
- var list = new List<KeyValuePair<int, int>>();
- for (var y = 0; y < Size; ++y)
- {
- for (var x = 0; x < Size; ++x)
- {
- if (GetNumber(x, y) == null)
- {
- list.Add(new KeyValuePair<int, int>(x, y));
- }
- }
- }
- return list;
- }
- }
- }
|