Created
September 17, 2019 06:34
-
-
Save pdobrev/242b9ed994fee8f3ed4ba5ef8b53c0d8 to your computer and use it in GitHub Desktop.
Akshet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// const game = createBoard(width, height, noMines); | |
// game.open(0, 1); // x, y | |
// [ [ '.', '1', '1', ..., '.'], | |
// [ '.', '1', '0', .... '.'], | |
// --> DEAD | |
// --> N | |
// --> | |
// | |
function getRandomInt(max) { | |
return Math.floor(Math.random() * Math.floor(max)); | |
} | |
class Minesweeper { | |
private width = 0 | |
private height = 0 | |
private noMines = 0 | |
private numbers = null | |
private mines = null | |
private status = null | |
constructor(width, height, noMines) { | |
this.width = width | |
this.height = height | |
this.noMines = noMines | |
this.mines = [] | |
this.numbers = [] | |
this.status = [] | |
for (let i = 0; i < height; i+=1) { | |
this.mines.push([]) | |
this.status.push([]) | |
for (let j = 0; j < width; j+=1) { | |
this.mines[i].push(false) | |
this.status[i].push(false) | |
} | |
} | |
const placedMines = {} | |
for (let i = 0; i < noMines;) { | |
const randNum = getRandomInt(width * height) | |
if (placedMines[randNum]) { | |
continue | |
} | |
placedMines[randNum] = true | |
const x = randNum % width | |
const y = Math.floor(randNum / width) | |
this.mines[x][y] = true | |
i += 1 | |
} | |
} | |
printBoard() { | |
for (let i = 0; i < this.height; i+=1) { | |
let printStr = '' | |
for (let j = 0; j < this.width; j+=1) { | |
if(this.status[i][j]) { | |
printStr += this.numbers[i][j] ? `${this.numbers[i][j]}` : ' ' | |
} else { | |
if (this.mines[i][j]) { | |
printStr += '$' | |
} else { | |
printStr += '.' | |
} | |
} | |
} | |
console.log(printStr) | |
} | |
} | |
open(x, y) { | |
const toVisit = [] | |
for () | |
} | |
} | |
const board = new Minesweeper(10, 10, 10) | |
board.printBoard() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment