forked from Indrabay/minesweeper-terminal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminesweeper.js
171 lines (153 loc) · 4.63 KB
/
minesweeper.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
const readline = require("readline-sync")
const NEIGHBOR = [
{ row: -1, col: -1 },
{ row: -1, col: 0 },
{ row: -1, col: 1 },
{ row: 0, col: 1 },
{ row: 1, col: 1 },
{ row: 1, col: 0 },
{ row: 1, col: -1 },
{ row: 0, col: -1 },
]
function initBoard(length, width) {
let board = []
for (let i = 0; i < length; i++) {
board[i] = []
for (let j = 0; j < width; j++) {
board[i].push({
isBomb: false,
number: 0,
isOpen: false,
showBoard: "-",
showCheat: "-"
})
}
}
return board
}
function initBomb(board, bombs) {
let row = Math.floor(Math.random() * board.length)
let col = Math.floor(Math.random() * board[0].length)
while (bombs > 0) {
if (board[row][col].isBomb == false) {
board[row][col].isBomb = true
board[row][col].showCheat = "*"
bombs--
} else {
row = Math.floor(Math.random() * board.length)
col = Math.floor(Math.random() * board[0].length)
}
}
}
function checkTile(board, row, col, isLoop = false) {
if (row < 0 || row >= board.length || col < 0 || col >= board[0].length) {
return isLoop ? 1 : console.log("di luar papan")
}
if (board[row][col].isOpen === true) {
return isLoop ? 1 : console.log("sudah dibuka")
}
if (board[row][col].isBomb === true) {
return 4
}
let bombCount = 0
NEIGHBOR.forEach(elm => {
let newRow = row + elm.row
let newCol = col + elm.col
if (newRow >= 0 && newRow < board.length && newCol >= 0 && newCol < board[0].length) {
if (board[newRow][newCol].isBomb === true) {
bombCount++
}
}
})
board[row][col].showBoard = bombCount
board[row][col].isOpen = true
if (bombCount !== 0) {
return 1
} else {
NEIGHBOR.forEach(elm => {
checkTile(board, row + elm.row, col + elm.col, true)
})
}
return 1
}
function checkInitBoard(length, width) {
return length >= 1 && width >= 1
}
function checkInitBomb(board, bombs) {
return board.length * board[0].length - 2 >= bombs
}
function printBoard(board, cheat = false) {
let ListBomb = []
let stringToPrint = "$".padEnd(3, " ")
for (let i = 0; i < board[0].length; i++) {
stringToPrint += `${i + 1}`.padEnd(3, " ")
}
stringToPrint += "\n"
for (let i = 0; i < board.length; i++) {
stringToPrint += `${i + 1}`.padEnd(3, " ")
for (let j = 0; j < board[0].length; j++) {
stringToPrint += cheat === true ? `${board[i][j].showCheat}`.padEnd(3, " ") : `${board[i][j].showBoard}`.padEnd(3, " ")
if (board[i][j].isBomb === true) {ListBomb.push((i+1)+","+(j+1))}
}
stringToPrint += "\n"
}
console.log(stringToPrint)
if (cheat === true) {console.log(ListBomb)}
}
function countOpened(board) {
let opened = 0
board.forEach(elm => {
elm.forEach(inElm => {
if (inElm.isOpen === true) {
opened++
}
})
})
return opened
}
function main() {
// initialization game
let boardBound = readline.question("Masukkan ukuran papan panjang, lebar: ")
boardBound = boardBound.split(",")
let statusBoard = checkInitBoard(Number(boardBound[0]), Number(boardBound[1]))
// loop init board
while (statusBoard === false) {
boardBound = readline.question("Masukkan ukuran papan panjang, lebar: ")
boardBound = boardBound.split(",")
statusBoard = checkInitBoard(Number(boardBound[0]), Number(boardBound[1]))
}
let board = initBoard(Number(boardBound[0]), Number(boardBound[1]))
let bombQuantity = readline.question("Masukkan jumlah bomb: ")
let statusBomb = checkInitBomb(board, Number(bombQuantity))
// loop init bomb
while (statusBomb === false) {
bombQuantity = readline.question("Masukkan jumlah bomb: ")
statusBomb = checkInitBomb(board, Number(bombQuantity))
}
initBomb(board, Number(bombQuantity))
// main loop
let notEnd = true
let bombs = Number(bombQuantity)
while (notEnd) {
printBoard(board)
let coordinat = readline.question("Masukkan koordinat baris,colom: ")
if (coordinat.toLowerCase() === "cheat") {
printBoard(board, true)
} else {
coordinat = coordinat.split(",")
let statusTile = checkTile(board, Number(coordinat[0]) - 1, Number(coordinat[1]) - 1)
if (statusTile === 4) {
notEnd = false
console.log("Kamu kalah, sailahkan coba kembali dengan command node server.js")
} else if (statusTile === 1) {
let openedTile = countOpened(board)
if (board.length * board[0].length - openedTile === bombs) {
notEnd = false
console.log("Kamu berhasil, mau main lagi?")
}
}
}
}
}
let app = { initBoard, initBomb, checkInitBoard, checkInitBomb, countOpened, main }
module.exports = app