User Interface
User interface is very simple. It only consists of a table with mines, timer and menu. After user loses or wins the game, menu drops down and shows ending message and playing time.
Mines distribution
Mines are distributed randomly and distribution is managed by one while circle. Condition of this circle ensures that there won’t be two mines in one cell.
// deploy mines tu the table and also calculate all numbers
function _generateMines()
{
var x,y;
// calculate random positions of mines
for(var i = 0; i
do {
// generate x and y position
x = rand(this.columns - 1);
y = rand(this.rows - 1);
// if there is mine on generated position generate new position
} while (this.table[x][y]._state == -1); this.table[x][y]._state = -1;
this.minesList[i] = new Array();
this.minesList[i][0] = x;
this.minesList[i][1] = y
this._addToNeighbours(x,y)
}
}
Digits calculator
Digits in the cells indicate the number of the mines in surrounding squares. These digits are calculated by addToNeighbours function - mentioned in previous code. Behavior of this function is very simple. It only adds one digit to every surrounding cell which does contain mine. This function is called for every cell.
// add one to all neighbours of mine
function _addToNeighbours(x,y)
{
for (var i = x-1; i < x + 2; i++) {
for (var q = y-1; q < y + 2; q++) {
if (this._inTable(i,q) && !(y == q && x == i) && this.table[i][q]._state != -1)
this.table[i][q]._state += 1;;
}
}
}
When mines are distributed and digits calculated, the game can start. Uncovering the mines is a simple operation. Only uncovering an area with no mines can be quite a challenge. Our application manages this operation by recursive calls of uncoverNull function.
// uncover cell with no number with all null neighbours.
function _uncoverNull(x,y)
{
this.table[x][y].uncovered = true;
this.open();
this.table[x][y].native.invalidate(); if (this.table[x][y]._state == 0) {
for (var i = x-1; i < x + 2; i++) {
for (var q = y-1; q < y + 2; q++) {
if (this._inTable(i,q) && !this.table[i][q].uncovered && !this.table[i][q].mark)
this._uncoverNull(i, q);
}
}
}
}
So let’s try it and have some fun!!!
The best part is that there are no differences across all supported platforms like Android, iOS, Symbian, Samsung bada and Windows Mobile.
License
This article, along with any associated source codes and files, is licensed under The BSD License
|