# 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

By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.

3
7 4
4 6
8 5 9 3

That is, 3 + 7 + 4 + 9 = 23.

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

75
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
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23

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

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

3
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.

Original:

3
7 4
4 6
8 5 9 3

Add bottom row elements to the top.

8 > 5

9 > 5

9 > 3

3
7 4
10 13 15
8 5 9 3

Add the new row to the row above it.

10 < 13

13 < 15

3
20 19
10 13 15
8 5 9 3

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

20  > 19

23
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!

# Minesweeper

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 😛

Board Screenshot

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

Windows Installer

Source

# 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;
public:
list();
list(data_type);
void push_back(data_type);
void push(data_type);
void pop();
void pop_back();
friend ostream& operator << (ostream &out, list &l)
{
node *iter= l.top;
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;
nNodes++;
}

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;

nNodes++;
}

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;
nNodes--;
}

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;
}

```

My very first sports game! A simple basketball game. Get the ball into the basket. No nonsense, really. The physics for this game was relatively simple. And fun. But then again, physics is always fun.

The key here is to setup a good aiming system. The game builds itself around that. One of the best solutions is to represent exactly half the parabolic path of the ball. The player will still have to judge the path of the ball, but the portion that we show on the screen would give him an idea of the velocity and angle.

Our interest is in calculating the path of the ball, which is determined by two parameters:

• The velocity
• The launch angle

Given that the mouse pointer represents the highest point on the parabolic path, both launch velocity and launch angle can be calculated. It’s really simple (should you have taken physics in high school).

Parabolic path of the ball

In the above equations R is the range of the projectile and H is the maximum height to which it rises. It’s easy to see why, for a given position of the mouse, R and H are constants that can be calculated. Rearranging and solving these equations will give us the velocity and angle which is what we need to describe the motion. The velocity will then be resolved into components, for convenience.

The game depends on a few libraries like SOIL for texture loading, freeglut for openGL and irrKlang for audio.

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

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

Source

# Tower Defense

First tower defense game 😀 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

Source

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.

Recently, I took on a project that turned out to be real fun. I attempted to recreate paint. With OpenGL handling the drawing, it was a task to find a good UI library that integrated OpenGL code. An obvious solution would be to use Win32 controls but they’re cumbersome and difficult to use. GLUI turned out to be a disappointment as well, as it refused to work with Visual Studio without the dll. Finally, FLTK provided a much needed break. It’s pretty spiffy as far as GUI libraries go, is cross platform as well.

It’s also the first time I’ve split a project onto a more than 3 source files;
Screenshots:

Source

Release (Windows)

# Stock Quotes

StockSearch is a nifty little application that retrieves Stock Market data using the Yahoo! Finance API. This is the first time I’ve used HTTP in an application.

HTTP requests are handled using the `libcurl` library.

The app maintains a list of all known stock ticker symbols from the NASDAQ and NYSE. It uses this list to find Companies matching the name or symbol that the user has entered. As of now, the list cannot be updated (The functionality exists in the code but has been commented out).

The app has been written in C++ (Visual C++), The GUI has been written for win32 and hence will not compile on other platforms.