Blog Archives

Minesweeper sample

So, I didn’t have anything better to do, so I got again down and tried to make something of my own. I always liked to check my ability to code prototypes. I try to participate in game jams to test my skills, and I tend to watch a lot of tutorials for any type of game out there. Minesweeper was one of the games that people suggested that good coders should be able to make in less that one day, and I’ve never made a minesweeper before. Also, lately I got inspired by a student of mine, whom I teach ActionScript so I got motivated to create a minesweeper game to teach classes (among other things). The result was quite well and took me less than 1 hour, although I tried to optimize it later and make it as easy to learn and as efficient as I can within the limits of knowledge of my student.

I’ve uploaded the code to GitHub and you can find it here. The game was inspired by Windows Minesweeper. It’s plain ActionScript. 3 classes, and 1 more event class. Cloc reports 367 lines of code which is pretty small number, considering the auto completion I got from FlashDevelop and all. To sum this up here is the hierarchy:

  • In the Main class I just initialize the app. I create the Minefield class and a restart textfield which serves as my button and upon click it calls a Minefield function to reset its grid.
  • In Tile class I set a rectangular tile which serves as a tile in classic minefield. I create the color with a simple shape, and I just store information of the position of the tile (without any good reason. Just housekeeping). then I have a tile id which indicates if the tile is empty or contains a mine. A status variable which store the state of the mine if its shown or hidden (that’s an integer though it could become a boolean). Then I have one more integer which stores the amount of mines that exist in neighboring tiles. That one is print in a Textfield above the shape, when the tile is shown. Functions are the usual, and pretty much self described. Show, hide, reset/update, and a checkTile function which shows it for the player. That one dispatches an event in case it’s empty without any mines arround, and creates a flood effect to show up all other tiles until there are mines around. More on that on the Minefield class.
  • A TileEvent class which serves to dispatch events from Tiles. Most of it is not used but I liked to show up the abilities it could have. Mainly it stores the event type, and the information of the tile (position, and if it contains a mine or not[Tile id]).
  • Finally the Minefield class. First of all I set the minefield’s dimensions and mines. Interesting values are 8×8 with 10 mines, 16×16 with 40 mines, and 30×16 with 100 mines. Then I create the grid, which is a 2D array. I prefer to use Vectors for array but that one could be done the same with the Array class. Grid is created upon player click in order to avoid placing a mine where player made his 1st click. When we have the information of player click and which tile it hit, we then randomly place the mines avoiding the same tile for 2 mines and the tile which player clicked. With mine placement I increase by one the surrounding tiles info about neighboring mines to update their respective text. Minefield is ready. Then I respond to player clicks by doing a check at the respective clicked tiles. If tile was totally empty with no surrounding mines I dispatch an event to unfold surrounding tiles until a mine is around. This is called a flood-fill algorithm with a really simple implementation. In function showUntilMineAround, I check each surrounding tile if it has any mines around or not. If it doesn’t this means that we can unfold more neighboring tiles, so I set the current tile as open and then continue my search calling the same function recursively. Although calling the same function recursively could lead to an infinite loop causing the app to hang I avoid this case by setting in advance the tile to open and then if the tile is already open I return to the parent caller (that might seem a little confusing at first so if you didn’t understand it I urge you to search about recursively calling a function). When I spot a tile that has a mine around I also stop the search at this tile by returning to the parent caller.

That’s just about it. This is a simple sample and tutorial on how to create a minesweeper. I hope you liked this guide, the same as I’ve enjoyed creating it, and as my student appreciated it.

Αναπτύσσοντας ένα παιχνίδι – Φθάνοντας στην beta

