Placing the mines

Once the empty mine field has been created, we need to add the mines. We just have to define how many mines we want in the game, then place them in random spots.

The idea: We know a mined tile will have 9 value, so the idea is to select a random tile, check if its value is 0, then set it to 9. You must check if its value is zero to avoid placing a mine on a tile that already contains a mine. You would end up with a number of mined tiles that's less than the one you defined.

The development: Remove all previous traces to clean the code and change class level variables this way:

// class level variables private const FIELD_W:uint=9; private const FIELD_H:uint=9; private const NUM_MINES:uint=10; private var mineField:Array=new ...

Get Flash Game Development by Example now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.