Recursive Sudoku solver

I got back to coding after a long absence cause mainly by the final exams. My first project was to implement a recursive Sudoku solver.

I had previously written an iterative Sudoku solver that makes use of the same techniques that people do by ruling out what numbers can fit in a cell untill there is only one possible number that can fir. However, this method fails on harder puzzles where a guess has to be made in order to solve the puzzle.

Brute force algorithms enter here. The Sudoku puzzle lends itself well to the classic recursive brute force algorithm,

The step by step algorithm:

  1. If puzzle is solved
    1. End
  2. If puzzle is not valid
    1. End
  3. Find first empty cell : cell that contains 0
  4. While the cell value is less than 10 and while the puzzle is unsolved
    1. Increment cell by 1
    2. Solve
  5. If the cell value is greater than 9
    1. Reset the cell value to 0 : This means that a previous guess was invalid so this ends and the previous cell value is incremented

Mapping the random function

As described over here, the rand() function in C++ is a pseudo random generator that returns a number between 0 and RAND_MAX. This number is generated by an algorithm that returns a sequence of apparently non-related numbers each time it is called. This algorithm uses a seed to generate the series, which should be initialized to some distinctive value using srand.

RAND_MAX is a constant defined in <cstdlib>

But just how random is the rand() function. Its easy to find out by calling the function and comparing how often different values are returned. I do this graphically using OpenGL code to render the graph.

Using fraps, I recorded the variation of outputs from the rand() function.

The graph tends towards a straight line indicating that the more often you call the rand() function, the more even its distribution.

Project Euler # 18

Project Euler # 18


7 4
4 6
8 5 9 3


Find the maximum total from top to bottom of the triangle below:

95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57

NOTE: As there are only 16384 routes, it is possible to solve this problem by trying every route. However, Problem 67, is the same challenge with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)

Brute Force

The obvious solution is to brute force the solution by trying every possible route, through recursion. However, since at each junction the path branches into two, there will be 214 = 16384 routes. With a 100 rows, there will be 299 rows, which is way too much to brute force!

Non – Obvious method

Let us understand this with the help of this small triangle:

7 4
4 6
8 5 9 3

Each element can lead to the element directly below, or the element directly below and to the right.

Assuming a given element is a point on the solution path, the next element has to be the greatest of the two possible elements. Thus add the greatest number below a given number to it and iterate upwards. This is demonstrated on the small triangle.


7 4
4 6
8 5 9 3

Add bottom row elements to the top.

8 > 5

9 > 5

9 > 3

7 4
10 13 15
8 5 9 3

Add the new row to the row above it.

10 < 13

13 < 15

20 19
10 13 15
8 5 9 3

And again, add the new row, this time to the topmost row.

20  > 19

20 19
10 13 15
8 5 9 3

The topmost element is the largest sum!

Source in C++

Data file containing triangle (Program reads from this file)

Note: You can use this to solve problem 67 as well. Remember to change array sizes, iteration limits and the data file!


When Vista came out, Microsoft did away with the classic Minesweeper game that they distributed with Windows XP. So, for my own entertainment and that of the general public, I decided to create my own implementation :P


Board Screenshot

Its written in C++, with openGL for graphics, and the SOIL library for texture loading.

Menu Screenshot

Windows Installer


A class based implementation of linked lists

A class based impelementation of linked lists. Learnt really basic C style lists at school, so I thought I’d go the whole hog and implement a class based interface. Taught myself templates while doing this! Now, I have to resist the urge to template nearly every damn function. Use this implementation by declaring an object of the list class. Add nodes using the push(data) and push_back(data) functions, and remove nodes using pop() and pop_back() functions.

// list.h
#include <iostream>

using namespace std;

template <class data_type>
class list
struct node
data_type data;
node *next;
node *prev;

node *top;
node *back;

int nNodes;
bool indexed;
void push_back(data_type);
void push(data_type);
void pop();
void pop_back();
friend ostream& operator << (ostream &out, list &l)
node *iter=;
while(iter != nullptr)
out << iter->data << " ";
iter = iter->next;

return out;
data_type operator[](int);

template <class data_type> list<data_type>::list()
top = nullptr;
back = nullptr;
nNodes = 0;

template <class data_type> void list<data_type>::push_back(data_type data) //push a new element onto the list (from behind)
node *new_node = new node;
new_node->next = nullptr;
new_node->prev = back;
new_node->data = data;

back->next = new_node;
back = back->next;

template <class data_type> void list<data_type>::push(data_type data) //push an new element onto the list (from the top)
node *new_node = new node;
new_node->next = top;
new_node->prev = nullptr;
new_node->data = data;

top = new_node;

if(top->next == nullptr)
back = top;


template <class data_type> void list<data_type>::pop() //pop off top element of the list
node* delete_node = top;
top = top->next;

delete delete_node;

template <class data_type> void list<data_type>::pop_back() //pop off last element of the list
node* delete_node = back;
back = back->prev;
back->next = nullptr;

delete delete_node;

template <class data_type> data_type list<data_type>::operator[](int index)//displaye element[x] of the list
node *iter = top;
for(int i = 0; i < index && iter->next != nullptr; i++)
iter = iter->next;

return iter->data;





  • The velocity
  • The launch angle


Parabolic path of the ball




Coming up next is a version with highscore tables! Gotta learn a little php for that :P

Edit: High score table is live but a little buggy.

Available on gumroad:


Tower Defense

First tower defense game :D Of course, it doesn’t have all the bells and whistles of commercial games. But it’s simple enough to play a couple of times.

If this project taught me anything, it’s that some things are easier with Vectors. The key to any good tower defense game is predicting the path of the enemy in order to hit!

Diagrammatic representation

Simply aiming at the enemy when you’re about to fire is no good. He isn’t likely to hang around there for two long!
The problem and its givens are listed:

We know,

  • The position of the enemy(which moves) at the time of firing
  • The position of the gun (which doesn’t move)
  • The absolute velocity (speed) of the bullet. (The direction is what we need to determine).
  • The direction and magnitude of the velocity of the enemy

My approach was as follows:

In order to hit the enemy, the final position of the bullet and the enemy must be the same. Also, in time t, the displacement of the bullet and the enemy must be equal. This is written vectorially as:

Which on expanding,

And on squaring and rearranging,

Where cos θ is given by,

Solving this equation using the quadratic formula, and selecting the positive root (since time can’t be negative, in this discussion), will give us the time after which the bullet and the enemy will collide.

Velocity of the bullet is then given by:

Keep in mind that they’re all vectors, so you need to write appropriate classes/structures to deal with them.

Game Screenshot


Executable (Windows) 

Lego NXT Line Follower

Building a line follower with the Lego NXT kit is easy!

Fine tuning it, however, requires some skill. One of the most common algorithm is PID, or Proportional Integral Derivative.

Proportional says: Fix myself according to my present error

Integral says: Fix myself according to the sum off my past errors

Derivative says: Fix myself based on what the error is going to be.

There is a very good treatment of PID over here:

NXC source code

Making Paint.





Release (Windows) 

Stock Quotes


HTTP requests are handled using the libcurl library.



Download the Source

Download the App 


Get every new post delivered to your Inbox.