AngularJS Templae
Basic JSFiddle for AngularJS 1.2.1. Already contains the ngApp, ngController, data and method binding.
by Luis Perez
HTML
<div ng-controller="MinesweeperController">
<h3 ng-if="isWinMessageVisible">You won!</h3>
<table class="minefield">
<tr ng-repeat="row in minefield.rows">
<td ng-repeat="spot in row.spots" ng-click="uncoverSpot(spot)">
<img ng-if="spot.isCovered" src="http://luis-perez.s3-us-west-2.amazonaws.com/angularjs-minesweeper-game/covered.png" />
<img ng-if="!spot.isCovered && spot.content == 'empty'" src="http://luis-perez.s3-us-west-2.amazonaws.com/angularjs-minesweeper-game/empty.png" />
<img ng-if="!spot.isCovered && spot.content == 'mine'" src="http://luis-perez.s3-us-west-2.amazonaws.com/angularjs-minesweeper-game/mine.png" />
<img ng-if="!spot.isCovered && spot.content == 1" src="http://luis-perez.s3-us-west-2.amazonaws.com/angularjs-minesweeper-game/number-1.png" />
<img ng-if="!spot.isCovered && spot.content == 2" src="http://luis-perez.s3-us-west-2.amazonaws.com/angularjs-minesweeper-game/number-2.png" />
<img ng-if="!spot.isCovered && spot.content == 3" src="http://luis-perez.s3-us-west-2.amazonaws.com/angularjs-minesweeper-game/number-3.png" />
<img ng-if="!spot.isCovered && spot.content == 4" src="http://luis-perez.s3-us-west-2.amazonaws.com/angularjs-minesweeper-game/number-4.png" />
<img ng-if="!spot.isCovered && spot.content == 5" src="http://luis-perez.s3-us-west-2.amazonaws.com/angularjs-minesweeper-game/number-5.png" />
<img ng-if="!spot.isCovered && spot.content == 6" src="http://luis-perez.s3-us-west-2.amazonaws.com/angularjs-minesweeper-game/number-6.png" />
<img ng-if="!spot.isCovered && spot.content == 7" src="http://luis-perez.s3-us-west-2.amazonaws.com/angularjs-minesweeper-game/number-7.png" />
<img ng-if="!spot.isCovered && spot.content == 8" src="http://luis-perez.s3-us-west-2.amazonaws.com/angularjs-minesweeper-game/number-8.png" />
...
CSS
.minefield {
line-height:0;
border-spacing:0;
}
.minefield td {
padding:0
}
JavaScript
function createMinefield() {
var minefield = {};
minefield.rows = [];
for(var i = 0; i < 9; i++) {
var row = {};
row.spots = [];
for(var j = 0; j < 9; j++) {
var spot = {};
spot.isCovered = true;
spot.content = "empty";
row.spots.push(spot);
}
minefield.rows.push(row);
}
placeManyRandomMines(minefield);
calculateAllNumbers(minefield);
return minefield;
}
function getSpot(minefield, row, column) {
return minefield.rows[row].spots[column];
}
function placeRandomMine(minefield) {
var row = Math.round(Math.random() * 8);
var column = Math.round(Math.random() * 8);
var spot = getSpot(minefield, row, column);
spot.content = "mine";
}
function placeManyRandomMines(minefield) {
for(var i = 0; i < 10; i++) {
placeRandomMine(minefield);
}
}
function calculateNumber(minefield, row, column) {
var thisSpot = getSpot(minefield, row, column);
// if this spot contains a mine then we can't place a number here
if(thisSpot.content == "mine") {
return;
}
var mineCount = 0;
// check row above if this is not the first row
if(row > 0) {
// check column to the left if this is not the first column
if(column > 0) {
// get the spot above and to the left
var spot = getSpot(minefield, row - 1, column - 1);
if(spot.content == "mine") {
mineCount++;
}
}
// get the spot right above
var spot = getSpot(minefield, row - 1, column);
if(spot.content == "mine") {
mineCount++;
}
// check column to the right if this is not the last column
if(column < 8) {
// get the spot above and to the right
var spot = getSpot(minefield, row - 1, column + 1);
if(spot.content == "mine") {
...