473,625 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Minesweeper

1 New Member
I have this code in Javascript (minesweeper) but I want to add this article in the code . How ?

---
Expand|Select|Wrap|Line Numbers
  1. class Minefield {
  2.     /* Construct a minefield with the given width, height, and the number of mines. */
  3.     constructor(width, height, mines) {
  4.         /* Sanitize input parameters. */
  5.         width = Number(width);
  6.         height = Number(height);
  7.         mines = Number(mines);
  8.         if (!Number.isInteger(width)) width = 10;
  9.         if (!Number.isInteger(height)) height = 10;
  10.         if (!Number.isInteger(mines)) mines = 10;
  11.  
  12.         /* Validate input parameters. */
  13.         if (width < 1) width = 1;
  14.         if (height < 1) height = 1;
  15.         if (mines > width * height) mines = width * height;
  16.  
  17.         /* Initialize the minefield. */
  18.         this.fieldWidth = width;
  19.         this.fieldHeight = height;
  20.         this.field = new Array(width);
  21.         this.veil = new Array(width);
  22.         for (var x = 0; x < width; x++) {
  23.             this.field[x] = new Array(height);
  24.             this.veil[x] = new Array(height);
  25.             for (var y = 0; y < height; y++) {
  26.                 this.field[x][y] = ' ';
  27.                 this.veil[x][y] = true;
  28.             }
  29.         }
  30.  
  31.         /* Place mines. */
  32.         var placed = 0;
  33.         while (placed < mines) {
  34.             var x = Math.floor(Math.random() * this.width);
  35.             var y = Math.floor(Math.random() * this.height);
  36.             if (this.field[x][y] == ' ') {
  37.                 this.field[x][y] = 'x';
  38.                 placed++;
  39.             }
  40.         }
  41.  
  42.         /* Compute numbers. */
  43.         for (var x = 0; x < width; x++) {
  44.             for (var y = 0; y < height; y++) {
  45.                 if (this.field[x][y] == ' ') {
  46.                     var count = 0;
  47.                     for (var dx = -1; dx <= 1; dx++) {
  48.                         for (var dy = -1; dy <= 1; dy++) {
  49.                             var x0 = x + dx;
  50.                             var y0 = y + dy;
  51.                             if (x0 >= 0 && x0 < width && y0 >= 0 && y0 < height)
  52.                                 count += (this.field[x0][y0] == 'x' ? 1 : 0);
  53.                         }
  54.                     }
  55.                     this.field[x][y] = '' + count;
  56.                 }
  57.             }
  58.         }
  59.     }
  60.  
  61.     /* Return the width of the field. */
  62.     get width() {
  63.         return this.fieldWidth;
  64.     }
  65.  
  66.     /* Return the height of the field. */
  67.     get height() {
  68.         return this.fieldHeight;
  69.     }
  70.  
  71.     /* Return the number of veiled tiles. */
  72.     get veiled() {
  73.         /* Get global variables. */
  74.         var veil = this.veil;
  75.         var width = this.width;
  76.         var height = this.height;
  77.  
  78.         var count = 0;
  79.         for (var x = 0; x < width; x++) {
  80.             for (var y = 0; y < width; y++) {
  81.                 if (veil[x][y]) count++;
  82.             }
  83.         }
  84.  
  85.         return count;
  86.     }
  87.  
  88.     /* Return the number of explosions until now. */
  89.     get explosions() {
  90.         /* Get global variables. */
  91.         var field = this.field;
  92.         var veil = this.veil;
  93.         var width = this.width;
  94.         var height = this.height;
  95.  
  96.         var count = 0;
  97.         for (var x = 0; x < width; x++) {
  98.             for (var y = 0; y < width; y++) {
  99.                 if (!veil[x][y] && field[x][y] == 'x')
  100.                     count++;
  101.             }
  102.         }
  103.  
  104.         return count;
  105.     }
  106.  
  107.     /* Return the visible symbol on a tile. */
  108.     symbol(x, y) {
  109.         /* Sanitize input parameters. */
  110.         var x0 = Number(x);
  111.         var y0 = Number(y);
  112.         if (!Number.isInteger(x0) || !Number.isInteger(y0))
  113.             return '.';
  114.  
  115.         /* Get global variables. */
  116.         var field = this.field;
  117.         var veil = this.veil;
  118.         var width = this.width;
  119.         var height = this.height;
  120.  
  121.         /* Return the symbol. */
  122.         if (x0 >= 0 && x0 < width && y0 >= 0 && y0 < height && !veil[x0][y0])
  123.             return field[x0][y0];
  124.         else
  125.             return '.';
  126.     }
  127.  
  128.     unveil(x, y) {
  129.         /* Sanitize input parameters. */
  130.         var x0 = Number(x);
  131.         var y0 = Number(y);
  132.         if (!Number.isInteger(x0) || !Number.isInteger(y0))
  133.             return;
  134.  
  135.         /* Get global variables. */
  136.         var field = this.field;
  137.         var veil = this.veil;
  138.         var width = this.width;
  139.         var height = this.height;
  140.  
  141.         /* Check input parameters. */
  142.         if (x0 < 0 || x0 >= width || y0 < 0 || y0 >= height) return;
  143.         if (!veil[x0][y0]) return;
  144.  
  145.         /* Unveil tiles. */
  146.         var tiles = [[x0, y0]];
  147.         while (tiles.length > 0) {
  148.             var tile = tiles.pop();
  149.             x0 = tile[0];
  150.             y0 = tile[1];
  151.             veil[x0][y0] = false;
  152.             if (field[x0][y0] == '0') {
  153.                 for (var dx = -1; dx <= 1; dx++) {
  154.                     for (var dy = -1; dy <= 1; dy++) {
  155.                         var x1 = x0 + dx;
  156.                         var y1 = y0 + dy;
  157.                         if (x1 >= 0 && x1 < width && y1 >= 0 && y1 < height && veil[x1][y1])
  158.                             tiles.push([x1, y1]);
  159.                     }
  160.                 }
  161.             }
  162.         }
  163.     }
  164.  
  165.     /* Return representation of the field as string. */
  166.     toString() {
  167.         var output = "";
  168.         for (var y = 0; y < this.height; y++) {
  169.             for (var x = 0; x < this.width; x++) {
  170.                 if (!this.veil[x][y])
  171.                     output += this.field[x][y];
  172.                 else
  173.                     output += '.';
  174.             }
  175.             output += "\n";
  176.         }
  177.         return output;
  178.     }
  179. }
