- #Minesweeper game logic c++ tutorial code#
- #Minesweeper game logic c++ tutorial free#
- #Minesweeper game logic c++ tutorial windows#
Puts("c: 0 or 1: step on cell or flag cell (default: 0)") Puts("Difficulty can be easy, medium, or hard.") Printf("Run \"%s -help\" for help.\n\n", prog)
#Minesweeper game logic c++ tutorial free#
Puts("This is free software, and you are welcome to redistribute it") Puts("This program comes with ABSOLUTELY NO WARRANTY.") Puts("Minestest Copyright (C) 2018 Arda Ünlü")
#Minesweeper game logic c++ tutorial code#
I think my code is OK, but I'm afraid that I might be getting into the Dunning-Kruger effect. var y= Math.I was bored at school, and it was forbidden to use phones in classes so I began writing a minesweeper game on my book and tested it on a simple compiler on my phone and copied into my computer after I got home. var x= index% columns // Convert index into (x,y) coordinates. If( board!=' mine'& picture( index)=" x") // If it's covered and not a mine: revealed++ // If it was uncovered, increase the count of revealed tiles. you win!įunction reveal( index) // Uncover the tile If( revealed= rows* columns- mines) // If all tiles revealed: If((x1>= 0)&(y1>= 0)&(x1 Click here to restart' Įlse if( picture( id)=' x') reveal( id) // otherwise reveal the tile. In either case we display all the mines.įunction check(x1, y1) // this function returns the value of the tile using the (x1,y1) coordinates. The game ends when the player clicks on a tile that contains a mine (lose) or all non-mine tiles are revealed (win). That's probably the most interesting part of this code. The function 'reveal' will call itself recursively, until all the neighboring zero tiles are revealed.
If any of the surrounding tiles' value is zero, we'll reveal the tiles surrounding that tile. 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. These two are only shown after the game is finished:Į.png - a place where a flag was incorrectly placed 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: 'M' 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 uncovered, flag and question mark. 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 logic c++ tutorial 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 80 lines of Javascript