Thursday, July 23, 2009

Learn to write better code

If you don't make mistakes, you're not working on hard enough problems.  And that's a big mistake.  ~F. Wikzek

It was when I found out I could make mistakes that I knew I was on to something.  ~Ornette Coleman

While one person hesitates because he feels inferior, the other is busy making mistakes and becoming superior.  ~Henry C. Link

I consider myself lucky, in every place of work I had a mentor that helped me improve and taught me something that made me a better software developer. image

Looking back I realize that most of what I’ve learnt I learnt because I made a mistake and someone was able to catch it and show me how to do a better job.

Not all software developers have the benefit of a mentor, either because they work alone or because their code doesn’t get reviewed or simply because no such person exist in their place of work.

In order to become a better software developer one needs to learn how to do so by himself. Reading blogs is one possibility as well as learning from mistakes others already made.

I have two sites I read daily that show mistakes and errors done by others and over time I’ve leant quite a lot from them:

  • The Daily WTF – I really enjoy reading this site, It has coding errors and funny stories about our industry. It taught me a lot on what not to do as well as common pitfalls in software projects.
  • Unmaintainable code – an extensive glossary of how not to write good code. Just read it and do the exact opposite.

Both sites are a good resource of mistakes done (over and over) during code development that can be avoided by understanding the reason they were made and why we shouldn’t do them again.

Wednesday, July 15, 2009

Refactor! – a free C++ refactoring tool

When I write .NET code I have Resharper to increase my productivity and help me generate and refactor code. I miss such a tool when I program in C++, I knew such tools exist but because I spend more time writing managed code I don’t want to purchase them.

It seems that in C++ where every method is written at least twice (header and code) the environment is in desperate need of a proper code generation/refactor tool.

Last week I discovered that Refactor for C++ is shipped as part of DevExpress DXperience suite – for free!


It has a lot of much needed goodies:

  • Rename all instances (same as R# F2)
  • Change method’s parameter order
  • Extract code to method

A must tool for every C++ developer.

Tuesday, July 14, 2009

Getting started with IronPython – Part 4: MiniMax algorithm

After learning the basics of IronPython programming in the previous posts you’re ready for the fourth post – where I’m going to add simple AI logic to the Mancala game.

Previous Posts:

As part of my quest to learn how to program in Python I’ve decided to implement a simple Minimax algorithm.

The algorithm is simple and relies on a elementary principle – each turn you will choose the best move and your opponent will choose the worst move for you. By building a search tree with a limited depth we can then choose each time either the maximum result possible or the minimum result according to the player’s turn.

You can read more about Minimax algorithm in Wikipedia – or continue reading and learn it from my implementation.

Getting board value and available moves

The first thing needed for our algorithm is a method that will evaluate the “board value”, in our case we can calculate the board as the number of seeds in my store minus the number of seeds on the opponent store or in IronPython:


The second ingredient is a method that will return all of the possible moves from a specific board. Finding all of the possible moves is simple just iterate all of the pots on one player side and choose all of the pots that have seeds in them:



Michael Foord suggested that I use a single list comprehension (see comments below) instead of the code above, I’ve tried doing so and liked the result. here is calculateValue version 2:


An index field was added to the pot class and now I can get all of the available moves in a single line. It seems like a better more “Pythonish” solution of creating the list by certain criteria instead of writing a less easy to understand iteration.

Update 2:

As Danilo Cabello pointed out that I slightly missed the point, as a C++/C# developer I tend to overuse for and index and Python can do exactly what I need without keeping index field as part of the class:


For more details see his comment at the end of this post.

The Minimax Algorithm

Now that we have the two basic functions we’re ready to actually write the algorithm.



The Minimax algorithm is recursive by nature, and as such it requires a stop condition, in our case either the game has ended (no more moves) or the desired depth has been reached (lines 3-8).

Lines 10-16 checks whether its the player’s turn (Max) or the opponent (Min). I’ve used Lambda and function pointer to decide if to use a Max or Min functions. If you’re familiar with Lambdas from other programming language you should be able to understand it. basically I’ve declared a method that takes two arguments and returns a Boolean value. Declaring a new function pointer (or delegate) is simple like any other declaration in Python just write a variable and you’re pretty much done.

Lines 18-27 are the heart of the algorithm:

  • First choose a move and call the method again with the resulting board
  • Then if the result is better (depending on the lambda used) keep the best score and the path that caused it.

finally the method returns two values – the best score and the path. I really like the fact that Python methods can return several values, no more ref and out parameters for me.

The Story so far

Now that we have a (simple) AI player our game is almost finished, in case you’ve forgotten what this is all about you can take a look the Mancala class diagram:


As always this post's code is available for download so you can check it and give me some pointers.

Stay tuned - we have at least one more post to go…

Related Posts Plugin for WordPress, Blogger...