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

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