Last active
January 14, 2016 07:51
-
-
Save kchia/87c09422bfa1d7e48890 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// sets up Board class for an n x n board, where n is the dimension | |
var Board = function(dimension, mines) { | |
this.board = []; | |
// place all mined cells into array first, followed by mine-less cells | |
this.plantMines(dimension,mines); | |
// perform an in-place shuffle to randomly place mines on board; see algorithm below | |
this.board = shuffle(this.board); | |
// convert 1D array to 2D array | |
this.transform(dimension); | |
console.log('Generated a board of dimensions ' + dimension + ' by ' + dimension); | |
console.log('There are ' + this.countMines() + ' mines on your board!'); | |
}; | |
Board.prototype.plantMines = function(dimension,mines) { | |
var initialMinesPlanted = 0, | |
numberOfCells = dimension * dimension, | |
i; | |
for(i = 0; i < numberOfCells; i++) { | |
if(initialMinesPlanted < mines) { | |
this.board.push(new Cell(true)); | |
initialMinesPlanted++; | |
} else { | |
this.board.push(new Cell(false)); | |
} | |
} | |
} | |
Board.prototype.transform = function(dimension) { | |
var row = []; | |
while(this.board.length > dimension) { | |
row = this.board.splice(0,dimension); | |
this.board.push(row); | |
row = []; | |
} | |
}; | |
// retrieves board 2D array | |
Board.prototype.getBoard = function() { | |
return this.board; | |
}; | |
// checks the number of mines on your board instance | |
Board.prototype.countMines = function() { | |
var count, i, j, row, cell; | |
count = 0; | |
for(i = 0; i < this.board.length; i++) { | |
row = this.board[i]; | |
for(j = 0; j < row.length; j++) { | |
cell = row[j]; | |
if(cell.hasMine){ | |
count++; | |
} | |
} | |
} | |
return count; | |
}; | |
// sets up Cell class, with only hasMine property | |
var Cell = function(val) { | |
this.hasMine = val; | |
}; | |
Cell.prototype.setMine = function(val) { | |
if(typeof val === 'boolean') { | |
this.hasMine = val; | |
} else { | |
throw new Error('Expected a boolean input but received a ' + typeof val + ' input instead.' ) | |
} | |
}; | |
// for checking whether the cell has a mine | |
Cell.prototype.getMineValue = function() { | |
return this.hasMine; | |
}; | |
/* | |
Helper Functions | |
*/ | |
// in-place array shuffle with O(n) time complexity | |
var shuffle = function(arr) { | |
var k, l, temp; | |
k = arr.length; | |
while (k) { | |
l = Math.floor(Math.random() * k--); | |
temp = arr[k]; | |
arr[k] = arr[l]; | |
arr[l] = temp; | |
} | |
return arr; | |
}; | |
// sets the dimension and number of mines to be placed on the board | |
var dimension = 10; | |
var mines = 10; | |
// instantiates a board | |
var board = new Board(dimension,mines); | |
console.log(board.getBoard()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment