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.

game.js 1.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. class Game {
  2. #tubes;
  3. #selectedTube;
  4. constructor(nbTubes, tubeLevel)
  5. {
  6. this.#tubes = [];
  7. this.#selectedTube = null;
  8. this.isCompleted = false;
  9. for (let i = 0; i < nbTubes; i++)
  10. {
  11. this.#tubes.push(new Tube(tubeLevel));
  12. }
  13. }
  14. tubeAt(tubeIndex)
  15. {
  16. if (!this.isInRange(tubeIndex)) return null;
  17. return this.#tubes[tubeIndex];
  18. }
  19. selectedTube()
  20. {
  21. return this.#selectedTube;
  22. }
  23. selectTubeAt(index)
  24. {
  25. if (this.isInRange(index))
  26. {
  27. this.#selectedTube = this.#tubes[index];
  28. }
  29. }
  30. removeTube(tube)
  31. {
  32. let tubeIndex = this.#tubes.indexOf(tube);
  33. if (tubeIndex != -1)
  34. {
  35. let removed = this.#tubes.splice(tubeIndex, 1)[0];
  36. if (removed = this.#selectedTube) this.unselectTube();
  37. }
  38. }
  39. clean()
  40. {
  41. this.#tubes = []
  42. this.unselectTube();
  43. }
  44. unselectTube()
  45. {
  46. this.#selectedTube = null;
  47. }
  48. tubesNumber()
  49. {
  50. return this.#tubes.length;
  51. }
  52. isInRange(index)
  53. {
  54. return index >= 0 && index < this.#tubes.length;
  55. }
  56. isComplete()
  57. {
  58. return this.#tubes.every(
  59. tube => tube.isComplete() || tube.isEmpty()
  60. );
  61. }
  62. }