arraysweeper
Advanced tools
Comparing version 0.0.6 to 0.0.7
module.exports = { | ||
readme: { | ||
src: [ "index.md", "Gruntfile.md" ] | ||
src: [ "gui.md", "Gruntfile.md" ] | ||
} | ||
}; |
@@ -1,1 +0,1 @@ | ||
!function(a,b){"function"==typeof define&&define.amd?define([],b):"object"==typeof exports?module.exports=b():a.ArraySweeper=b()}(this,function(){for(var a=function(a,b,c){this.height=a,this.width=b,this.count=c,this._buildBoard()},b={mineCount:function(a,b){return"revealed"===this._board[a][b].state?this._board[a][b].count:"Reveal space to see count"},flag:function(a,b,c){return void 0===c?(this.count.flag++,"flag"===this._board[a][b].state):void(this._board[a][b].state=c?"flag":"hidden")},reveal:function(a,b){var c=this._board[a][b];return c.bomb?"Game Over!":this.count.flag+this.count.hidden===this.count.mine?"You Win!":(this.count.moves++,this._reveal(a,b))},render:function(){for(var a=[],b=0;b<this.height;b++)a.push(JSON.stringify(this._board[b].map(this._renderMap)));console.log(a.join(",\n"))},getBoard:function(){return this._board},_renderMap:function(a){return"revealed"===a.state?a.count.toString():"flag"===a.state?"!":"X"},_getMines:function(){this.mines=[];for(var a=0;a<this.count;a++){for(var b,c=!1;c===!1;){var d=this._getMine();-1===this.mines.indexOf(d.row+","+d.col)&&(b=d,c=!0)}this.mines.push(b.row+","+b.col)}},_getMine:function(){return{row:Math.floor(Math.random()*(this.height-1)),col:Math.floor(Math.random()*(this.width-1))}},_reveal:function(a,b,c,d){d=d||0,c="object"==typeof c?c:[],this._board[a][b].state="revealed",this.revealCount++;for(var e=a-1>0?a-1:0;a+1>=e;e++)for(var f=b-1>0?b-1:0;b+1>=f;f++)e>=0&&f>=0&&e<this.height&&f<this.width&&0===this._board[e][f].count&&"revealed"!==this._board[e][f].state&&!this._board[e][f].bomb&&-1===c.indexOf(e+","+f)&&(this._board[e][f].state="",c.push(e+","+f));if(c.length&&c.length>=d+1){var g=c[d].split(",");this._reveal(g[0],g[1],c,d+1)}return c},_count:function(a,b){for(var c=0,d=a-1;a+1>=d;d++)for(var e=b-1;b+1>=e;e++)-1!==this.mines.indexOf(d+","+e)&&c++;return c},_buildBoard:function(){var a=[];this._getMines();for(var b=0;b<this.height;b++){for(var c=[],d=0;d<this.width;d++)c.push({state:"hidden",bomb:this.mines.indexOf(b+","+d)>=0,count:this._count(b,d)});a.push(c)}this._board=a}},c=Object.keys(b),d=0;d<c.length;d++)a.prototype[c[d]]=b[c[d]];return a}); | ||
!function(a,b){"function"==typeof define&&define.amd?define([],b):"object"==typeof exports?module.exports=b():a.Arraysweeper=b()}(this,function(){for(var a=function(a,b,c){this.height=a,this.width=b,this.count={mines:c,hidden:b*a,moves:0,revealed:0,flags:0},this._buildBoard()},b={mineCount:function(a,b){return"revealed"===this._board[a][b].state?this._board[a][b].count:"Reveal space to see count"},flag:function(a,b,c){return void 0===c?"flag"===this._board[a][b].state:(this.count.flags=this.count.flags+(c?1:-1),this.count.hidden=this.count.hidden-(c?1:-1),this._board[a][b].state=c?"flag":"hidden",console.log(this.count.hidden),this.count.flags+this.count.hidden===this.count.mines?"You Win!":void 0)},reveal:function(a,b){var c,d,e=this._board[a][b];return e.bomb&&0!==this.count.moves?"Game Over!":(e.bomb?this._buildBoard():this.count.flags+this.count.hidden===this.count.mines&&(c="You Win!"),this.count.moves++,d=this._reveal(a,b),c||d)},render:function(){for(var a=[],b=0;b<this.height;b++)a.push(JSON.stringify(this._board[b].map(this._renderMap)));console.log(a.join(",\n"))},getBoard:function(){return this._board},_renderMap:function(a){return"revealed"===a.state?a.count.toString():"flag"===a.state?"!":"X"},_getMines:function(){this.mines=[];for(var a=0;a<this.count.mines;a++){for(var b,c=!1;c===!1;){var d=this._getMine();-1===this.mines.indexOf(d.row+","+d.col)&&(b=d,c=!0)}this.mines.push(b.row+","+b.col)}},_getMine:function(){return{row:Math.floor(Math.random()*(this.height-1)),col:Math.floor(Math.random()*(this.width-1))}},_reveal:function(a,b,c,d){d=d||0,c="object"==typeof c?c:[],this._board[a][b].state="revealed",this.count.revealed++,this.count.hidden--;for(var e=a-1>0?a-1:0;a+1>=e&&e<this.width;e++)for(var f=b-1>0?b-1:0;b+1>=f&&f<this.width;f++)e>=0&&f>=0&&e<this.height&&f<this.width&&0===this._board[e][f].count&&"revealed"!==this._board[e][f].state&&!this._board[e][f].bomb&&-1===c.indexOf(e+","+f)&&(e!==a||f!==b)&&(this._board[e][f].state="",c.push(e+","+f));if(c.length&&c.length>=d+1){var g=c[d].split(",");this._reveal(parseFloat(g[0],10),parseFloat(g[1],10),c,d+1)}return c},_count:function(a,b){for(var c=0,d=a-1;a+1>=d;d++)for(var e=b-1;b+1>=e;e++)-1!==this.mines.indexOf(d+","+e)&&c++;return c},_buildBoard:function(){var a=[];this._getMines();for(var b=0;b<this.height;b++){for(var c=[],d=0;d<this.width;d++)c.push({state:"hidden",bomb:this.mines.indexOf(b+","+d)>=0,count:this._count(b,d)});a.push(c)}this._board=a}},c=Object.keys(b),d=0;d<c.length;d++)a.prototype[c[d]]=b[c[d]];return a}); |
38
index.js
@@ -24,3 +24,9 @@ // This just supports loading in any enviroment | ||
this.width = width; | ||
this.count = count; | ||
this.count = { | ||
mines: count, | ||
hidden: width * height, | ||
moves: 0, | ||
revealed: 0, | ||
flags: 0 | ||
}; | ||
this._buildBoard(); | ||
@@ -51,6 +57,11 @@ }; | ||
if ( state === undefined ) { | ||
this.count.flag++; | ||
return this._board[ row ][ col ].state === "flag"; | ||
} | ||
this.count.flags = this.count.flags + ( state ? 1 : -1 ); | ||
this.count.hidden = this.count.hidden - ( state ? 1 : -1 ); | ||
this._board[ row ][ col ].state = state ? "flag" : "hidden"; | ||
console.log( this.count.hidden ); | ||
if ( this.count.flags + this.count.hidden === this.count.mines ) { | ||
return "You Win!"; | ||
} | ||
}, | ||
@@ -64,12 +75,16 @@ | ||
reveal: function( row, col ) { | ||
var returnValue; | ||
var revealValue; | ||
var space = this._board[ row ][ col ]; | ||
if ( space.bomb ) { | ||
if ( space.bomb && this.count.moves !== 0 ) { | ||
return "Game Over!"; | ||
} else if ( this.count.flag + this.count.hidden === this.count.mine ) { | ||
return "You Win!"; | ||
} else if ( space.bomb ) { | ||
this._buildBoard(); | ||
} else if ( this.count.flags + this.count.hidden === this.count.mines ) { | ||
returnValue = "You Win!"; | ||
} | ||
this.count.moves++; | ||
return this._reveal( row, col ); | ||
revealValue = this._reveal( row, col ); | ||
return returnValue || revealValue; | ||
}, | ||
@@ -103,3 +118,3 @@ | ||
this.mines = []; | ||
for ( var i = 0; i < this.count; i++ ) { | ||
for ( var i = 0; i < this.count.mines; i++ ) { | ||
var valid = false; | ||
@@ -130,8 +145,7 @@ var mine; | ||
this._board[ row ][ col ].state = "revealed"; | ||
this.revealCount++; | ||
this.count.revealed++; | ||
this.count.hidden--; | ||
for ( var r = row - 1 > 0 ? row - 1 : 0; r <= row + 1 && r < this.width; r++ ) { | ||
for ( var c = col - 1 > 0 ? col - 1 : 0; c <= col + 1 && c < this.width; c++ ) { | ||
console.log( row + ":" + col + " - " + r + "," + c ); | ||
console.log( col + 1 ); | ||
if ( r >= 0 && c >= 0 && r < this.height && c < this.width && | ||
@@ -142,3 +156,3 @@ this._board[ r ][ c ].count === 0 && | ||
pending.indexOf( r + "," + c ) === -1 && | ||
( r !== row || c!== col ) ) { | ||
( r !== row || c !== col ) ) { | ||
this._board[ r ][ c ].state = ""; | ||
@@ -145,0 +159,0 @@ pending.push( r + "," + c ); |
{ | ||
"name": "arraysweeper", | ||
"description": "A array based implementation of a minesweeper board. Can be used via a browserconsole or the command line via node JS. The main intended purpose of this is to provide the board, and logic to implement a traditional minesweeper game using the api.", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Alex Schmitz", |
@@ -1,2 +0,2 @@ | ||
# ArraySweeper | ||
# Arraysweeper | ||
@@ -9,3 +9,3 @@ A module for creating an array based minesweeper game. | ||
### ArraySweeper.module:ArraySweeper(height, width, count) | ||
### Arraysweeper.module:Arraysweeper(height, width, count) | ||
@@ -24,3 +24,3 @@ A module for creating an array based minesweeper game. | ||
### ArraySweeper.mineCount(row, col) | ||
### Arraysweeper.mineCount(row, col) | ||
@@ -37,3 +37,3 @@ Get the mine count for the current space | ||
### ArraySweeper.flag(row, col, state) | ||
### Arraysweeper.flag(row, col, state) | ||
@@ -52,3 +52,3 @@ Get or set the flag status of the space | ||
### ArraySweeper.reveal(row, col) | ||
### Arraysweeper.reveal(row, col) | ||
@@ -65,3 +65,3 @@ Reveal the space | ||
### ArraySweeper.render() | ||
### Arraysweeper.render() | ||
@@ -72,3 +72,3 @@ Render the board - This logs a textual representation to the console | ||
### ArraySweeper.getBoard() | ||
### Arraysweeper.getBoard() | ||
@@ -75,0 +75,0 @@ Get the board object - This should not be used by players only when implementing the api |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14649
20
266