-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.js
114 lines (98 loc) · 2.74 KB
/
controller.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
const getBoard = function () {
return document.getElementById("board");
};
const generateBoard = function(grid) {
let board=getBoard();
for (var rowNumber = 0; rowNumber < grid; rowNumber++) {
row = board.insertRow();
for (var cellNumber = 0; cellNumber < grid; cellNumber++) {
numberOfCell = (rowNumber * grid) + cellNumber + 1;
cell = row.insertCell();
cell.id = numberOfCell;
cell.innerText = numberOfCell;
};
};
};
const getPlayerMove = function() {
return +event.target.id;
};
const displayMessage = function(message) {
let messageBox = document.getElementById("messageBox");
messageBox.innerText = message;
};
const resetBoard=function() {
let board = getBoard();
board.removeEventListener("click", executeGame);
board.innerHTML = "";
};
const stopGame = function() {
resetBoard();
let restartButton = document.getElementById('restartButton');
restartButton.className = "showButton";
};
const updateBoard = function(currentMove, color) {
let currentMoveCell = document.getElementById(currentMove);
currentMoveCell.style.backgroundColor = color;
};
const updateChance = function(chances){
let chancesBox = document.getElementById("chanceBox");
chancesBox.innerText = chances;
}
const hideStartButton = function(){
let startButton = document.getElementById("startButton");
startButton.className = "hideButton";
}
const showChances = function(){
let chances = document.getElementById("chanceContainer");
chances.className = "show";
}
// =============>>>>>>>> controller <<<<<<<<<<<<=============///////
const gridSize = 10;
const game = new MineField(gridSize);
const executeResultCondition = function(currentMove) {
if (game.isGameOver()) {
displayMessage("you lost");
stopGame();
};
if (game.hasPlayerWon(currentMove)) {
console.log("hii");
displayMessage("you won");
stopGame();
};
return;
};
const isFirstMove = function(currentMove) {
return game.isFirstMove() && currentMove <= 10;
};
const executeCurrentMove = function(currentMove) {
if (game.isBomb()) {
updateBoard(currentMove, "red");
updateChance(game.actionOnBomb());
} else {
game.gameInPlay();
};
return;
};
const executeGame = function() {
let currentMove = getPlayerMove();
game.updateCurrentMove(currentMove);
updateBoard(currentMove, "green");
if (isFirstMove(currentMove)) {
game.startGame(currentMove);
} else {
executeCurrentMove(currentMove);
};
executeResultCondition(currentMove);
return;
};
const restartGame = function(){
document.location.reload();
};
const startGame = function() {
hideStartButton()
updateChance(game.chances)
showChances()
let board = getBoard();
generateBoard(game.gridSize);
board.addEventListener("click", executeGame);
};