Realize the classic game in memory - Minesweeper

Abstract: "Minesweeper" is an intellectual game that is popular in school computer rooms. The goal of the game is to find all the non-raid squares in the shortest time according to the numbers that appear in the clicked squares. This article uses the JS language to simply complete the core functions of the minesweeper game.

One, the rules of the game

  1) Click on the minefield, and it will prompt the game to fail.

  2) Click to the warning area and open the warning area (this article divides the grids in the game into non-mine areas, mine areas, and warning areas).

  3) Click on the no-mine area, open the clicked area outward, and expand outward until it hits the warning area. (Warning area as boundary)

 

2. Core functions and realization ideas

1) Generate a map

  First generate a N*M blank map, with 0 indicating no minefield. Use a two-dimensional array to store.

2) Fill mines

  We set a 10*10 map, and the number of mines set is M*N/10. Rays must be uniformly distributed. It is easier to realize the random distribution of mines in the rectangle. The random number seed generated by the computer is used to generate the coordinates XY. We set the corresponding position in the array to -1, which means that the mine is buried.

3) Fill in the warning area (ray edge numbers)

  For each mine, the grids in the eight directions with him as the center will record that there is a mine next to them, that is, the value of the grid itself is +1. When we fill in the mines, we also fill in the number of the warning area by the way.

4) Click on the map to open the corresponding area

  1> point to no minefield

  The rule of opening the mine-free area is to open all the warning areas on the border that are directly connected to all the mine-free blocks we clicked on. Here we use the width of the graph to traverse. When we click to 0, we will open it and traverse the surrounding 4 spaces. If the value is not 0 (warning area), we will open this area directly. If it is 0, we push it into the open queue, and save the processed node to the close queue to prevent it from going back. After that, the open queue head element is popped up, and the judgment processing in the previous step is continued until the queue is empty.

 

 

  2> Click to the warning area to open the area

 

    3> Click to the minefield prompt failure

 

  4> No minefield is opened to declare victory
  When the M*N-(M*N/10) block area is opened, victory is declared.

3. Algorithm Implementation

Implemented using typeScript.

https://github.com/sincw/miniGame/blob/master/webContent/src/main/webapp/work/mineSweeper/controllers/mineSweeperController.ts

After downloading, the following page can browse the mine clearance process

/webContent/src/main/webapp/work/mineSweeper/mineSweeper.html

 

Fourth, summary

  The only difficult point in the process of implementing mine clearance is to open the mine-free area. This is a classic algorithm, the FloodFill algorithm, also known as the flood filling algorithm, which is widely used. For example, the brush tool in PS can cut out the outline of tasks or items.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325077141&siteId=291194637