Comutare navigație
pbInfo.ro
Probleme
Probleme - clasa a IX-a
Probleme - clasa a X-a
Probleme - clasa a XI-a
Probleme din concursuri
Căutare problemă
Exerciții
Programează cu Blockly
Desenează cu Processing
Exersează SQL
Soluţii
Resurse
Resurse pentru clasa a IX-a
Resurse pentru clasa a X-a
Resurse pentru clasa a XI-a
Subiecte bacalaureat
Ajutor
Autentificare
Înregistrare
×
Autentificare
Utilizator sau email
Parola
Acest site foloseşte cookies. Navigând în continuare, vă exprimaţi acordul asupra folosirii cookie-urilor.
Îti place pbInfo? Atunci acceptă-l cu totul! Dezactivează modulul de blocare a reclamelor!
×
Lista scripturi
Script Nou
Ajutor
"Un inceput de Minesweeper" - Processing
ID
Autor
Duplicat din
Ultima modificare
#4472
Takacs Robert (RoTak)
-
Miercuri, 16 iun 2021, 17:30
let game; let di = [ 1, 1, 1, 0, 0, -1, -1, -1]; let dj = [-1, 0, 1, -1, 1, -1, 0, 1]; let HIDDEN = 0, REVEALED = 1; function setup() { createCanvas(400, 400); textAlign(CENTER, CENTER); game = new Game(); } let DIM = 400; let SIZE = 10; let SQ = DIM / SIZE; class Space { constructor(x, y) { this.x = x; this.y = y; this.state = 0; this.bomb = false; this.val = 0; this.hovering = 0; } show() { push(); translate(this.x, this.y); stroke(0); strokeWeight(2); fill("#aaaaaa"); if(this.state == HIDDEN && !this.hovering) { fill("#aaaaaa"); stroke("#000000"); } if(this.state == HIDDEN && this.hovering) { fill("#bbbbbb"); stroke("#222222"); } if(this.state == REVEALED && !this.hovering && !this.bomb) { fill("#e0d6c1"); stroke("#000000"); } if(this.state == REVEALED && this.hovering && !this.bomb) { fill("#e0d6c1"); stroke("#222222"); } if(this.state == REVEALED && this.bomb) { fill("#782524"); stroke("#000000"); } rect(0, 0, SQ, SQ); if(this.state == 1 && !this.bomb) if(this.val) { push(); translate(20, 20) text(this.val, 0, 0) pop(); } pop(); } hover() { this.hovering = (mouseX > this.x && mouseX < this.x + SQ && mouseY > this.y && mouseY < this.y + SQ); return; } } class Game { constructor() { this.table = []; for(let i = 0; i < SIZE; ++i) this.table[i] = new Array(SIZE); for(let i = 0; i < SIZE; ++i) for(let j = 0; j < SIZE; ++j) this.table[i][j] = new Space(i * SQ, j * SQ); this.active = false; } setupBombs(nrBombs, xs, ys) { this.active = true; let count = 0; let x = 0, y = 0; while(count != nrBombs) { x = floor(random(10)); y = floor(random(10)); let aroundStart = false; for(let k = 0; k < 8; ++k) { let iv = xs + di[k]; let jv = ys + dj[k]; if(iv < 0 || jv < 0 || iv >= SIZE || jv >= SIZE) continue; if(x == iv && y == jv) aroundStart = true; } if(x == xs || y == ys) aroundStart = true; if(!this.table[x][y].bomb && !aroundStart) { this.table[x][y].bomb = true; count++; } } this.calcValues(); } calcValues() { for(let i = 0; i < SIZE; ++i) for(let j = 0; j < SIZE; ++j) { this.table[i][j].val = 0; for(let k = 0; k < 8; ++k) { let iv = i + di[k]; let jv = j + dj[k]; if(iv < 0 || jv < 0 || iv >= SIZE || jv >= SIZE) continue; this.table[i][j].val += this.table[i + di[k]][j + dj[k]].bomb; } } } show() { for(let i = 0; i < SIZE; ++i) for(let j = 0; j < SIZE; ++j) this.table[i][j].show(); } update() { for(let i = 0; i < SIZE; ++i) for(let j = 0; j < SIZE; ++j) this.table[i][j].hover(); for(let i = 0; i < SIZE; ++i) for(let j = 0; j < SIZE; ++j) if(this.table[i][j].hovering) ; } click() { for(let i = 0; i < SIZE; ++i) for(let j = 0; j < SIZE; ++j) if(this.table[i][j].hovering) if(this.table[i][j].state == HIDDEN) { this.table[i][j].state = REVEALED; if(this.active == false) this.setupBombs(15, i, j); } } } function draw() { background(220); game.update(); game.show(); } function mousePressed() { if(mouseButton == LEFT) game.click(); }
Duplicare
Executare
Cod
×
Cod HTML
<iframe sandbox="allow-scripts" src="/p5js/index.php?id=4472" style="width:408px; height:408px;border:solid 1px gray; overflow: scroll;"></iframe>
×
Duplicare script
Denumirea noului script
Du-te sus!