Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

minesweeper-sort-of

Name: Anonymous 2012-03-07 1:55

Hey /prog/

I'm planning to prog a little game in C. Sort of like minesweeper.
you fall into minefield (random position), spawned mines (also, random) and you have to navigate out of the minefield.
i know some C - no OOP stuff, console prog.

Name: Anonymous 2012-03-07 2:17

Okay. Get started.

Name: Anonymous 2012-03-07 2:24

yeah, real nice. any good ideas how to? I thought of using ncurses, or is there any easier way?

Name: Anonymous 2012-03-07 2:26

I wrote a minesweeper in JavaScript.

Do it in JavaScript.

Name: bam 2012-03-07 2:32

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>js-mines</title>
    <style type="text/css">

      body {
        font-family: sans-serif;
      }

      table {
        border: 1px solid gray;
        border-collapse: collapse;
      }

      table tr td {
        border: 1px dotted lightgray;
        height: 32px;
        text-align: center;
        width: 32px;
      }

      table tr td button {
        height: 100%;
        width: 100%;
      }

    </style>

    <script type="text/javascript">

      function Field(size, mines) {
        this.size = size;
        this.mines = mines;
        this.initTable();
        this.initMines();
        this.armMines(this.mines);
        this.initCheats();
      }

      Field.prototype.initTable = function () {
        this.table = document.createElement('table');
        for (var row = 0; row < this.size; row++) {
          this.table.insertRow(row);
          for (var col = 0; col < this.size; col++) {
            this.table.rows[row].insertCell(col);
          }
        }
        document.body.appendChild(this.table);
      };

      Field.prototype.initCheats = function () {
        var field = this;
        var showMinesButton = document.createElement('button');
        showMinesButton.innerHTML = 'Show mines';
        showMinesButton.onclick = function () {
          field.showMines();
        };
        document.body.appendChild(showMinesButton);
      };

      Field.prototype.initMines = function () {
        var field = this; // for mine.onclick
        for (var row = 0; row < this.size; row++) {
          for (var col = 0; col < this.size; col++) {
            var mine = document.createElement('button');
            mine.row = row;
            mine.col = col;
            mine.armed = false;
            mine.number = 0;
            mine.onclick = function () {
              if (this.armed) {
                alert('Game over!'); // TODO: handle game over
              } else {
                field.disarmMine(this.row, this.col);
              }
            };
            this.getCell(row, col).appendChild(mine);
          }
        }
      };

      Field.prototype.showMines = function () {
        for (var row = 0; row < this.size; row++) {
          for (var col = 0; col < this.size; col++) {
            if (!!this.getMine(row, col) && this.isArmed(row, col)) {
              this.getMine(row, col).innerHTML = '<span style="color:red;">&times;</span>';
            }
          }
        }
      };

      /**
       * Arm n random mines.
       */
      Field.prototype.armMines = function (n) {
        while (n > 0) {
          var row = Math.floor(Math.random() * this.size);
          var col = Math.floor(Math.random() * this.size);
          if (!this.isArmed(row, col)) {
            this.armMine(row, col);
            n--;
          }
        }
      };

      Field.prototype.armMine = function (row, col) {
        for (var i = -1; i <= 1; i++) {
          if (this.inRange(row + i)) {
            for (var j = -1; j <= 1; j++) {
              if (this.inRange(col + j)) {
                var mine = this.getMine(row + i, col + j);
                if (i == 0 && j == 0) {
                  mine.armed = true;
                } else {
                  mine.number++;
                }
              }
            }
          }
        }
      };

      Field.prototype.inRange = function (n) {
        return n >= 0 && n < this.size;
      };

      Field.prototype.isArmed = function (row, col) {
        return this.getMine(row, col).armed;
      };

      Field.prototype.getCell = function (row, col) {
        return this.table.rows[row].cells[col];
      };

      Field.prototype.getMine = function (row, col) {
        return this.getCell(row, col).firstChild;
      };

      Field.prototype.disarmMine = function (row, col) {
        var mine = this.getMine(row, col);
        if (mine.number) {
          mine.parentNode.replaceChild(document.createTextNode(mine.number), mine);
        } else {
          mine.parentNode.removeChild(mine);
          for (var i = -1; i <= 1; i++) {
            if (this.inRange(row + i)) {
              for (var j = -1; j <= 1; j++) {
                if (this.inRange(col + j)) {
                  if (!!this.getMine(row + i, col + j)) {
                    //this.disarmMine(row + i, col + j);
                  }
                }
              }
            }
          }
        }
      };

      window.onload = function () {
        var field = new Field(8, 10);
      };

    </script>
  </head>
  <body></body>
</html>

Name: Anonymous 2012-03-07 3:03

way to use loops and conditionals like there is no tomorrow.

well, at least there is some sort of encapsulation. good for you.

now do it functional style.

Name: Anonymous 2012-03-07 3:44

>>6
This was just an exercise to learn about prototypes. I typically use jQuery, in which I write slightly more functional style.

Don't change these.
Name: Email:
Entire Thread Thread List