game.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. var GRID_SIZE = 5;
  2. var GRID_TOTAL = GRID_SIZE * GRID_SIZE;
  3. var STATE_LOSE = -1;
  4. var STATE_WIN = 1;
  5. var STATE_PLAY = 0;
  6. var BOMB_NUM = 5;
  7. var BLANK_NUM = GRID_TOTAL - BOMB_NUM;
  8. var I_BOMB = -1;
  9. var I_BLANK = 0;
  10. var I_NUMBER = 1;
  11. function Game() {
  12. STORAGE.init("sweep");
  13. var game = this;
  14. CONTROL.clickOn(".retry",
  15. function(event) {
  16. game.newGame()
  17. });
  18. this.setBestTime(STORAGE.getInt("best"));
  19. this.initCells();
  20. this.newGame();
  21. setInterval(function() {
  22. game.updateTime()
  23. },
  24. 100)
  25. }
  26. Game.prototype.initCells = function() {
  27. var container = $(".cell-container");
  28. this.cells = new Array(GRID_TOTAL);
  29. for (var i = 0; i < GRID_TOTAL; ++i) {
  30. var div = $(document.createElement("div"));
  31. var cell = new Cell(div, i % GRID_SIZE, i / GRID_SIZE);
  32. div.on(CLICK_EVENT, cell, this.cellClick);
  33. container.append(div);
  34. this.cells[i] = cell
  35. }
  36. };
  37. Game.prototype.newGame = function() {
  38. this.clearEnding();
  39. this.state = STATE_PLAY;
  40. this.startTime = new Date().getTime();
  41. this.clickNum = 0;
  42. this.randomCellData()
  43. };
  44. Game.prototype.randomCellData = function() {
  45. var cells = this.cells;
  46. var data = "1000010000100001000010000".split("");
  47. TOOL.shuffle(data);
  48. for (var n = 0; n < GRID_TOTAL; ++n) {
  49. cells[n].reset()
  50. }
  51. for (var n = 0; n < GRID_TOTAL; ++n) {
  52. var cell = cells[n];
  53. if (data[n] == "0") {
  54. continue
  55. }
  56. cell.item = I_BOMB;
  57. for (var i = -1; i <= 1; ++i) {
  58. for (var j = -1; j <= 1; ++j) {
  59. var x = i + cell.x;
  60. var y = j + cell.y;
  61. if (x < 0 || x >= GRID_SIZE || y < 0 || y >= GRID_SIZE) {
  62. continue
  63. }
  64. var around = cells[y * GRID_SIZE + x];
  65. if (around.item >= 0) {++around.item
  66. }
  67. }
  68. }
  69. }
  70. var r, c;
  71. do {
  72. r = parseInt(Math.random() * GRID_TOTAL);
  73. c = cells[r]
  74. } while ( c . item == I_BOMB );
  75. this.showCellAround(c)
  76. };
  77. Game.prototype.cellClick = function(event) {
  78. var game = GAME;
  79. var cell = event.data;
  80. if (cell.isClicked) {
  81. return
  82. }
  83. if (cell.item == I_BOMB) {
  84. game.loseGame()
  85. } else {
  86. game.showCellAround(cell);
  87. if (game.clickNum == BLANK_NUM) {
  88. game.winGame()
  89. }
  90. }
  91. };
  92. Game.prototype.winGame = function() {
  93. var time = $(".current>span").text();
  94. time = parseInt(time * 1000);
  95. if (time < this.best || this.best < 1) {
  96. this.setBestTime(time);
  97. STORAGE.save("best", time)
  98. }
  99. this.showEnding(STATE_WIN);
  100. dp_submitScore(time);
  101. };
  102. Game.prototype.loseGame = function() {
  103. this.showEnding(STATE_LOSE)
  104. };
  105. Game.prototype.showEnding = function(state) {
  106. this.showCellAll();
  107. this.state = state;
  108. var isWin = (state == STATE_WIN);
  109. var ending = $(".ending");
  110. var title = ending.find("p");
  111. title.text(isWin ? "胜利": "失败");
  112. title.css("background-color", isWin ? "#5cb85c": "#f0ad4e");
  113. var btn = ending.find(".retry");
  114. btn.attr("class", "retry btn btn-" + (isWin ? "success": "warning"));
  115. ending.show();
  116. };
  117. Game.prototype.clearEnding = function() {
  118. $(".ending").hide()
  119. };
  120. Game.prototype.showCellAround = function(cell) {
  121. if (cell.isClicked || cell.item == I_BOMB) {
  122. return
  123. }
  124. cell.isClicked = true; ++this.clickNum;
  125. if (cell.item == I_BLANK) {
  126. for (var i = 0; i < 4; ++i) {
  127. var x = DIRECTION[i].x + cell.x;
  128. var y = DIRECTION[i].y + cell.y;
  129. if (x < 0 || x >= GRID_SIZE || y < 0 || y >= GRID_SIZE) {
  130. continue
  131. }
  132. var around = this.cells[y * GRID_SIZE + x];
  133. this.showCellAround(around)
  134. }
  135. }
  136. this.showCell(cell)
  137. };
  138. Game.prototype.showCellAll = function() {
  139. for (var i = 0; i < GRID_TOTAL; ++i) {
  140. var cell = this.cells[i];
  141. this.showCell(cell)
  142. }
  143. };
  144. Game.prototype.showCell = function(cell) {
  145. var div = cell.div;
  146. switch (cell.item) {
  147. case I_BLANK:
  148. div.attr("class", "blank");
  149. break;
  150. case I_BOMB:
  151. div.html("<span class='glyphicon glyphicon-asterisk'></span>");
  152. div.attr("class", "bomb");
  153. break;
  154. default:
  155. div.html(cell.item);
  156. div.attr("class", "number");
  157. break
  158. }
  159. };
  160. Game.prototype.setBestTime = function(best) {
  161. this.best = best;
  162. var time = best / 1000;
  163. $(".best>span").text(best == 0 ? "--": time.toFixed(1))
  164. };
  165. Game.prototype.updateTime = function() {
  166. if (this.state != STATE_PLAY) {
  167. return
  168. }
  169. var timeDiv = $(".current>span");
  170. var time = new Date().getTime();
  171. time = (time - this.startTime) / 1000;
  172. if (time >= 99.9) {
  173. time = 99.9;
  174. this.loseGame()
  175. }
  176. timeDiv.text(time.toFixed(1))
  177. };
  178. function Cell(div, x, y) {
  179. this.x = parseInt(x);
  180. this.y = parseInt(y);
  181. this.div = div
  182. }
  183. Cell.prototype.reset = function() {
  184. this.item = 0;
  185. this.isClicked = false;
  186. this.div.empty();
  187. this.div.attr("class", "")
  188. }