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.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. isFull()
  25. {
  26. return this.#colors.length == this.#maxLevels;
  27. }
  28. isEmpty()
  29. {
  30. return this.#colors.length == 0;
  31. }
  32. emptySpaceSize()
  33. {
  34. return this.#maxLevels - this.#colors.length;
  35. }
  36. getColorAtLevel(index)
  37. {
  38. if (index < 0 || index >= this.#colors.length) return '';
  39. return this.#colors[index].toString();
  40. }
  41. peekTopColor()
  42. {
  43. let length = this.#colors.length;
  44. return this.getColorAtLevel(length-1);
  45. }
  46. isComplete()
  47. {
  48. if (this.emptySpaceSize() != 1) return false;
  49. let reference = this.#colors[0];
  50. for (let i = 1; i < this.#colors.length; i++)
  51. {
  52. if (this.#colors[i] != reference)
  53. {
  54. return false;
  55. }
  56. }
  57. return true;
  58. }
  59. }