C'est le jeu des tubes de couleur dans les pubs la
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.

tube.js 1.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. class Tube {
  2. #maxLevels;
  3. #colors;
  4. constructor(levels)
  5. {
  6. this.#maxLevels = levels;
  7. this.#colors = [];
  8. }
  9. addColorLayer(color)
  10. {
  11. if (this.isFull()) return false;
  12. this.#colors.push(color);
  13. return true;
  14. }
  15. removeColorLayer()
  16. {
  17. if (this.isEmpty()) return null;
  18. return this.#colors.pop();
  19. }
  20. drain()
  21. {
  22. this.#colors = [];
  23. }
  24. height()
  25. {
  26. return this.#maxLevels;
  27. }
  28. isFull()
  29. {
  30. return this.#colors.length == this.#maxLevels;
  31. }
  32. isEmpty()
  33. {
  34. return this.#colors.length == 0;
  35. }
  36. remainingSpace()
  37. {
  38. return this.#maxLevels - this.#colors.length;
  39. }
  40. getColorAtLevel(index)
  41. {
  42. if (index < 0 || index >= this.#colors.length) return '';
  43. return this.#colors[index].toString();
  44. }
  45. peekTopColor()
  46. {
  47. let length = this.#colors.length;
  48. return this.getColorAtLevel(length-1);
  49. }
  50. isComplete()
  51. {
  52. if (this.remainingSpace() != 1) return false;
  53. let reference = this.#colors[0];
  54. for (let i = 1; i < this.#colors.length; i++)
  55. {
  56. if (this.#colors[i] != reference)
  57. {
  58. return false;
  59. }
  60. }
  61. return true;
  62. }
  63. }