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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. class Game {
  2. #tubes;
  3. #selectedTube;
  4. constructor(nbTubes, tubeLevel)
  5. {
  6. this.#tubes = [];
  7. this.#selectedTube = null;
  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. selectedTube()
  19. {
  20. return this.#selectedTube;
  21. }
  22. selectTubeAt(index)
  23. {
  24. if (this.isInRange(index))
  25. {
  26. this.#selectedTube = this.#tubes[index];
  27. }
  28. }
  29. removeTube(tube)
  30. {
  31. let tubeIndex = this.#tubes.indexOf(tube);
  32. if (tubeIndex != -1)
  33. {
  34. let removed = this.#tubes.splice(tubeIndex, 1)[0];
  35. if (removed = this.#selectedTube) this.unselectTube();
  36. }
  37. }
  38. clean()
  39. {
  40. this.#tubes = []
  41. this.unselectTube();
  42. }
  43. unselectTube()
  44. {
  45. this.#selectedTube = null;
  46. }
  47. tubesNumber()
  48. {
  49. return this.#tubes.length;
  50. }
  51. isInRange(index)
  52. {
  53. return index >= 0 && index < this.#tubes.length;
  54. }
  55. isComplete()
  56. {
  57. return this.#tubes.every(
  58. tube => tube.isComplete() || tube.isEmpty()
  59. );
  60. }
  61. }