1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

<head>

    <link rel='stylesheet' type='text/css' href='styles.css' />

    <script type="text/javascript" src="wz_jsgraphics.js"></script>
    <script type="text/javascript" src="underscore-min.js"></script>

    <title>Minesweeper</title>
</head>

<body onload="javascript:init();" oncontextmenu="return false;">

    <div class='text_a'>
        <input type="radio" id="radio1" name="radios_diff" value="false" checked>
        <label for="radio1">easy</label>
        <input type="radio" id="radio2" name="radios_diff" value="true">
        <label for="radio2">medium</label>
        <input type="radio" id="radio3" name="radios_diff" value="false">
        <label for="radio3">hard</label>

        <strong> | </strong>

        <input type="radio" id="radio11" name="radios_size" value="false" checked>
        <label for="radio11">tiny</label>
        <input type="radio" id="radio22" name="radios_size" value="true">
        <label for="radio22">normal</label>
        <input type="radio" id="radio33" name="radios_size" value="false">
        <label for="radio33">huge</label>

        <strong> | </strong>

        <input id="gobutton" type="submit" value="Go!" onclick="new_game()" />
        <a href="code.html" ><input id="gobutton2" type="submit" value="source code"/> </a>

    </div>

    <div id="myCanvas" style="position:absolute;left:0px; top:100px;width:200px;height:200px;z-index:10" oncontextmenu="return false"></div>

    <script language="javascript" type="text/javascript">
    //Global variables
    var xMousePos = 0; // Horizontal position of the mouse on the screen
    var yMousePos = 0; // Vertical position of the mouse on the screen

    var jg = new jsGraphics("myCanvas");
    var plant_mine_button = false;

    var offset_x = 50;
    var offset_y = 100;
    var block_px = 25;
    var height = 25;
    var width = height;

    //percentage of mines:
    var easy = 10;
    var medium = 15;
    var expert = 20;
    var custom = 3;
    var difficulty = "medium";

    var free_click = true;
    var num_of_mines = 0;
    var suspected_mines = 0;

    var mines_arr = new Array();
    var suspected_mines_arr = new Array();

    var board = new Array(width);
    for (var i = 0; i < width; i++) {
        board[i] = new Array(height);
    }


    var board_exposed = new Array(width);
    for (var k = 0; k < width; k++) {
        board_exposed[k] = new Array(height);
    }

    for (var i = 0; i < width; i++) {
        for (var j = 0; j < width; j++) {
            board_exposed[i][j] = "x";
        }
    }

    function clearTiles(i, j) {
        if (i < 0 || j < 0 || i > width || j > width) {
            return; //out of bounds
        }
        if (board_exposed[i][j] == "0") {
            return; //was here already
        }
        if (board[i][j] != "0") {
            return; //not a blank square
        }
        if (board_exposed[i][j] == "f") {
            return; //flag
        }

        board_exposed[i][j] = "0";


        //tests for numbers in blocks surrounding blank tiles:
        try {
            clearTiles(i + 1, j);
            if (board[i + 1][j] != "b" && board[i + 1][j] != "f") {
                board_exposed[i + 1][j] = board[i + 1][j];
            }
        }
        catch (e) {}
        try {
            clearTiles(i + 1, j - 1);
            if (board[i + 1][j - 1] != "b" && board[i + 1][j - 1] != "f") {
                board_exposed[i + 1][j - 1] = board[i + 1][j - 1];
            }
        }
        catch (e) {}
        try {
            clearTiles(i + 1, j + 1);
            if (board[i + 1][j + 1] != "b" && board[i + 1][j + 1] != "f") {
                board_exposed[i + 1][j + 1] = board[i + 1][j + 1];
            }
        }
        catch (e) {}
        try {
            clearTiles(i - 1, j);
            if (board[i - 1][j] != "b" && board[i - 1][j] != "f") {
                board_exposed[i - 1][j] = board[i - 1][j];
            }
        }
        catch (e) {}
        try {
            clearTiles(i - 1, j - 1);
            if (board[i - 1][j - 1] != "b" && board[i - 1][j - 1] != "f") {
                board_exposed[i - 1][j - 1] = board[i - 1][j - 1];
            }
        }
        catch (e) {}
        try {
            clearTiles(i - 1, j + 1);
            if (board[i - 1][j + 1] != "b" && board[i - 1][j + 1] != "f") {
                board_exposed[i - 1][j + 1] = board[i - 1][j + 1];
            }
        }
        catch (e) {}
        try {
            clearTiles(i, j + 1);
            if (board[i][j + 1] != "b" && board[i][j + 1] != "f") {
                board_exposed[i][j + 1] = board[i][j + 1];
            }
        }
        catch (e) {}
        try {
            clearTiles(i, j - 1);
            if (board[i][j - 1] != "b" && board[i][j - 1] != "f") {
                board_exposed[i][j - 1] = board[i][j - 1];
            }
        }
        catch (e) {}
    }


    function generate_mines(diff, exclude_coordinates) {
        if (diff == "easy") {
            num_of_mines = width * height * easy / 100;
        }
        if (diff == "medium") {
            num_of_mines = width * height * medium / 100;
        }
        if (diff == "expert") {
            num_of_mines = width * height * expert / 100;
        }
        if (diff == "custom") {
            num_of_mines = width * height * custom / 100;
        }

        num_of_mines = Math.round(num_of_mines);
        suspected_mines = num_of_mines;

        var n = 0;
        while (n < num_of_mines) {
            var tx = Math.round((Math.random() * (width - 1)));
            var ty = Math.round((Math.random() * (height - 1)));

            var temp_coordinate = makeCoordinate(tx, ty);
            if (_.contains(mines_arr, temp_coordinate) == false && JSON.stringify(temp_coordinate) != JSON.stringify(exclude_coordinates)) {
                storeCoordinate(tx, ty, mines_arr);
                n++;

                board[tx][ty] = "b";
            }
        }
        generate_numbers();
    }

    function generate_numbers() {

        for (var x = 0; x < width; x++) {
            for (var y = 0; y < height; y++) {
                var bomb_counter = 0;

                if (board[x][y] != "b") {
                    //top left
                    try {
                        if (board[x - 1][y - 1] == "b") {
                            bomb_counter++;
                        }
                    }
                    catch (e) {}
                    //middle left
                    try {
                        if (board[x - 1][y] == "b") {
                            bomb_counter++;
                        }
                    }
                    catch (e) {}
                    //bottom left
                    try {
                        if (board[x - 1][y + 1] == "b") {
                            bomb_counter++;
                        }
                    }
                    catch (e) {}
                    //top right
                    try {
                        if (board[x + 1][y - 1] == "b") {
                            bomb_counter++;
                        }
                    }
                    catch (e) {}
                    //middle right
                    try {
                        if (board[x + 1][y] == "b") {
                            bomb_counter++;
                        }
                    }
                    catch (e) {}
                    //bottom right
                    try {
                        if (board[x + 1][y + 1] == "b") {
                            bomb_counter++;
                        }
                    }
                    catch (e) {}
                    //top middle
                    try {
                        if (board[x][y - 1] == "b") {
                            bomb_counter++;
                        }
                    }
                    catch (e) {}
                    //bottom middle
                    try {
                        if (board[x][y + 1] == "b") {
                            bomb_counter++;
                        }
                    }
                    catch (e) {}
                    board[x][y] = bomb_counter;
                }
            }

        }
    }

    function storeCoordinate(xVal, yVal, array) {
        array.push({
            x: xVal,
            y: yVal
        });
    }

    function makeCoordinate(x, y) {
        return {
            x: x,
            y: y
        };
    }


    document.getElementById('myCanvas').onclick = function () {
        doMouseDown();
    }

    document.getElementById('myCanvas').oncontextmenu = function () {
        rightClick();
    }

    document.onmousemove = function (e) {
        e = e || window.event;
        fixPageXY(e);
        xMousePos = e.pageX;
        yMousePos = e.pageY - 100; //this '100' comes from <div id="myCanvas" style="..top:100px;"></div>
    }


    function fixPageXY(e) {
        if (e.pageX == null && e.clientX != null) {
            var html = document.documentElement;
            var body = document.body;

            e.pageX = e.clientX + (html.scrollLeft || body && body.scrollLeft || 0);
            e.pageX -= html.clientLeft || 0;

            e.pageY = e.clientY + (html.scrollTop || body && body.scrollTop || 0);
            e.pageY -= html.clientTop || 0;
        }
    }

    function doMouseDown() {
        mousedown = true;
        //plant_mine_button == false;

        var tx = xMousePos;
        var ty = yMousePos;

        if (tx > offset_x + (width * block_px) + 50 && tx < offset_x + (width * block_px) + 50 + (block_px * 2)) {
            if (ty > (offset_y + (width * block_px)) / 2 && ty < (offset_y + (width * block_px)) / 2 + (block_px * 2)) {
                if (plant_mine_button == false) {
                    plant_mine_button = true;
                    paint();
                    console.log("XX");
                }
                else {
                    plant_mine_button = false;;
                    paint();
                }
            }
        }

        for (var by = 0; by < height; by++) {
            for (var bx = 0; bx < width; bx++) {
                if (tx > (bx * block_px) + offset_x && tx < (bx * block_px) + offset_x + block_px) {
                    if (ty > (by * block_px) + offset_y && ty < (by * block_px) + offset_y + block_px) {
                        if (board_exposed[bx][by] != "f") {
                            if (free_click == true) {
                                var temp_coor = makeCoordinate(bx, by);
                                start_game(difficulty, temp_coor);
                            }
                            if (plant_mine_button == true) {
                                plant_flag(bx, by);
                                plant_mine_button = false;
                                paint();
                            }
                            else {
                                if (board[bx][by] == "b") {
                                    alert("Dead! \n\nx_x");
                                    board_exposed = board;

                                    paint();
                                }
                                if (isNaN(board[bx][by]) == false && board[bx][by] != 0) {
                                    //number now exposed / clicked on a number
                                    board_exposed[bx][by] = board[bx][by];
                                    paint();
                                }
                                if (isNaN(board[bx][by]) == false && board[bx][by] == 0) {
                                    //open all adjacent blank tiles
                                    clearTiles(bx, by);
                                    paint();
                                }
                            }
                            free_click = false;
                        }
                        else if (plant_mine_button == true) {
                            plant_flag(bx, by);
                            plant_mine_button = false;
                            paint();
                        }
                    }
                }
            }
        }
    }

    function plant_flag(bx, by) {
        if (board_exposed[bx][by] == "x") {
            board_exposed[bx][by] = "f";
            suspected_mines--;

            var temp_coordinate = makeCoordinate(bx, by);
            storeCoordinate(bx, by, suspected_mines_arr);

            //check win:
            if (suspected_mines == 0 && num_of_mines != 0) {
                console.log("testing win case");
                var hits = 0;
                for (var p = 0; p < mines_arr.length; p++) {
                    var mines_arr_stringify = JSON.stringify(mines_arr);

                    if (mines_arr_stringify.indexOf(JSON.stringify(suspected_mines_arr[p])) != -1) //compare two 2D arrays:
                    {
                        hits++;
                    }
                }

                if (hits == suspected_mines_arr.length) {
                    alert("You won! \n:-)");
                }
            }

            paint();
            //break;
        }
        else if (board_exposed[bx][by] == "f") {
            board_exposed[bx][by] = "x";
            suspected_mines++;
            suspected_mines_arr.pop();

            paint();
            //break;
        }

    }

    function rightClick() {
        var tx = xMousePos;
        var ty = yMousePos;

        for (var by = 0; by < height; by++) {
            for (var bx = 0; bx < width; bx++) {
                if (tx > (bx * block_px) + offset_x && tx < (bx * block_px) + offset_x + block_px) {
                    if (ty > (by * block_px) + offset_y && ty < (by * block_px) + offset_y + block_px) {
                        plant_flag(bx, by);
                    }
                }
            }
        }
    }

    function init() {
        paint();
    }

    function new_game() {

        var selected_easy = document.getElementById('radio1').checked;
        var selected_medium = document.getElementById('radio2').checked;
        var selected_expert = document.getElementById('radio3').checked;

        var selected_tiny = document.getElementById('radio11').checked;
        var selected_normal = document.getElementById('radio22').checked;
        var selected_huge = document.getElementById('radio33').checked;

        free_click = true;
        num_of_mines = 0;
        suspected_mines = 0;
        mines_arr = [];
        suspected_mines_arr = []
        board = [];

        if (selected_easy == true) {
            difficulty = "easy";
        }
        if (selected_medium == true) {
            difficulty = "medium";
        }
        if (selected_expert == true) {
            difficulty = "expert";
        }

        var size = 0;
        if (selected_tiny == true) {
            size = 10;
        }
        if (selected_normal == true) {
            size = 20;
        }
        if (selected_huge == true) {
            size = 30;
        }

        width = size;
        height = size;

        board = new Array(width);
        for (var i = 0; i < width; i++) {
            board[i] = new Array(height);
        }

        board_exposed = new Array(width);
        for (var k = 0; k < width; k++) {
            board_exposed[k] = new Array(height);
        }

        for (var i = 0; i < width; i++) {
            for (var j = 0; j < width; j++) {
                board_exposed[i][j] = "x";
            }
        }

        paint();
    }

    function start_game(exclude_coordinates) {
        generate_mines(difficulty, exclude_coordinates);
        paint();
    }

    function paint() {
        jg.clear();

        jg.setColor("5E0587");
        jg.setFont("arial", "30px", Font.BOLD);

        if (num_of_mines == 0) {
            jg.drawString("??", (offset_x + (width * block_px)) / 2, offset_y / 2);
        }
        else {
            jg.drawString("" + suspected_mines, (offset_x + (width * block_px)) / 2, offset_y / 2);
        }
        for (var i = 0; i < height; i++) {
            for (var j = 0; j < width; j++) {

                if (board_exposed[i][j] == "x") {
                    jg.drawImage("gfx/tile_closed.png", offset_x + (i * block_px), offset_y + (j * block_px), block_px, block_px);
                }
                if (board_exposed[i][j] == "0") {
                    jg.drawImage("gfx/tile_open2.png", offset_x + (i * block_px), offset_y + (j * block_px), block_px, block_px);
                }

                if (board_exposed[i][j] == "b") {
                    jg.drawImage("gfx/tile_mine3.png", 2 + offset_x + (i * block_px), 2 + offset_y + (j * block_px), block_px - 2, block_px - 2);
                }
                if (isNaN(board_exposed[i][j]) == false && board_exposed[i][j] != 0) {
                    //jg.setColor("000000");
                    jg.drawImage("gfx/tile_number.png", offset_x + (i * block_px), offset_y + (j * block_px), block_px, block_px);
                    jg.setFont("arial", "15px", Font.BOLD);
                    if (board_exposed[i][j] == 1) {
                        jg.setColor("0000CC");
                    }
                    if (board_exposed[i][j] == 2) {
                        jg.setColor("006600");
                    }
                    if (board_exposed[i][j] == 3) {
                        jg.setColor("FF0000");
                    }
                    if (board_exposed[i][j] == 4) {
                        jg.setColor("191919");
                    }
                    if (board_exposed[i][j] == 5) {
                        jg.setColor("CC6600");
                    }
                    if (board_exposed[i][j] == 6) {
                        jg.setColor("CC00FF");
                    }
                    if (board_exposed[i][j] == 7) {
                        jg.setColor("FFFFFF");
                    }
                    if (board_exposed[i][j] == 8) {
                        jg.setColor("333399");
                    }
                    if (board_exposed[i][j] == 9) {
                        jg.setColor("FFFF00");
                    }

                    jg.drawString("" + board[i][j], 8 + offset_x + (i * block_px), 5 + offset_y + (j * block_px));
                }
                if (board_exposed[i][j] == "f") {
                    jg.drawImage("gfx/tile_flag.png", 2 + offset_x + (i * block_px), 2 + offset_y + (j * block_px), block_px - 2, block_px - 2);
                }
            }
        }
        jg.setColor("000000");
        for (var i = 0; i <= width; i++) {
            jg.drawLine(offset_x + (i * block_px), offset_y, offset_x + (i * block_px), offset_y + (height * block_px));
        }
        for (var i = 0; i <= height; i++) {
            jg.drawLine(offset_x, offset_y + (i * block_px), offset_x + (width * block_px), offset_y + (i * block_px));
        }
        jg.setColor("00CC66");

        if (plant_mine_button == false) {
            jg.drawImage("gfx/tile_flag.png", offset_x + (width * block_px) + 50, (offset_y + (width * block_px)) / 2, block_px * 2, block_px * 2);
        }
        else {
            jg.drawImage("gfx/tile_flag_sel.png", offset_x + (width * block_px) + 50, (offset_y + (width * block_px)) / 2, block_px * 2, block_px * 2);
        }
        jg.paint();
    }
</script>
</body>
</html>