Apr 25 '21 #1
0 1058

Sign in to post your reply or Sign up for a free account.

Similar topics

28
1670
by: Intermouse | last post by:
I have recently purchased an aspi wrapper control for vb. I haven't had much experience with hex and memory addresses and that's my problem. The piece of code that baffles me is: With ASPI1 .HostAdapter = 0 .SCSIID = 4
44
8550
by: JKop | last post by:
You know how the saying goes that *unsigned* overflow is... well.. defined. That means that if you add 1 to its maximum value, then you know exactly what value it will have afterward on all implementations. But then you have signed integers. Let's say a signed integer is set to its maximum positive value. If you add 1 to it, what happens?: A) It's implementation defined what value it will
1
2316
by: AlmasKhan01 | last post by:
Hi I have been for sometime now been working on a MineSweeper program and I'm 90% complete on it it involves a window interface and is quite simple but the issue is not. The program is long(to me) and includes maybe 4 classes and lots of code I was wondering if anyone would mind taking a look at it for me, I know thats a lot to ask of someone that knows nothing of me, but this problem is killing me and I have spent many hours trying to figure...
2
1832
by: blacklife | last post by:
I have explain in file Word below,please help me,i'm newbie in java.Thanks for support! calculatormine.doc - 0.07MB
5
24351
kadghar
by: kadghar | last post by:
Most of the times VBA is used with variables. Objects (such as worksheets, cells or databases) are only used when we read their properties (value, formula, font...) or we use a method (save, open...). But their events are rarely used, and mainly when working with MS Forms. Excel has two very important object types: Workbook and Worksheet, which besides their properties and methods, they have events. The Worksheet's events are not shown in...
1
2305
by: JOhn Donnelly | last post by:
in C# I have a file that has Button name on one line next line is what i want the button to do and the third line has a true or false value on wether to create the button or not. The file is populated from a grid view that part works fine the button name works fine but i cant get the button click to read to work or the true false can anybody help text file looks like this Mahjong C:\Program Files\Microsoft Games\Mahjong\Mahjong.exe TRUE...
0
8182
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8688
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8635
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8494
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7178
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4085
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2614
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1496
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.