Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let size = 40
- let edge = 3
- let cells = []
- class Cell {
- constructor(x, y) {
- this.x = x
- this.y = y
- this.hidden = true
- }
- draw() {
- if (this.hidden) {
- fill(200, 200, 200)
- triangle(this.x, this.y,
- this.x + size, this.y,
- this.x, this.y + size)
- fill(150, 150, 150)
- triangle(this.x + size, this.y + size,
- this.x + size, this.y,
- this.x, this.y + size)
- fill(175, 175, 175)
- square(this.x + edge, this.y + edge,
- size - 2 * edge)
- } else {
- noFill()
- stroke(175)
- strokeWeight(0.5)
- square(this.x, this.y,
- size)
- noStroke()
- }
- }
- show() {
- this.hidden = false
- }
- }
- class Mine extends Cell {
- constructor(x, y) {
- super(x, y)
- this.observers = []
- }
- addObserver(obs) {
- this.observers.push(obs)
- }
- show() {
- super.show()
- for (let obs of this.observers) {
- if (obs.hidden)
- obs.show()
- }
- }
- }
- class Model {
- constructor(w, h, n=10) {
- this.cells = []
- this.mines = []
- for (let i = 0; i < w; i+=size) {
- this.cells.push([])
- for (let j = 0; j < h; j+=size)
- this.cells[i / size].push(new Cell(i, j))
- }
- let i = 0
- while (i < n) {
- let x = Math.floor(random() * w / size)
- let y = Math.floor(random() * h / size)
- let bx = x * size
- let by = y * size
- if (!(this.cells[x][y] instanceof Mine)) {
- this.cells[x][y] = new Mine(bx, by)
- for (let mine of this.mines) {
- mine.addObserver(this.cells[x][y])
- this.cells[x][y].addObserver(mine)
- }
- this.mines.push(this.cells[x][y])
- i++
- }
- }
- }
- }
- let modelo
- function mousePressed() {
- let x = Math.floor(mouseX / size)
- let y = Math.floor(mouseY / size)
- if (x < modelo.cells.length && y < modelo.cells[0].length)
- modelo.cells[x][y].show()
- }
- function setup() {
- createCanvas(400, 400);
- noStroke()
- modelo = new Model(width, height)
- }
- function draw() {
- background(205)
- for (let row of modelo.cells) {
- for (let cell of row)
- cell.draw()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement