
If the value of the tile equals zero (which means none of the neighboring tiles have a mine), we'll also reveal all the surrounding tiles. When the player left clicks on a covered tile, we'll reveal its contents. Each of those elements will also be an array, whose elements represent an actual tile.įirst, in the 'init' function, we'll create the tiles and place them on then screen. For example the main array 'board' will consist of 5 elements representing rows of the matrix. "picture", which will contain on of the following strings: hidden, flag and question so that we don't have to get the image name and strip the '.png' ending each time we need to check what the tile is showingīecause JavaScript does not directly support two-dimensional arrays, we will create an array of arrays (lines 13-19). Misplaced.png - a place where a flag was incorrectly placedģ. These two are only shown after the game is finished: The image can be:Ġ.png thru 8.png - number of mines in neighboring tiles (blank means zero) "tile", which will consist of dynamically created image (IMG) objects, on which the player will click. "board", which will contain: 'mine' for mine or a number 0 thru 8 which is the number of mines in the eight neighboring tiles.Ģ. The mechanism of uncovering all tiles with zero value is a good exercise in recursive functions - the function calls itself multiple times.ġ. Play the game by clicking the tiles on top of the page!Ĭoding this game in JavaScript is a lot of fun, since it is a relatively short and easy algorithm, but at the same time quite interesting. This allows you to simulate different scenarios - you can place a question mark and then verify if the neighboring tiles have the correct numbers. A question mark is placed if you suspect a mine might be there, but you're not certain. This reduces the count of mines remaining. You place a flag when you are sure the tile has a mine. Right-clicking on a tile toggles between flag, question mark and hidden. You repeat the process until all safe tiles are revealed. Based on the revealed numbers, you calculate which of the neighboring tiles contain mines, mark them with a flag and click on the safe ones to reveal them. Otherwise, the tile will show the sum of mines in the eight neighboring tiles. When you click a tile, its content is revealed. The point of the game is to uncover all the tiles except the ones containing the mines. The board consists of rows and columns of tiles, some of which contain a mine. If you're older than 30, let me remind you the rules, since at our age memory no longer serves us well: If you're younger than 30, let me teach you the rules. It probably led to millions of hours of productivity wasted when people played it at work.
#MINESWEEPER GAME IN JAVA WINDOWS#
Let's bring the 1989 Minesweeper game back to life!Īctually the game is much older, but it gained worldwide fame after it was bundled with Windows 3.1. The Minesweeper game in 100 lines of JavaScript
