/SuperMemory/Order/FieldMatrix.cs
C# | 71 lines | 50 code | 10 blank | 11 comment | 16 complexity | a4b77870de1358f542c875b9ab78def8 MD5 | raw file
- using System.Collections.Generic;
- using System.Diagnostics.Contracts;
- using System.Linq;
- using Smart.Classes.Collections;
- using Smart.Classes.Extensions;
- using Smart.Classes.Randomization;
-
- namespace SuperMemory.Order
- {
- /// <summary>
- /// Class that represents items added to the memorizing field
- /// </summary>
- public class FieldMatrix : SquareCollection<Num2D>
- {
- public int IconsLeft { get; set; }
-
- public FieldMatrix(int xCount, int yCount)
- : base(xCount, yCount, () => Num2D.Emtpy)
- {
- }
-
- /// <summary>
- /// Fills matrix with random values needed for memorization
- /// </summary>
- /// <returns></returns>
- public void GenerateMatrix<T>(SmartCollection2D<T> images, int icons)
- {
- Contract.Requires(ColsNum*RowsNum > icons && ColsNum > 1 && RowsNum > 1);
- GenerateMatrix(images, icons, this.ColsNum, this.RowsNum);
- }
-
-
-
- public void ClearAll(int cols, int rows)
- {
- Contract.Ensures(rows == RowsNum && cols == ColsNum);
- this.ClearAll();
- this.IconsLeft = 0;
- this.ColsNum = cols;
- this.RowsNum = rows;
- }
-
- /// <summary>
- /// Fills matrix with random values needed for memorization
- /// </summary>
- /// <returns></returns>
- public void GenerateMatrix<T>(SmartCollection2D<T> images, int icons, int cols, int rows)
- {
- Contract.Requires(icons <= cols*rows);
- Contract.Ensures(
- this.Flatten(n => Equals(n, Num2D.Emtpy) == false).Count() == icons
- && rows == RowsNum
- && cols == ColsNum
- && IconsLeft == icons);
- Contract.Ensures(icons == IconsLeft);
- ClearAll(cols, rows);
- var indexes = images.GetRandomIndexesUnique(icons);
- Contract.Assert(indexes.Count == icons);
- var def = CreateDefaultFunc();
- foreach (var index in indexes) this.SetRandom2D(index, i => Equals(i, def));
- this.IconsLeft = icons;
- }
-
- public IEnumerable<KeyValuePair<Num2D,Num2D>> NonEmptyIndexed()
- {
- return this.FlattenIndexed(kv => Equals(kv.Value, Num2D.Emtpy) == false);
- }
-
- }
-
- }