123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- var COLS;
- var ROWS;
- var MINES;
- var W;
-
- const GAME_STATE = {
- DEFEAT: 1,
- VICTORY: 2,
- ONGOING: 3,
- STOPPED: 4
- }
-
- function setup() {
- newGame();
- }
-
- function newGame() {
- let params = gatherMenuValues();
-
- COLS = params.COLUMNS;
- ROWS = params.ROWS;
- MINES = params.MINES;
- W = params.CELLSIZE;
- console.log(COLS, ROWS, MINES);
- this.grid = new Grid(COLS, ROWS, MINES);
- this.nbCellsToWin = COLS * ROWS - MINES;
- this.gamestate = GAME_STATE.ONGOING;
-
- createCanvas(COLS * W, ROWS * W);
- //resizeCanvas(COLS * W, ROWS * W);
- needUpdate = true;
- }
-
- function mousePressed(e) {
- if (this.gamestate != GAME_STATE.ONGOING) return;
-
- let cellX = floor(mouseX / W);
- let cellY = floor(mouseY / W);
-
- if (!this.grid.inbounds(cellX, cellY)) return;
-
- let clickedCell = this.grid.cell(cellX, cellY);
-
- if (mouseButton === LEFT) {
- handleLeftClickAtCell(clickedCell);
- }
-
- if (mouseButton === RIGHT) {
- clickedCell.toggleFlag();
- }
-
- needUpdate = true;
- return false;
- }
-
- function gatherMenuValues() {
- let menu_columns = parseInt(document.getElementById("columns").value);
- let menu_rows = parseInt(document.getElementById("rows").value);
- let menu_mines = parseInt(document.getElementById("mines").value);
- let menu_cellsize = parseInt(document.getElementById("cellsize").value);
-
- return {
- COLUMNS: menu_columns,
- ROWS: menu_rows,
- MINES: menu_mines,
- CELLSIZE: menu_cellsize
- };
- }
-
- function handleLeftClickAtCell(cell) {
- let nbRevealed = this.grid.revealCell(cell);
-
- if (nbRevealed == -1) { // hit mine
- defeat();
- return;
- }
-
- this.nbCellsToWin -= nbRevealed;
- if (this.nbCellsToWin == 0) {
- victory();
- }
- }
-
- function victory() {
- grid.fullReveal();
- this.gamestate = GAME_STATE.VICTORY;
- }
-
- function defeat() {
- grid.fullReveal();
- this.gamestate = GAME_STATE.DEFEAT;
- }
-
- function displayMessage(message, color) {
- textSize(W);
- textAlign(CENTER, CENTER);
- fill(200);
- stroke(1);
- strokeWeight(2);
-
- let textW = textWidth(message);
- let textH = textAscent(message);
- let padding = 10;
-
- rect (
- width / 2 - padding,
- height / 2 - textH / 2 - padding,
- 2 * padding + textW,
- 2 * padding + textH
- );
-
- noFill();
- noStroke();
- fill(color);
- text(message, width / 2 + textW / 2, height / 2);
- }
-
- function displayVictoryMessage() {
- displayMessage('Victory !', 'green');
- }
-
- function displayDefeatMessage() {
- displayMessage('Perdu !', 'red');
- }
-
- var needUpdate = true; // optimization trick
- function draw() {
- if (!needUpdate) return;
- background(0);
-
- drawGrid();
-
- if (this.gamestate == GAME_STATE.DEFEAT) displayDefeatMessage();
- if (this.gamestate == GAME_STATE.VICTORY) displayVictoryMessage();
- needUpdate = false;
- }
-
- function drawGrid() {
- for (let i = 0; i < COLS; i++) {
- for (let j = 0; j < ROWS; j++) {
- drawCell(this.grid.cell(i, j));
- }
- }
- }
-
- function drawCell(cell) {
- if (cell.revealed) {
- drawRevealedCell(cell)
- } else {
- drawHiddenCell(cell);
- }
- }
-
- function drawRevealedCell(cell) {
- let drawX = cell.x * W;
- let drawY = cell.y * W;
-
- fill(255);
- strokeWeight(0);
-
- rect (drawX, drawY, W, W);
-
- if (cell.hasMine) {
- drawMine(drawX, drawY, W);
- }
-
- if (cell.weight > 0) {
- drawCellMinesNumber(cell);
- }
- }
-
- function drawCellMinesNumber(cell) {
- let drawX = cell.x * W;
- let drawY = cell.y * W;
-
- textAlign(CENTER, CENTER);
- textSize(W / 2);
- fill(0);
-
- text (cell.weight, drawX, drawY, W, W);
- }
-
- function drawHiddenCell(cell) {
- let drawX = cell.x * W;
- let drawY = cell.y * W;
-
- stroke(0);
- strokeWeight(1);
- fill(155);
-
- rect (drawX, drawY, W, W);
-
- if (cell.flagged) {
- drawFlag(drawX, drawY, W);
- }
- }
-
- function drawFlag(x, y, w) {
- stroke(0);
- strokeWeight(1);
-
- line (
- x + w * 0.3,
- y + w * 0.2,
- x + w * 0.3,
- y + w * 0.8,
- );
-
- fill('red');
- triangle(
- x + w * 0.3,
- y + w * 0.2,
- x + w * 0.7,
- y + w * 0.35,
- x + w * 0.3,
- y + w * 0.5
- );
- }
-
- let sin60 = Math.sin(Math.PI / 3); // optimization
- function drawMine(x, y, w) {
- let r = w * 0.4;
-
- stroke(0);
- strokeWeight(1);
- fill(0);
-
- circle (
- r * 1.2 + x,
- r * 1.2 + y,
- r * 1.4
- );
-
- triangle(
- r * 1.7 + x, r * (sin60 + 1.2) + y,
- r * 1.7 + x, r * (1.2 - sin60) + y,
- r * 0.2 + x, r * 1.2 + y
- );
-
- triangle (
- r * 0.7 + x, r * (sin60 + 1.2) + y,
- r * 0.7 + x, r * (1.2 - sin60) + y,
- r * 2.2 + x, r * 1.2 + y
- );
-
-
- }
|