Win an Ipad
  Developer   Demos & Samples   Minesweeper

Minesweeper

Screenshot
Minesweeper is a classic game, known from the Microsoft Windows OS. The objective of the game is to clear an abstract minefield without detonating a mine. In this article, we are going to show you how to create this game for both smartphones and tablets.

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

 

 Memory Cards (Pairs, ... 
18.12.2012 21:18:33
there is more to this game

this only explains the mines part of the demo, but this is the most complete game for moscrif, it handles vector painting, scenes handeling, Objects and classes, how to handle resources and more, so this tutorial is too shot

Write a Comment (1)
Subject
Please complete this mandatory field.
HTML Tags Not Allowed!
Comment
Please complete this mandatory field.