-
Notifications
You must be signed in to change notification settings - Fork 0
/
index2.js
166 lines (143 loc) · 4.01 KB
/
index2.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
var mineField = [];
var bombList = [];
class cell {
constructor(cell){
this.isBomb = false;
this.bombsClose = 0;
this.isOpen = false;
}
}
//Create MineField
function createMineField(){
let thisField = [];
for(let i = 0; i < 10 ; i++){
thisField[i] = [];
for (let j = 0; j < 10; j ++){
thisField[i][j] = new cell;
}
}
return thisField;
}
//Print Field
function printField(field){
let headers = " ";
for (let i = 0; i < field.length ; i++){
headers += i + " ";
}
console.log(headers);
for (let i = 0; i < field.length ; i++){
let row = i + " ";
for(let j = 0; j <field[i].length; j++){
if(field[i][j].isOpen === true){
if(field[i][j].isBomb === true){
row += "B " ;
}
else {
row += field[i][j].bombsClose + " ";
}
}
else {
row += " " ;
}
}
console.log(row)
}
}
//Place Bombs
function placeBombs(field){
for(let i = 0; i < 10 ; i++){
let bombPlaced = false;
while(!bombPlaced){
let coord1 = Math.floor(Math.random() * field.length);
let coord2 = Math.floor(Math.random() * field.length);
if(field[coord1][coord2].isBomb === true){
bombPlaced = false;
}
else {
field[coord1][coord2].isBomb = true;
bombPlaced = true;
}
console.log(field[coord1][coord2]);
console.log(coord1,coord2);
bombList.push([coord1,coord2]);
}
}
}
function isValidCoord(coord1,coord2,arr){
console.log("Valid Coord Check :" + arr.length);
if(coord1 < 0 || coord2 < 0 || coord1 >= arr.length || coord2 >= arr.length){
return false;
}
return true;
}
//Look For Bombs
function lookForBombs(){
let checkCoord = [-1,0,1];
for(let coord of bombList){
let coord1 = coord[0];
let coord2 = coord[1];
for(let rowCheck of checkCoord){
for(let colCheck of checkCoord){
let x = coord1 + rowCheck;
let y = coord2 + colCheck;
// console.log(coord);
// console.log(x + " " + y);
// console.log(typeof(colCheck));
// console.log(isValidCoord(x,y,mineField));
if(isValidCoord(x,y,mineField)){
// console.log(mineField[x][y]);
mineField[x][y].bombsClose += 1;
}
}
}
}
}
function checkPlay(coord, grid) {
if (coord.length !== 3 || coord.indexOf(",") === -1) {
return false;
} else if (coord[0] > grid.length || coord[2] > grid.length) {
return false;
} else if (grid[coord[0]][coord[2]].isOpen === true){
return false;
}
return true;
}
function openCell(grid,coord){
grid[coord[0]][coord[2]].isOpen = true;
return grid[coord[0]][coord[2]].isBomb;
}
function getNeighbours(coord){
let checkCoord = [-1,0,1];
}
mineField = createMineField();
printField(mineField);
placeBombs(mineField);
printField(mineField);
lookForBombs();
printField(mineField);
let totalBombs = 10;
let lifes = 3;
let gameOver = false;
while (!gameOver){
let validPlay = false;
let play ;
while(!validPlay){
play = prompt("Choose a coordenate separated by comma");
if (checkPlay(play,mineField)){
validPlay = checkPlay(play,mineField);
}
else {console.log(`Not a valid coordenate!`);
}
}
console.log(`Valid Play - ${play}`);
console.log(`You have ${lifes} lifes left!!! \n`);
if (openCell(mineField,play) === true){
lifes -= 1;
console.log(`BOOOOOOOM you lost 1 life - ${lifes} left\n`);
}
printField(mineField);
if (lifes === 0){
console.log(`GAME OVER!`);
gameOver = true;
}
}