Click here to Skip to main content
15,879,490 members
Articles / Programming Languages / Javascript
Article

OOP Minesweeper using JavaScript

Rate me:
Please Sign up or sign in to vote.
4.88/5 (11 votes)
8 May 20034 min read 97.7K   1.7K   36   14
OOP Minesweeper on a web page.

Sample Image - js_minesweeper.gif

Introduction

This is my first attempt at a game as well as my first article here at The Code Project.

I have always wanted to create a game and the first thing that came to my mind that I might be able to make, was the classic game Minesweeper, that has always come along with the Microsoft OS (at least to my knowledge). Since my skills are mainly in the area of web application development, I immediately jumped into creating the game board with HTML and scripted it along with JavaScript. I learned many cool tricks developing this game and I hope that you find some of them useful and interesting as I share them in this article.

Planning

I know that planning is a good idea and all ... but ... I just jumped in head first and tried to make different parts of the game work as they seemed logical to me. After coming back through and cleaning up my code, I can see many future possibilities of expanding the features of the game but for now, it works and can be played as many times as you want.

The game board

The game board itself is represented by a two-dimensional array filled with mineNodes. Each mine node captures its own events (left and right-clicking, along with some useful information in the status bar on mouse-overs) and holds information about itself and its surroundings.

JavaScript
/*/
/// MINE NODE CONSTRUCTOR
/// This creates a default node for the mine game to be used for
/// keeping track of its status, if it is a mine, and how many mines
/// are surrounding it.
/*/
function mineNode() {
    // Properties
    this.strStatus = "unclicked";
    this.boolIsMine = false;
    this.intSurroundingMines = 0;
}

Making it versatile

Originally I just made the game very sloppily (no planning will do that) and it would only do the novice level of the Minesweeper game one time. After thinking about it, and remembering that I did learn how to make object oriented programs back in college, I knew the logic should be basically the same for any size game. So, that's what I did. A bunch of thinking, removing unwanted and ugly code, and one huge recursive function later I developed a mineGame object. This takes in three arguments to define the number of columns, rows, and mines.

JavaScript
/*/
/// MINE GAME CONSTRUCTOR
/// This creates a mine game
/*/
function mineGame(rows, cols, mines) {
    // Properties
    this.boolGameOver = false;
    this.intFlags = 0;
    this.intQuestions = 0;
    this.intRows = parseInt(rows);
    this.intCols = parseInt(cols);
    this.intMines = parseInt(mines);
    this.intRemainingSpaces = this.intRows * this.intCols;
    this.strDivHTML = "";
    this.arrRows = new Array(rows);

    // Methods
    ...
}

Current drawbacks

Unfortunately the game is not completely independent in its design. The game has only been tested with Internet Explorer 6.0 on a WinXP machine. I don't think any version of Netscape will run it properly and have no idea how other OS's will handle the game. It relies on DIVs to place the game board, mines remaining, and the timer on a web page. Images are used to display the different states of a mineNode and they must load for their first use, which can delay the game play. Oh, and what fun is it to play a game if you can't keep track of your high-score (or other people's high-scores too) and try to beat that mark? Alas, the game is not as fast as a compiled game (of course) and will fluctuate in performance depending on your Internet connection (mainly the image issue) and CPU speed. If you are a serious Minesweeper player this maybe a very frustrating version to play.

Bugs

For the most part, the game is rather "bug free". The bug that I have found is that sometimes (mainly in the Intermediate or Advanced games) the game will end in a "You Win" prompt before you have actually cleared all of the remaining squares. My bet is that the counting is messed up somewhere in the hairy recursive function to clear large areas. As of yet I have not had the time to find the error and correct it, however, any comments or suggestions are most appreciated.

The future

Here is what I would like to do with the game in the future:

  • Create different style-sheets to apply different themes / looks
  • Keep high scores
    • Gotta validate users with some sort of login
    • Use server side scripting (probably ASP.NET / C# ... maybe a web service?) to save high scores
    • Keep track of individual and overall high scores
  • Maybe make the game into a HTML Component (HTC)
  • Update the high-scores dynamically using XMLDOM (to avoid refreshing the entire page)

Versions

  • 1.0 - This was the original version of the JavaScript Minesweeper game that I created using images to display the different states of the mineNodes. You can still play this game here.
  • 2.0 - The images have been removed from the game and replaced with style sheets. This helps reduce the load time and will allow for people to easily create a different look for the game. You can play this version here.

Conclusion

It was a ton of fun creating this game. Please feel free to send suggestions for this project. I'd love to hear any feedback people might have ...

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Born and raised in Charlottesville, Virginia, USA.

Majored in Information Science at Christopher Newport University and now I design/program web applications for a living.

I'm an avid fan of the W3C web standards and would love to see them used to their full potential and make the web a better place for both surfers and developers.

Comments and Discussions

 
GeneralSlick Pin
AdamNThompson30-Sep-07 14:34
AdamNThompson30-Sep-07 14:34 
I like it.

-Adam N. Thompson

GeneralNice, but... Pin
Oskar Austegard14-Mar-06 4:05
Oskar Austegard14-Mar-06 4:05 
GeneralCool Work Pin
zarouriali8-Oct-05 22:13
zarouriali8-Oct-05 22:13 
GeneralGreat work!! Pin
PSK_29-Jul-04 20:56
PSK_29-Jul-04 20:56 
GeneralRe: Great work!! Pin
Anonymous16-Nov-04 22:15
Anonymous16-Nov-04 22:15 
GeneralGreat work Pin
Dr.Net13-May-03 10:15
Dr.Net13-May-03 10:15 
GeneralVery Cute Pin
Blake Coverett10-May-03 9:30
Blake Coverett10-May-03 9:30 
GeneralRe: Very Cute Pin
DFU2313-Jun-03 4:27
DFU2313-Jun-03 4:27 
GeneralRe: Very Cute Pin
Blake Coverett14-Jun-03 22:43
Blake Coverett14-Jun-03 22:43 
GeneralCrazy Pin
Kocil20-Mar-03 3:50
Kocil20-Mar-03 3:50 
GeneralRe: Crazy Pin
DFU2320-Mar-03 7:54
DFU2320-Mar-03 7:54 
GeneralExcellent Pin
Anonymous9-Oct-02 13:40
Anonymous9-Oct-02 13:40 
GeneralGreat. Pin
Skutvik1-Oct-02 20:03
Skutvik1-Oct-02 20:03 
GeneralOOP Minesweeper using JavaScript Pin
Anonymous1-Oct-02 8:26
Anonymous1-Oct-02 8:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.