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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. class Game {
  2. #colorsBank = ['black','silver','gray','maroon','red','purple','fuchsia','green','lime','olive','yellow','navy','blue','teal','aqua'];
  3. constructor(nbTubes, tubeLevel)
  4. {
  5. this.tubes = [];
  6. this.selectedTube = null;
  7. for (let i = 0; i < nbTubes; i++)
  8. {
  9. this.tubes.push(new Tube(tubeLevel));
  10. }
  11. this.initializeColorTubes(tubeLevel);
  12. }
  13. /* The goal is to have each tube filled with its own color. Thus, there
  14. is (tubes minus one) different colors divided in (tubeLevel) layers
  15. mixed in the tubes.
  16. One tube is empty to allow movement.
  17. */
  18. initializeColorTubes(tubeLevel)
  19. {
  20. let nbColors = this.tubes.length - 1;
  21. let allColors = [];
  22. for (let coloridx = 0; coloridx < nbColors; coloridx++)
  23. {
  24. for (let times = 0; times < tubeLevel; times++)
  25. {
  26. allColors.push(this.#colorsBank[coloridx]);
  27. }
  28. }
  29. for (let i = allColors.length - 1; i > 0; i--) {
  30. let j = Math.floor(Math.random() * (i + 1));
  31. let temp = allColors[i];
  32. allColors[i] = allColors[j];
  33. allColors[j] = temp;
  34. }
  35. for (let index = 0; index < allColors.length; index++)
  36. {
  37. this.tubes[Math.floor(index/tubeLevel)].addColor(allColors[index]);
  38. }
  39. }
  40. selectTube(tubeId)
  41. {
  42. let newSelectedTube = this.tubes[tubeId];
  43. if (this.selectedTube == null && newSelectedTube.isEmpty()) return;
  44. if(this.selectedTube == null && !newSelectedTube.isEmpty())
  45. {
  46. this.selectedTube = newSelectedTube;
  47. }
  48. else if (this.selectedTube == newSelectedTube)
  49. {
  50. this.selectedTube = null;
  51. }
  52. else if (newSelectedTube.isEmpty() || this.selectedTube.peekTopColor() == newSelectedTube.peekTopColor())
  53. {
  54. this.pourColorInto(this.selectedTube, newSelectedTube);
  55. }
  56. }
  57. pourColorInto(sourceTube, targetTube)
  58. {
  59. if (sourceTube.isEmpty()) return;
  60. let pouringColor = sourceTube.peekTopColor();
  61. while (sourceTube.peekTopColor() == pouringColor && !targetTube.isFull())
  62. {
  63. targetTube.addColor(sourceTube.removeColorLayer());
  64. }
  65. }
  66. }