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 790B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. class Tube {
  2. #maxLevels;
  3. #colors;
  4. constructor(levels)
  5. {
  6. this.#maxLevels = levels;
  7. this.#colors = [];
  8. }
  9. addColor(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. }