Bricks 3 Bot
Recently, I have been suggested by someone to show to you a bot to play my game Bricks 3. So, here it is ;)
I have injected those 100 lines of code into the proper project, so the bot can be controlled with game built-in commands “botstart” and “botstop”. Cheats do not have to be active to use it. Code is available here. If you do not want to download and compile the code you can also download this jar file and run a bot as a separate process. The only way to stop it then is to kill the process.
Ok, so how it works? The bot simulates mouse events (the computer behaves as if a user really moved a pointer and pressed a mouse button). It does not “think” even though I wanted to create such a bot, but unfortunately this one did well enough :) It presses every two adjacent bricks and because it checks all possible swaps, it always finds a move if there is a one.
The biggest problem with writing bots (or rather macros) is to control them and do not let them work when they should not work – in this case for example, you wouldn’t like to have left mouse button pressed few hundreds of times while you are on a desktop and have plenty of links there… The most funny are first tests of such a bot – when the mechanism to stop it failed and the bot was pressing icons on the desktop or buttons in a browser – it finished with restarting a computer few times…
The best idea I came up with while creating mouse macros in Java (I do it quite often) is to create a JFrame with no content and let the macro work only when JFrame is not focused. So if you switch with alt+tab to this frame, the macro freezes and then you can close a window to exit a main loop of a macro.
But this bot works in a bit different way. In my game there are two different sizes of a board – 10×10 and 11×11 (with bricks sizes of 55px and 50px, respectively). Let me describe it for a one size only. The coordinates of bricks are fixed. Also there exists such a pixel inside each brick which always shows brick’s colour, no matter if the brick is selected or it is a special one and has a picture on it. My bot takes those 100 (or 121) pixels from the screen and for all of them it checks whether they have the colours used by game. If each pixel has a colour of those ten used in game, then the bot continues and starts moving a pointer and pressing the mouse button. If not, it goes back to checking if game is active by taking those pixels’ colours and comparing them – that’s why it is so CPU consuming. This method ensures that whenever a user minimizes a game or a game crashes, the bot will stop working as well. Also, the bot stops if user pauses the game – because background colour is not on that list of ten.
The bot will not play a game forever. After some time the level increases and time gained for vanishing bricks decreases so much that animation of falling bricks needs more time than the amount gained. That’s “a problem” of my vision of the game – I intentionally do not stop a timer for animations, because, to be honest, it doesn’t really matter for a human player ;)
I have created a main GUI of my MTG implementation, fixed encoding problems with saving/loading/transferring decks, created a server GUI and changed a bit a structure of server and client. The main thing that has to be done to make a game playable is to connect a client thread with a game itself, so when the user manipulates the cards, a proper notification is sent to the server and to all other clients.