Skip to content

A React.js implementation of minesweeper game + an algorithmic approach of solving it

Notifications You must be signed in to change notification settings

Garydmg/minesweeper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Minesweeper

Part A: React.js Implementation

1. Setup

In the project directory, run yarn install then you can run:

yarn start

Runs the app in the development mode.
Open http://localhost:3000 to view it in the browser.

The page will reload if you make edits.
You will also see any lint errors in the console.

yarn test

Launches the test runner in the interactive watch mode.

2. Code Design

Click to see code directly:

React.js Components

Board API

Human Player

Computer Player

Part B: Puzzle Solver

1. How to Run

Launches the run solver script with the given optional arguments.

yarn solve [ n [ numMines [ numTrials ] ] ]

yarn solve

automatically solves the 10 * 10 puzzle with 10 mines 100,000 times (each time is a randomly generated puzzle)

If you want to see a step-by-step solution generated by GameAI:

Go to Solver and set the flag below to true.

  const printSteps = false; // set this to true

2. Algorithm

This solver implements a Double-Set-Single-Point (DSSP) strategy. For more information, check here in Chapter 5.3.

Solving a 10 by 10 puzzle with 10 mines 100,000 times (each time is a different puzzle), the chance of winning is around 72%, which is similar to what's been obtained in the paper. The run time is within 35 seconds.

The pseudo-code is illustrated below:

  firstStep = firstMove()
  S = new Set()   // "safe" cells to explore
  Q = new Set()   // cells that need more information about neighbors
  S.add(firstStep)
  
  while game is not over:
    if S is empty:
      x = selectRandomSquare()
      S.add(x)  
    
    while S is not empty:
      x = S.remove()
      probe(x)    // probe means uncover
      if x is mine:
        return failure
      if AFN(x):   // all-free-neighbor: # neighbor mines - marked cells == 0
        S.add(unmarkedNeighbors(x))
      else:
        Q.add(x)
    
    for q in Q:
      if AMN(q):   // all-mines-neighbor: # neighbor mines - marked cells == # of unmarked cells
        for y in unmarkedNeighbors(q):
          mark(y)   // mark cell y as mine
        Q.remove(q)
     
     for q in Q:
      if AFN(q):
        S.add(unmarkedNeighbors(q))
        Q.remove(q)  
        
   return success

About

A React.js implementation of minesweeper game + an algorithmic approach of solving it

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published