forked from AdamYee/AlbertMineSweeper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.js
More file actions
77 lines (69 loc) · 2.09 KB
/
ui.js
File metadata and controls
77 lines (69 loc) · 2.09 KB
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
/* Called after a WIN or a LOSS.
* Intended to disconnect the user interface from the game state,
* freezing the html and preventing the user from playing the game
* any further.
* User would be forced to either quit or refresh the page for
* a new game.
*
* Current plan: Overlay a Div in front of the board, displaying message. Later, will also provide button to refresh.
*/
var stopGame = function () {
// $("script:eq(1)").remove(); //#1
//$("#USER_INTERFACE").remove(); //#2
//$(document).replace("script", "NOT_A_SCRIPT"); //#3
// var replaceLoc = $(document.head); //#4
// var regExMatch = "script";
// var replaceWith = "NOT_A_SCRIPT";
// var result = replaceLoc.replace(regExMatch, replaceWith);
// $("head").html(result);
};
$(document).ready(function() {
var grid;
var report;
$('#gridSetup').submit(function(){
var $values = $('#gridSetup').serializeArray();
grid = new Grid(parseInt($values[0].value),parseInt($values[1].value),parseInt($values[2].value));
// var grid = new Grid(7,7,5);
report = grid.print();
}); // <<<The program kicks me out here.>>>
$('.cell').mousedown(function(event) {
var row = $(this).data('row');
var col = $(this).data('col');
//right-click event for flags
if(event.which === 3) {
if(grid.setFlag(row,col)) {
$(this).toggleClass('flagged');
$(this).html("F");
}
}
//normal-click
if(event.which === 1) {
if(grid.stomp(row,col)) {
$('.cell').each(function() {
var row = $(this).data('row');
var col = $(this).data('col');
if(grid.cells[row][col].mine) {
grid.cells[row][col].visited = true;
$(this).addClass('red');
$(this).html(report[row][col]);
}
});
alert("Loss\nPlease refresh page for a new game.");
stopGame();
} else {
$('.cell').each(function() {
var row = $(this).data('row');
var col = $(this).data('col');
if(grid.cells[row][col].visited) {
$(this).addClass('green');
$(this).html(report[row][col]);
}
});
}
}
if(grid.winCheck()) {
alert("Win.\nPlease refresh page for a new game.");
stopGame();
}
});
});