Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. class Game {
  2. constructor(numberOfRows, numberOfColumns, numberOfBombs) {
  3. this._board = new Board(numberOfRows,numberOfColumns,numberOfBombs);
  4. }
  5.  
  6. playMove(rowIndex, columnIndex) {
  7. this._board.flipTile(rowIndex, columnIndex);
  8. if (this._board.playerBoard[rowIndex]
  9. [columnIndex] === 'B'){
  10. console.log('The game is over!');
  11. this._board.print();
  12. }
  13. else if (!this._board.hasSafeTiles()) {
  14. console.log('congratulates the user on winning');
  15. } else {
  16. console.log('Current Board:');
  17. this._board.print();
  18. }
  19. }
  20. }
  21.  
  22. class Board {
  23. constructor(numberOfRows, numberOfColumns, numberOfBombs) {
  24. this._numberOfBombs = numberOfBombs;
  25. this._numberOfTiles = (numberOfRows * numberOfColumns);
  26. this._playerBoard = Board.generatePlayerBoard(numberOfRows, numberOfColumns);
  27. this._bombBoard = Board.generatePlayerBoard(numberOfRows, numberOfColumns, numberOfBombs);
  28. }
  29.  
  30. get playerBoard () {
  31. return this._playerBoard;
  32. }
  33.  
  34. flipTile(rowIndex, columnIndex) {
  35. if (this._playerBoard[rowIndex][columnIndex] !== '') {
  36. console.log('This tile has already been flipped!');
  37. return;
  38. }
  39. else if (bombBoard[rowIndex][columnIndex] === 'B'){
  40. console.log('Place your bomb here')
  41. }
  42. else {
  43. playerBoard [rowIndex][columnIndex] = this.getNumberOfNeighborBombs(rowIndex, columnIndex);
  44. }
  45. this._numberOfTiles--;
  46. };
  47.  
  48.  
  49. this_getNumberOfNeighborBombs(rowIndex, columnIndex) {
  50. const neighborOffsets = [
  51. [-1, -1],
  52. [-1, 0],
  53. [-1, 1],
  54. [0, -1],
  55. [0, 1],
  56. [1, -1],
  57. [1, 0],
  58. [1, 1]
  59. ];
  60.  
  61. const numberOfRows = bombBoard.length;
  62. const numberOfColumns = bombBoard[0].length;
  63. const numberOfBombs = 0;
  64. neighborOffsets.forEach(offset => {
  65. const neighborRowIndex = rowIndex + offset[0];
  66. const neighborColumnIndex = columnIndex + offset[1];
  67. if (neighborRowIndex >= 0 && neighborRowIndex < numberOfRows && neighborColumnIndex >= 0 && neighborColumnIndex < numberOfColumns) {
  68. if (bombBoard[neighborRowIndex] [neighborColumnIndex] == 'B'){
  69. numberOfBombs++;
  70. }
  71. }
  72. });
  73. return numberOfBombs;
  74. };
  75. hasSafeTiles(){
  76. return this._numberOfTiles !== this._numberOfBombs;
  77. }
  78.  
  79.  
  80.  
  81. print(board){
  82. console.log(this._playerBoard.map(row => row.join(' | ')).join('\n'));
  83. }
  84.  
  85. static generatePlayerBoard (numberOfRows, numberOfColumns){
  86. let board = [];
  87. for(let i = 0; i < numberOfRows; i++) {
  88. let row =[];
  89. for (let j = 0; j < numberOfColumns; j++) {
  90. row.push(" ");
  91. };
  92. board.push(row);
  93. };
  94. return board;
  95. };
  96.  
  97.  
  98. static generateBombBoard(numberOfRows, numberOfColumns, numberOfBombs) {
  99. let board = [];
  100. for(let i = 0; i < numberOfRows; i++) {
  101. let row =[];
  102. for (let j = 0; j < numberOfColumns; j++) {
  103. row.push(null);
  104. };
  105. board.push(row);
  106. };
  107.  
  108. let numberOfBombsPlaced = 0;
  109. while (numberOfBombsPlaced < numberOfBombs) {
  110. let randomRowIndex = Math.floor(Math.random() * numberOfRows);
  111. let randomColumnIndex = Math.floor(Math.random() * numberOfColumns);
  112. if (board[randomRowIndex] [randomColumnIndex] !== 'B') {
  113. board[randomRowIndex][randomColumnIndex] = 'B';
  114. numberOfBombsPlaced++;
  115. };
  116. /*The code in your while loop has the potential to place
  117. bombs on top of already existing bombs.*/
  118. };
  119. return board;
  120. }
  121.  
  122. };
  123.  
  124. //console.log(playerBoard);
  125.  
  126. /* let playerBoard = Board.generatePlayerBoard(3, 3);
  127. let bombBoard = Board.generateBombBoard(3, 4, 5);
  128. console.log('Player Board: ');
  129. printBoard(playerBoard);
  130. console.log('Bomb Board: ');
  131. printBoard(bombBoard);
  132. flipTile(playerBoard, bombBoard, 0, 0);
  133. console.log('Updated Player Board:');
  134. printBoard(playerBoard); */
  135. const g = new Game(3, 3, 3);
  136. g.playMove(1,0);
  137.  
  138.  
  139. //printBoard(board);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement