Jeu de démineur sur navigateur avec p5.js
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. class Grid {
  2. cols;
  3. rows;
  4. cells;
  5. nbMines;
  6. nbCells;
  7. constructor (cols, rows, nbMines) {
  8. this.cols = cols;
  9. this.rows = rows;
  10. this.nbCells = rows * cols;
  11. this.nbMines = nbMines;
  12. this.init();
  13. }
  14. init () {
  15. var remainingMines = this.nbMines;
  16. var remainingCells = this.nbCells;
  17. this.cells = new Array(this.cols);
  18. for (var i = 0; i < this.cols; i++) {
  19. this.cells[i] = new Array(this.rows);
  20. for (var j = 0; j < this.rows; j++) {
  21. var setMine = Math.random() < (remainingMines / remainingCells);
  22. this.cells[i][j] = new Cell(i, j, setMine);
  23. if (setMine) {remainingMines--;}
  24. remainingCells--;
  25. }
  26. }
  27. }
  28. cellNeighbors(cell) {
  29. if (cell.neighbors != null) return cell.neighbors;
  30. var neighbors = [];
  31. var x = cell.x;
  32. var y = cell.y;
  33. if (x > 0 && y > 0) neighbors.push(this.cell(x-1, y-1));
  34. if (x > 0) neighbors.push(this.cell(x-1, y ));
  35. if (x > 0 && y < this.rows-1) neighbors.push(this.cell(x-1, y+1));
  36. if (y > 0) neighbors.push(this.cell(x , y-1));
  37. if (y < this.rows-1) neighbors.push(this.cell(x , y+1));
  38. if (x < this.cols-1 && y > 0) neighbors.push(this.cell(x+1, y-1));
  39. if (x < this.cols-1) neighbors.push(this.cell(x+1, y ));
  40. if (x < this.cols-1 && y < this.rows-1) neighbors.push(this.cell(x+1, y+1));
  41. cell.neighbors = neighbors;
  42. return neighbors;
  43. }
  44. computeCellWeight (cell) {
  45. if (cell.weight >= 0) return cell.weight;
  46. var weight = 0;
  47. var neighbors = this.cellNeighbors(cell);
  48. neighbors.forEach(neighbor => {
  49. if (neighbor.hasMine) weight ++;
  50. })
  51. cell.weight = weight;
  52. return weight;
  53. }
  54. revealCell (cell) {
  55. if (cell.flagged || cell.revealed) return 0;
  56. if (cell.hasMine) return -1;
  57. var nbRevealed = 1
  58. cell.weight = this.computeCellWeight(cell);
  59. cell.reveal();
  60. if (cell.weight == 0) {
  61. var neighbors = this.cellNeighbors(cell);
  62. neighbors.forEach(neighbor => {
  63. nbRevealed += this.revealCell(neighbor);
  64. });
  65. }
  66. return nbRevealed;
  67. }
  68. cell (x, y) {
  69. return this.cells[x][y];
  70. }
  71. fullReveal () {
  72. for (var i = 0; i < this.cols; i++) {
  73. for (var j = 0; j < this.rows; j++) {
  74. var cell = this.cell(i, j);
  75. grid.computeCellWeight(cell);
  76. cell.flagged = false;
  77. cell.reveal();
  78. }
  79. }
  80. }
  81. inbounds(x, y) {
  82. return x >= 0 && x < this.cols && y >= 0 && y < this.rows;
  83. }
  84. }