forked from jontrainor/minesweeper-clone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsgame.js
160 lines (138 loc) · 4.16 KB
/
msgame.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
(function() {
function dom(element, className, contents) {
var element = jQuery(document.createElement(element));
if (className) {
element.addClass(className);
}
if(contents) {
jQuery.each(contents, function(index, content) {
element.append(content);
});
}
return element;
}
function makeIntArray(max) {
var arr = [];
for(var i=0; i < max; i++) {
arr.push(i);
}
return arr;
}
function getHintDistance(cols, pos) {
var hintDistanceCenter = [-1*(cols+1), -1*cols, -1*(cols-1), -1, 1, cols-1, cols, cols+1];
var hintDistanceLeft = [-1*cols, -1*(cols-1), 1, cols, cols+1];
var hintDistanceRight = [-1*(cols+1), -1*cols, -1, cols-1, cols];
if(pos === 0 || pos % cols === 0) {
return hintDistanceLeft;
} else if((pos + 1) % cols === 0) {
return hintDistanceRight;
} else {
return hintDistanceCenter;
}
};
function makePuzzleArray(rows, cols, bombCount) {
var randArr = makeIntArray(rows*cols);
var puzzleArr = [];
var incBombHints = function(bombPosition) {
var hintDistance = getHintDistance(cols, bombPosition);
for(var i=0; i < hintDistance.length; i++) {
var hintPosition = bombPosition + hintDistance[i];
if(hintPosition >= 0 && puzzleArr[hintPosition] >= 0 && puzzleArr[hintPosition] < rows*cols) {
puzzleArr[hintPosition]++;
}
}
};
//fill puzzleArr with zeros
for(var i=0; i < rows*cols; i++) {
puzzleArr[i] = 0;
}
//fill in bombs and hints
for(var i=0; i < bombCount; i++) {
//fill in bombs
var randInt = Math.floor(Math.random()*randArr.length);
var bombPosition = randArr[randInt];
//remove position from array
randArr.splice(randInt, 1);
puzzleArr[bombPosition] = -1;
incBombHints(bombPosition);
}
return puzzleArr;
}
var drawPuzzle = function() {
var ratioBombsToBoxes = 0.2;
var boxSize = 25;
var boxSizeWithBorder = boxSize + 2;
var windowWidth = 750;
var windowHeight = 400;
var rows = Math.floor(windowHeight/boxSizeWithBorder);
var cols = Math.floor(windowWidth/boxSizeWithBorder);
var bombCount = Math.floor(rows*cols*ratioBombsToBoxes);
var puzzleArray = makePuzzleArray(rows, cols, bombCount);
function addSelectedBoxClass(pos) {
for(var i=0; i < pos.length; i++) {
var selectedBox = jQuery('#' + pos[i]);
var boxVal = puzzleArray[pos[i]];
if(pos[i] >= 0 && pos[i] < rows*cols && !selectedBox.hasClass('selected')) {
selectedBox.addClass('selected ' + (boxVal < 0 ? 'bomb ' : '') + (boxVal === 0 ? 'empty ' : '') + (boxVal > 0 ? 'hint' : ''));
if(boxVal < 0) {
gameOver();
} else if(boxVal > 0) {
selectedBox.text(boxVal);
} else if(boxVal === 0) {
var hintDistance = getHintDistance(cols, pos[i]);
for(var j=0; j < hintDistance.length; j++) {
hintDistance[j] += pos[i];
}
addSelectedBoxClass(hintDistance);
}
}
}
}
var drawBox = function(className, contents) {
return dom('div', className || '', contents).css({'height': boxSize + 'px', 'width': boxSize + 'px'});
};
var puzzle = dom('div', 'puzzleContainer', []).css({'height': rows*boxSizeWithBorder + 'px', 'width': cols*boxSizeWithBorder + 'px'});
for(var i=0; i < (rows*cols); i++) {
puzzle.append(drawBox('box', i).attr('id', i));
};
jQuery('.mainview').append(puzzle);
var shiftDown = false;
jQuery(document).keydown(function(e) {
if(e.keyCode === 16) {
shiftDown = true;
}
});
jQuery(document).keyup(function(e) {
if(e.keyCode === 16) {
shiftDown = false;
}
});
jQuery('.box').click(function() {
if(shiftDown === true) {
jQuery(this).toggleClass('flag');
} else {
if(!jQuery(this).hasClass('flag')) {
var puzzlePosition = jQuery(this).attr('id');
addSelectedBoxClass([parseInt(puzzlePosition)]);
}
}
});
};
var setup = function() {
jQuery('.startButton').click(function() {
jQuery('.startview').hide();
jQuery('.mainview').empty().show();
drawPuzzle();
});
jQuery('.restartButton').click(function() {
jQuery('.gameoverview').hide();
jQuery('.mainview').empty().show();
drawPuzzle();
});
};
var gameOver = function() {
jQuery('.gameoverview').show();
};
setup();
drawPuzzle();
})();