-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgamelogic.js
More file actions
203 lines (178 loc) · 5.13 KB
/
gamelogic.js
File metadata and controls
203 lines (178 loc) · 5.13 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
$(function() {
var gameActive = true;
var godModeOn = false;
// each tile has two variables
function tile() {
this.hasMine = false;
this.isClicked = false;
}
// create new blank board
function newGame(size, numMines) {
// empty board
var board = [];
for (var i = 0; i < size; i++) {
board[i] = [];
for (var j = 0; j < size; j++) {
// each coordinate is made of "tiles"
board[i][j] = new tile();
}
}
// call populateMines
board = populateMines(board, size, numMines);
return board;
}
// add mines to board
function populateMines(gameBoard, size, numMines) {
var mineCount = 0;
while (mineCount < numMines) {
// randomly selects a tile in array
var i = Math.floor((Math.random() * (size - 1)));
var j = Math.floor((Math.random() * (size - 1)));
if (!gameBoard[i][j].hasMine) {
// places mines
gameBoard[i][j].hasMine = true;
mineCount++;
}
}
return gameBoard;
}
// create new gameboard 8x8 and 10 mines
var size = 8;
var mines = 10;
var gameBoard = newGame(size, mines);
// if you click on a tile
function tileClicked(x, y) {
gameBoard[x][y].isClicked = true;
// change background to red
$(".grid tr:nth-child(" + (x + 1) + ") td:nth-child(" + (y + 1) + ")").css('background-color', 'red');
// explode if there is a mine/end game
if (gameBoard[x][y].hasMine) {
$(".grid tr:nth-child(" + (x + 1) + ") td:nth-child(" + (y + 1) + ")").html("O");
endGame();
} else {
// show how many mines
showNearbyMineCount(x, y);
}
}
// count mines around it
function showNearbyMineCount(x, y) {
var mineCounter = 0;
// loop through all sides
for (var i = -1; i <= 1; i++) {
for (var j = -1; j <= 1; j++) {
// if within boundaries & there is a mine
if ((isWithinBounds(x + i, y + j)) && (gameBoard[x + i][y + j].hasMine)) {
mineCounter++;
}
}
}
// if there are no mines around it
if (mineCounter === 0) {
for (var i = -1; i <= 1; i++) {
for (var j = -1; j <= 1; j++) {
// click all tiles that are within boundaries & not clicked yet
if ((isWithinBounds(x + i, y + j)) && (!gameBoard[x + i][y + j].isClicked)) {
tileClicked(x + i, y + j);
}
}
}
} else {
// display # of mines
$(".grid tr:nth-child(" + (x + 1) + ") td:nth-child(" + (y + 1) + ")").html(mineCounter);
}
}
// checks if a coordinate is within the boundaries of the game
function isWithinBounds(x, y) {
if ((x >= 0) && (y >= 0) && (x <= (size - 1)) && (y <= (size - 1))) {
return true;
} else {
return false;
}
}
// shows all Mines on board
function showOrHideMines(currentState) {
for (var i = 0; i < size; i++) {
for (var j = 0; j < size; j++) {
if (gameBoard[i][j].hasMine) {
if (currentState) {
$(".grid tr:nth-child(" + (i + 1) + ") td:nth-child(" + (j + 1) + ")").html("X");
$(".grid tr:nth-child(" + (i + 1) + ") td:nth-child(" + (j + 1) + ")").css("background-color", "yellow");
}
else
{
$(".grid tr:nth-child(" + (i + 1) + ") td:nth-child(" + (j + 1) + ")").html("");
$(".grid tr:nth-child(" + (i + 1) + ") td:nth-child(" + (j + 1) + ")").css("background-color", "#C8C8C8");
}
}
}
}
}
// ends game
function endGame() {
showOrHideMines(true);
gameActive = false;
$('td').removeClass('active');
}
function checkGame() {
var counter = 0;
for (var i = 0; i < size; i++) {
for (var j = 0; j < size; j++) {
if ((gameBoard[i][j].hasMine) || (!gameBoard[i][j].isClicked))
{
counter++;
}
}
}
return (counter===10)? true : false;
}
//alerts
function validationAlert(){
if (checkGame()){
alert('Congratulations! You won!');
endGame();
}
else
{
alert('Please Keep Playing');
}
}
// on cilck of table cell
$('td').click(function() {
if (gameActive) {
var col = $(this).parent().children().index($(this));
var row = $(this).parent().parent().children().index($(this).parent());
tileClicked(row, col);
// auto validation
if (checkGame()){validationAlert()};
} else {
alert('Please Start a New Game')
}
});
// new game button
$('li.newGame').click(function() {
gameBoard = newGame(size, mines);
gameActive = true;
godModeOn = false;
$('td').css('background-color','#C8C8C8');
$('td').removeClass('active').addClass('active');
$('td').html('');
});
// god mode button
$('li.godMode').click(function() {
if (godModeOn)
{
showOrHideMines(false);
godModeOn = false;
$(this).removeClass('on');
}
else {
showOrHideMines(true);
godModeOn = true;
$(this).addClass('on');
}
});
//validate button (now done automatically unnecessary)
$('li.validate').click(function() {
validationAlert();
});
});