Thursday, April 26, 2012

Solving the "Move the Box" game (Programatically)


A couple of weeks ago some colleagues at work showed me this nice puzzle game called "Move the Box". I've seen dozens of these kinds of puzzle games in different variations, and as usual, I got hooked on it for a while.

I got stuck on a level I couldn't pass a couple of days ago, and this got my programming head thinking...
Hmmm... The board is only 6 by 7 tiles, and the levels only have up to 3 moves maximum... This shouldn't take too long for my computer to brute force through all the levels... :)

So, I got to work, and pretty easily got it solving the puzzles.
It works good on the puzzles i tested it on, which include puzzles with 1, 2 and 3 possible moves to the solution. The annoying part is that you have to enter the whole map of the level you want in a two dimensional array, which can take a long time. Then, all you have to do is tell it how many moves it gets, and hit F5!

Here's a screenshot of what I see when the code solves my puzzle -
Spoiler - This is the solution to the level "Osaka 22"

I put the code up on google code's project hosting if you wanna take a look, but this should come with a disclaimer -
The code is really ugly and hacky! This is only because it is a 'coding for fun' project at home, for something that will never reach any kind of production. The only thing that was important to me at the time coding this was the fact that it will work. I don't see myself using this ever again.
With that said, the logic of the board was still coded using classic TDD. It just seemed logical doing it that way, so it even has some cool tests.

http://code.google.com/p/move-the-box-solution/

...and if you're wondering, the answer is "yes" - This was a stupid idea, since I basically took all the fun out of playing this game for me! :)


Enjoy :)

5 comments:

  1. Awesome!! Can't wait for phase II - API that get a screenshot of a certain level, and return the solution for it, BTW you can just ask a friend to help you :)

    ReplyDelete
    Replies
    1. Good idea! I might get to trying this out sometime...
      If you want to help, you're welcome :)

      Delete
  2. So did you finish the game? ...and if you're wondering, the answer is "yes" - I did :)

    ReplyDelete
    Replies
    1. Hhh - No, like i said, it got boring after i universally solved the puzzles. But what really broke me is the fact that i updated it, and got a lot more levels!

      Delete
  3. Great Article. Thank you for sharing! Really an awesome post for every one.

    IEEE Final Year projects Project Centers in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation. For experts, it's an alternate ball game through and through. Smaller than expected IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble. Final Year Project Domains for IT It gives you tips and rules that is progressively critical to consider while choosing any final year project point.

    JavaScript Training in Chennai

    JavaScript Training in Chennai

    ReplyDelete