internal Potential(Square square, Group group, Number number,PotentialTypes potentialType) { this.Square = square; this.SquareGroup = group; this.Number = number; this.PotentialType = potentialType; }
internal new int Add(Square value) { for (int i = 0; i < _Numbers.Length; i++) { //this.Numbers[i].AvailableSquares.Add(value); this.Numbers[i].AvailableSquares.Add(value); } return List.Add(value); }
internal void SetNumberAvailableSquares(Square square, int iNumber, bool isAvailable) { if (isAvailable) { if (square.IsNumberUsedInRelatedGroups(iNumber + 1)) //If the number is used in another group, don't make it available... return; //if (!this.Numbers[iNumber].AvailableSquares.Contains(square)) // this.Numbers[iNumber].AvailableSquares.Add(square); if (!this.Numbers[iNumber].AvailableSquares.Contains(square)) this.Numbers[iNumber].AvailableSquares.Add(square); } else { //if (this.Numbers[iNumber].AvailableSquares.Contains(square)) // this.Numbers[iNumber].AvailableSquares.Remove(square); if (this.Numbers[iNumber].AvailableSquares.Contains(square)) this.Numbers[iNumber].AvailableSquares.Remove(square); } }
public SquareContainer(Square square) { this.Square = square; }
void Square_NumberChanging(Square square) { foreach (var relatedSquare in Squares) relatedSquare.MakeNumberAvailable(square.Number); }
void Square_NumberChanged(Square square) { foreach (var relatedSquare in Squares) relatedSquare.MakeNumberUnavailable(square.Number); //Check for potential square foreach (var number in RelatedNumbers) CheckPotentialSquare(number); }
internal void AddSquare(Square square) { square.NumberChanging += new Square.SquareEventHandler(Square_NumberChanging); square.NumberChanged += new Square.SquareEventHandler(Square_NumberChanged); square.NumberBecameUnavailable += new Square.NumberBecameUnavailableEventHandler(Square_NumberBecameUnavailable); _Squares.Add(square); }
static void Sudoku_SquareNumberChanged(Square square) { Console.WriteLine(string.Format(" Id {0}: {1} - {2}", square.Id.ToString("D2"), square.Number.ToString(), square.FillType.ToString())); }
public void Remove(Square value) { List.Remove(value); }
public bool Contains(Square value) { return List.Contains(value); }
public int Add(Square value) { return List.Add(value); }
internal void DeassignSquare(Square square) { if (_AssignedSquares.Contains(square)) _AssignedSquares.Remove(square); }
internal void AssignSquare(Square square) { if (!_AssignedSquares.Contains(square)) _AssignedSquares.Add(square); }
/// <summary> /// Validate /// </summary> /// <param name="square"></param> /// <returns></returns> internal static bool ValidatePotential(Square square) { return square.IsAvailable && square.AvailableNumbers.Count().Equals(1); }
/// <summary> /// Handles number changed event of the square; raises an event and makes the number of the square unavailable in the related squares /// </summary> /// <param name="square"></param> void Square_NumberChanged(Square square) { if (SquareNumberChanged != null) SquareNumberChanged(square); }
void Init(int size) { //Id //this.Id = Guid.NewGuid(); //Size this.Size = size; //All numbers; numbers will be created as the size of the sudoku (default 9 + zero value = 10) _AllNumbers = new List<Number>(Size + 1); //Zero first var zeroNumber = new Number(this, 0); _AllNumbers.Add(zeroNumber); //Other numbers for (int i = 1; i <= Size; i++) { var number = new Number(this, i); _AllNumbers.Add(number); } //All square groups _AllHorizontalTypeGroups = new List<Group>(Size); _AllVerticalTypeGroups = new List<Group>(Size); _AllSquareTypeGroups = new List<Group>(Size); for (int i = 1; i <= Size; i++) { //Generate the groups var horizontalGroup = new Group(i, GroupTypes.Horizontal, this); var verticalGroup = new Group(i, GroupTypes.Vertical, this); var squareGroup = new Group(i, GroupTypes.Square, this); //Register the events horizontalGroup.PotentialFound += new Potential.FoundEventHandler(PotentialSquare_Found); verticalGroup.PotentialFound += new Potential.FoundEventHandler(PotentialSquare_Found); squareGroup.PotentialFound += new Potential.FoundEventHandler(PotentialSquare_Found); //Add to the "all" lists _AllHorizontalTypeGroups.Add(horizontalGroup); _AllVerticalTypeGroups.Add(verticalGroup); _AllSquareTypeGroups.Add(squareGroup); } //All squares _AllSquares = new List<Square>(TotalSize); for (int i = 0; i < TotalSize; i++) { //Find the indexes of the current square's groups int horizontalGroupIndex = i / Size; int verticalGroupIndex = (i + 1) % Size == 0 ? Size - 1 : ((i + 1) % Size) - 1; int squareGroupIndex = (verticalGroupIndex / SquareRootOfSize) + ((horizontalGroupIndex / SquareRootOfSize) * SquareRootOfSize); //Get the groups var currentSquareHorizontalGroup = _AllHorizontalTypeGroups[horizontalGroupIndex]; var currentSquareVerticalGroup = _AllVerticalTypeGroups[verticalGroupIndex]; var currentSquareSquareGroup = _AllSquareTypeGroups[squareGroupIndex]; //Generate the squares Square square = new Square(i + 1, zeroNumber, this, currentSquareHorizontalGroup, currentSquareVerticalGroup, currentSquareSquareGroup); square.NumberChanged += new Square.SquareEventHandler(Square_NumberChanged); square.PotentialSquareFound += new Potential.FoundEventHandler(PotentialSquare_Found); _AllSquares.Add(square); //Add the squares to their own groups currentSquareHorizontalGroup.AddSquare(square); currentSquareVerticalGroup.AddSquare(square); currentSquareSquareGroup.AddSquare(square); } //Potential squares _PotentialSquares = new List<Potential>(); }
void FillSquare(Square square, Number number, FillTypes type) { //Validate first if (square == null) throw new Exception("Not a valid square id"); if (number == null) throw new Exception("Not a valid number"); //Set the number and type square.Fill(number, type); //Solve? if (AutoSolve) Solve(); }