-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.js
105 lines (100 loc) · 1.91 KB
/
logic.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
var app = angular.module('myapp',[]);
app.controller('controler',function($scope){
$scope.minified= crateminified();
});
function crateminified(){
var minified={};
minified.rows = [];
for(var i=0;i<9;i++){
var row={};
row.cols = [];
for(var j=0;j<9;j++){
var col={};
col.isRevealed=true;
col.content = "empty";
row.cols.push(col);
}
minified.rows.push(row);
}
placeManyRandomMines(minified);
callAllNumber(minified);
return minified;
}
function placerandommines(minified){
var row = Math.floor(Math.random()*8);
var column = Math.floor(Math.random()*8);
var col = getcol(minified,row,column);
col.content = "mine";
}
function getcol(minified,row,column){
return minified.rows[row].cols[column];
}
function placeManyRandomMines(minified){
for(var j=0;j<11;j++){
placerandommines(minified);
}
}
function createNumber(minified,row,column){
var thiscol = getcol(minified,row,column);
if(thiscol.content == 'mine'){
return;
}
var minecount=0;
if(row>0){
if(column>0){
var col= getcol(minified,row-1,column-1);
if(col.content == 'mine'){
minecount++;
}
}
var col= getcol(minified,row-1,column);
if(col.content == 'mine'){
minecount++;
}
if(column<8){
var col= getcol(minified,row-1,column+1);
if(col.content == 'mine'){
minecount++;
}
}
}
if(column>0){
var col= getcol(minified,row,column-1);
if(col.content == 'mine'){
minecount++;
}
}
if(column<8){
var col= getcol(minified,row,column+1);
if(col.content == 'mine'){
minecount++;
}
}
if(row<8){
if(column>0){
var col= getcol(minified,row+1,column-1);
if(col.content == 'mine'){
minecount++;
}
}
var col= getcol(minified,row+1,column);
if(col.content == 'mine'){
minecount++;
}
if(column<8){
var col= getcol(minified,row+1,column+1);
if(col.content == 'mine'){
minecount++;
}
}
}
if(minecount > 0){
thiscol.content = minecount;
}
}
function callAllNumber(minified){
for(var i=0;i<9;i++){
for(var j=0;j<9;j++){
createNumber(minified,i,j);}
}
}