PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/SuperMemory/Order/FieldMatrix.cs

https://github.com/antonkulaga/SuperMemory
C# | 71 lines | 50 code | 10 blank | 11 comment | 16 complexity | a4b77870de1358f542c875b9ab78def8 MD5 | raw file
  1. using System.Collections.Generic;
  2. using System.Diagnostics.Contracts;
  3. using System.Linq;
  4. using Smart.Classes.Collections;
  5. using Smart.Classes.Extensions;
  6. using Smart.Classes.Randomization;
  7. namespace SuperMemory.Order
  8. {
  9. /// <summary>
  10. /// Class that represents items added to the memorizing field
  11. /// </summary>
  12. public class FieldMatrix : SquareCollection<Num2D>
  13. {
  14. public int IconsLeft { get; set; }
  15. public FieldMatrix(int xCount, int yCount)
  16. : base(xCount, yCount, () => Num2D.Emtpy)
  17. {
  18. }
  19. /// <summary>
  20. /// Fills matrix with random values needed for memorization
  21. /// </summary>
  22. /// <returns></returns>
  23. public void GenerateMatrix<T>(SmartCollection2D<T> images, int icons)
  24. {
  25. Contract.Requires(ColsNum*RowsNum > icons && ColsNum > 1 && RowsNum > 1);
  26. GenerateMatrix(images, icons, this.ColsNum, this.RowsNum);
  27. }
  28. public void ClearAll(int cols, int rows)
  29. {
  30. Contract.Ensures(rows == RowsNum && cols == ColsNum);
  31. this.ClearAll();
  32. this.IconsLeft = 0;
  33. this.ColsNum = cols;
  34. this.RowsNum = rows;
  35. }
  36. /// <summary>
  37. /// Fills matrix with random values needed for memorization
  38. /// </summary>
  39. /// <returns></returns>
  40. public void GenerateMatrix<T>(SmartCollection2D<T> images, int icons, int cols, int rows)
  41. {
  42. Contract.Requires(icons <= cols*rows);
  43. Contract.Ensures(
  44. this.Flatten(n => Equals(n, Num2D.Emtpy) == false).Count() == icons
  45. && rows == RowsNum
  46. && cols == ColsNum
  47. && IconsLeft == icons);
  48. Contract.Ensures(icons == IconsLeft);
  49. ClearAll(cols, rows);
  50. var indexes = images.GetRandomIndexesUnique(icons);
  51. Contract.Assert(indexes.Count == icons);
  52. var def = CreateDefaultFunc();
  53. foreach (var index in indexes) this.SetRandom2D(index, i => Equals(i, def));
  54. this.IconsLeft = icons;
  55. }
  56. public IEnumerable<KeyValuePair<Num2D,Num2D>> NonEmptyIndexed()
  57. {
  58. return this.FlattenIndexed(kv => Equals(kv.Value, Num2D.Emtpy) == false);
  59. }
  60. }
  61. }