Τελευταία τα πράγματα μου πάνε αρκετά καλά. Πέραν της οδοντοστοιχίας μου τουλάχιστον. Αυτό το περίεργο πράγμα στην δουλειά να πηγαίνεις κάθε μέρα και να μην βαριέσαι είναι ένα θαύμα. Έχω κάνει πάρα πολλές δουλειές στο παρελθόν, μικρός μοίραζα φυλλάδια/διαφημιστικά, αργότερα η πρώτη μου δουλειά ήταν σε τηλεφωνικό κέντρο τηλεφωνικής υποστήριξης που κάθισα 18 μήνες, στην συνέχεια αρθρογραφούσα σε ένα περιοδικό, κατόπιν έκανα μεταφράσεις σε μια ιστοσελίδα. Όλες τους ήταν απλά ένας τρόπος να βγάλω μερικά λεφτά και πάντα ξεκίναγα με όρεξη. Άλλες τις υπόμενα περισσότερο άλλες λιγότερο. Κάποιες μάλιστα τις ευχαριστιόμουνα. Όλες όμως σε ένα βαθμό κάποια στιγμή τις βαριόμουνα.

Πλέον που δουλεύω αναπτύσσοντας ένα αρκετά μεγάλο παιχνίδι ( αυτό είναι λίγο υποκειμενικό γιατί το παιχνίδι είναι μικρό σε σχέση με γνωστούς τίτλους αλλά δυστυχώς ήμαστε λίγα άτομα που το κάνει αυτόματα μεγάλη δουλειά για εμάς ), κάθε μέρα είναι και μία πρόκληση. Εν τάξη, μπορεί να τύχει μια μέρα να είμαι λίγο κακόκεφος, κυρίως όταν τελειώνει το Σαββατοκύριακο. Αλλά μέσα στην μέρα που θα είμαι στο γραφείο αμέσως ξεχνιέμαι και στρώνομαι στην δουλειά. Read the rest of this entry

Should as3 constructor be lightweight?

When I work in flashdevelop I usually use the code quality tool (PMD) provided by Adobe, to check on the welfare of my code. It can be found under Tools>Flash Tools>Analyze Project Source Code. Generally I try to stick on its warnings and keep my code as clean as I can by its proposals, although sometimes I ignore it on purpose. But there is a warning that always caught my sight.

Constructor must be as lightweight as possible. No control statement allowed, whereas a cyclomatic complexe of 2 has been detected. The Just-In-Time compiler does not compile constructors. Make them as lightweight as possible, or move the complexity of the code to a method called by the constructor. Then the complexity will be compiled by the JIT

I always wondered how much difference can that make and if I should follow it or not. So today I put that on a test. Read the rest of this entry

Smooth movement in Starling

To whoever doesn’t know Starling is a flash library which incorporates GPU acceleration of Flash 11 (Molehill, Stage3D, name it as you want). I’ve been wanting to try it since I wanted to learn AS3, by not using FlashPunk just for academic reasons. And Starling was a good choice since it is very similar to Flash itself. But I had a major problem. There was not a variable from which I could have the time between two frames independently of the frame rate.

By a search in Starling forum I didn’t find the solution I wanted. So I solved it by myself. Read the rest of this entry

Ludum Dare – My experience

The past weekend I participated in 22nd Ludum Dare compo. For anyone who doesn’t know Ludum Dare is a game creation contest (more like a festival actually) where game developers who want to participate must create a game within a certain theme in 48 hours and release it as Open Source. The theme for the 22nd Ludum Dare was “Alone”. What do you earn by winning? Well, fame, and only fame.

48 HOURS? R U SERIOUS? NO MONEY? OPEN SOURCE? Well yes. 21 times now it worked, why not now :P ? It’s a really well known contest. The famous Notch (creator of Minecraft) is taking part in the last ones, and others like Chevy Ray, Sophie Houlden and other known indie developers. I always expected those taking part in the contest writing endless hours code, not eating, not sleeping, just CODE. Many times I have wondered the feeling of it, so I decided to take part in the 22nd contest. Unfortunately, things in my calendar didn’t go exactly well, and I lost the Saturday. Not until late Sarurday did I find out the the contest was running….

Read the rest of this entry