GithubHelp home page GithubHelp logo

karthiknedunchezhiyan / minesweeper Goto Github PK

View Code? Open in Web Editor NEW
21.0 3.0 2.0 943 KB

Minesweeper game developed with Rust, WebAssembly (Wasm), and Canvas

Home Page: https://karthiknedunchezhiyan.com/minesweeper/

Rust 47.16% HTML 16.22% JavaScript 26.92% CSS 9.71%
webassembly rust javascript wasm canvas minesweeper wasm-bindgen

minesweeper's Introduction

๐Ÿ‘‰ click here to play the game ๐Ÿ‘ˆ

Minesweeper game

Revealing all the cells without hitting the mines is the task. Each number in the cell denotes how many bombs are in the adjacent cells (8 sides). You can mark any arbitrary cell with a flag.

This game is developed with Rust, WebAssembly (Wasm), and Canvas. Live demo available here

Screenshots

Screenshot 1 Screenshot 2

Functionality

  • โœ… The first click should always be an empty cell.
  • โœ… Right click on PC or long-press on touch devices should place the flag.
  • โœ… Clicking on SpongeBob should reset the game.
  • โฌœ๏ธ Option to select difficulty level.
  • โฌœ๏ธ Option to customize board size and mines count.

Contributing

Suggestions are always welcome

minesweeper's People

Contributors

karthiknedunchezhiyan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

langzime

minesweeper's Issues

Code rework suggestion

I suggest reworking the Vec that holds cells in the board from cells: Vec<Vec<Cell>> to cells: Vec<Cell> and implementing an index calculating method, something like:

fn get_index(&self, i: u32, j: u32) -> usize {
    (i * self.row + j) as usize
}

And from looking at the rest of the code, most of it would have to be modified to use this get_index method, when ever accessing or modifying cells. And creating the cells in the board should be just one long loop of for i in 0..(row * column)

I also recommend reworking the bomb_near_cell method, to remove unnecessary recursive function, to something like:

fn bomb_near_cell(&self, i: u32, j: u32) -> u8 {
    let mut count = 0;
    for delta_i in [self.heigh - 1, 0, 1].iter().cloned() {
        for delta_j in [self.width - 1, 0, 1].iter().cloned() {
            if (delta_i == 0 && delta_j == 0) || (delta_i < 0 || delta_j < 0 || delta_i > self.row || delta_j > self.column) {
                continue;
            }

            let neighbor_i = (i + delta_i) % self.row;
            let neighbor_j = (j + delta_j) % self.column;
            let idx = self.get_index(neighbor_i, neighbor_j);
            count += self.cells[idx].is_bomb as u8 // This will work because bool casting to int is false = 0, true = 1
        }
    }
    count
}

This way we have no need of recursive calling of the bomb_near_cell method when checking the cells around the current one. No need for the search_end parameter.

While JS is not my strong point, at quick glance, if correctly implemented these changes will not have any effect on the front end.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.