-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphics2.js
212 lines (192 loc) · 7.43 KB
/
graphics2.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
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
204
205
206
207
208
209
210
211
212
var clickedArray = []
var numMarkedSquares = 0;
var renderer = {
x: 0,
y: 0,
width: 10000,
height: 1000,
Background: "black",
Boardx: 0,
Boardy: 0,
Boardwidth: 500,
Boardheight: 500,
squareColor: "gray",
Squaresize:25
}
function CreateElement(type, path, attributes, text)
{
const parent = document.querySelector("#" + path);
const el = document.createElement(type);
if(text != undefined) {
const node = document.createTextNode(text);
el.appendChild(node);
}
for(var attribute in attributes)
{
el.setAttribute(attribute, attributes[attribute]);
}
parent.appendChild(el);
return "#" + attributes["id"];
}
function opensquare(row, col) {
document.getElementById("BR" + row + "C" + col).style.display = "none";
clickedArray[col][row] = 1;
}
function SetUpBoard() {
var size = renderer.Squaresize;
for (let i = 0; i < game.height; i++) {
clickedArray.push([])
for (let j = 0; j < game.length; j++) {
CreateElement("div", "board", {class: "sq", id: "SR" + j + "C" + i, style: "background-color: "+renderer.squareColor+"; width:" + size +"px; height:" + size +"px; position:absolute; left: " + j * size + "px; top: "+ i * size +"px;"}, BoardArray[i][j]);
CreateElement("button", "board", {oncontextmenu: "markAsFlag("+j +", " + i+")", class: "but", id: "BR" + j + "C" + i, onclick: "clicked("+j +", " + i+")", style: "width:" + size +"px; height:" + size +"px; position:absolute; left: " + j * size + "px; top: "+ i * size +"px;"}, undefined);
clickedArray[i].push(0);
}
}
}
var sqauresToOpen = []
function clicked(row, col) {
if (!game.gameStarted) {
game.gameStarted = true;
}
if(document.getElementById("BR" + row + "C" + col).style.backgroundColor != "red" && game.gameOver == false && MarkingOn == false && document.getElementById("BR" + row + "C" + col).innerHTML == "") {
var clickedSquare = BoardArray[col][row];
opensquare(row, col);
if(clickedSquare == " ") {
openClickedOnZero(row, col);
var i = 0;
while(sqauresToOpen.length > i) {
openClickedOnZero(sqauresToOpen[i][0], sqauresToOpen[i][1]);
// console.log(sqauresToOpen[i])
// sqauresToOpen.splice(1);
i++;
}
sqauresToOpen.splice(0)
}
else if(clickedSquare == "💣") {
lost();
}
if(checkifwon()) {
win();
}
}
else if(MarkingOn == true && document.getElementById("BR" + row + "C" + col).style.backgroundColor != "red" && document.getElementById("BR" + row + "C" + col).innerHTML == "") {
markAsFlag(row, col);
}
else if(document.getElementById("BR" + row + "C" + col).style.backgroundColor = "red" && document.getElementById("BR" + row + "C" + col).innerHTML == "") {
document.getElementById("BR" + row + "C" + col).innerHTML = "?";
document.getElementById("BR" + row + "C" + col).style.backgroundColor = ''
}
else if(document.getElementById("BR" + row + "C" + col).innerHTML == "?") {
document.getElementById("BR" + row + "C" + col).innerHTML = "";
}
}
function open(row, col) {
var clickedSquare = BoardArray[col][row];
if(clickedSquare == " ") {
sqauresToOpen.push([row, col]);
}
opensquare(row, col);
}
function openClickedOnZero(row, col) {
var Checks = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]]
//----------------
for (let k = 0; k < Checks.length; k++) {
var xSquare = row + Checks[k][1];
var ySquare = col + Checks[k][0];
if(!(xSquare < 0 || xSquare > game.length - 1 || ySquare < 0 || ySquare > game.height - 1)) {
if(clickedArray[ySquare][xSquare] == " ") {
open(xSquare, ySquare);
}
}
}
}
var allButtons;
function markAsFlag(row, col) {
if(game.gameOver == false) {
if (!game.gameStarted) {
game.gameStarted = true;
}
if(document.getElementById("BR" + row + "C" + col).style.backgroundColor == "red") {
numMarkedSquares--;
document.getElementById("BR" + row + "C" + col).style.backgroundColor = '';
}
else {
numMarkedSquares++;
document.getElementById("BR" + row + "C" + col).style.backgroundColor = "red";
}
}
}
function lost() {
allButtons = document.querySelectorAll(".but");
allButtons.forEach(i => {
i.style.display = "none";
});
var sq = document.querySelectorAll(".sq");
for (let i = 0; i < game.height; i++) {
for (let j = 0; j < game.length; j++) {
if (BoardArray[i][j] == "💣") {
sq[game.length * i + j].style.backgroundColor = "red";
}
}
}
document.querySelector("#loseButton").style.left = window.innerWidth/2 - 80 + "px";
document.querySelector("#loseButton").style.top = window.innerHeight/2 - 7.5 + "px";
document.querySelector("#loseButton").style.display = "block";
game.gameWon = false;
gameOver();
// console.log(allButtons)
// allButtons.style.display = "none";
}
function checkifwon() {
for (let i = 0; i < clickedArray.length; i++) {
for (let j = 0; j < clickedArray[i].length; j++) {
if (clickedArray[i][j] == 1) {
}
else if(BoardArray[i][j] == "💣"){
}
else {
return false;
}
}
}
return true;
}
function win() {
// alert("you win");
game.gameWon = true;
document.querySelector("#game-time").innerHTML = game.time;
var sq = document.querySelectorAll(".sq");
sq.forEach(i => {
i.style.backgroundColor = "green";
});
gameOver();
}
function gameOver() {
game.gameOver = true;
var winCount = localStorage.getItem(game.gameMode + "-WinCount");
var Completedgames = localStorage.getItem(game.gameMode + "-CompletedGames");
var totalTime = localStorage.getItem(game.gameMode + "-TotalTime");
var fastestTime = localStorage.getItem(game.gameMode + "-FastestTime");
var totalWinTime = localStorage.getItem(game.gameMode + "-TotalWinTime");
if(winCount == null) {winCount = 0;}
if(Completedgames == null) {Completedgames = 0;}
if(totalTime == null) {totalTime = 0;}
if(fastestTime == null) {fastestTime = Infinity;}
if(totalWinTime == null) {totalWinTime = 0;}
totalTime = parseInt(totalTime) + game.time;
Completedgames = parseInt(Completedgames) + 1
if (game.gameWon === true) {
winCount = parseInt(winCount) + 1;
if(game.time < fastestTime) {
fastestTime = game.time
}
totalWinTime = parseInt(totalWinTime) + game.time;
// console.log(totalWinTime)
}
localStorage.setItem(game.gameMode + "-WinCount", winCount);
localStorage.setItem(game.gameMode + "-CompletedGames", Completedgames);
localStorage.setItem(game.gameMode + "-TotalTime", totalTime);
localStorage.setItem(game.gameMode + "-FastestTime", fastestTime);
localStorage.setItem(game.gameMode + "-TotalWinTime", totalWinTime);
updateMenu();
}