Igor Ostrovsky on February 22nd, 2009

After about 3 months of evening coding, the game that I’ve been working on is now live.

RoboZZle is an online puzzle game that challenges you to program a robot to pick up all stars on a game board. The game mechanics are simple, yet allow for a wide variety of challenges that call for very different solution approaches.

Here is an example of a solved RoboZZle puzzle, with the arrow edited-in to show the path of the robot:

Read the rest of this entry »

Tags:

Igor Ostrovsky on February 2nd, 2009

Here is a little puzzle for C# developers reading my blog. What is the error in the program below?

   using System.Collections.Generic;
   class Program
   {
       public static void Main()
       {
           int[] arr = new int[10];
           IEnumerator<int> e = arr.GetEnumerator();
       }
   }

Read the rest of this entry »

Tags:

Igor Ostrovsky on November 19th, 2008

Windows Live Writer is an awesome tool that I use to write all of my blog posts. I am so used to it that I can’t imagine blogging without it, but I have also found ways to shoot myself in the foot with it. Here are two ways in which I managed to lose a nearly finished blog post:

  1. Live Writer clears its undo history when you switch between views. So, if you mess up your article in the HTML view, you won’t be able to revert the change when you notice the disaster after switching back into the Normal view.
  2. In the Preview view, Live Writer looks a lot like Internet Explorer. If you are not careful, it is not too hard to close Live Writer by accident. And, the confirmation dialog looks somewhat like IE’s closing dialog.

Thankfully, I found a way to recover Live Writer posts, and saved myself some wasted work.

Read the rest of this entry »

Tags:

Igor Ostrovsky on October 31st, 2008

As promised at the end of my recent post, I am going to explain how to implement a program that prints itself, in addition to doing other things (like playing Game of Life).

A self-printing program – also called a quine – is a program that prints out its own source code. I will describe one simple way to implement a quine that can be adapted to just about any programming language. The technique does not depend on any unusual language features, but also does not necessarily yield the shortest possible quine in a particular language.

The main idea behind this quine implementation is simple. The quine will consist of two parts: a definition of a string and the program core. The string will contain the source code of the program core. And, what will the program core do? It will print the string twice: once to print the string definition, and again to print the program core.

Read the rest of this entry »

Tags:

Igor Ostrovsky on October 30th, 2008

Conway’s Game of Life has fascinated computer scientists for decades. Even though its rules are ridiculously simple, Conway’s universe gives rise to a variety of gliders, spaceships, oscillators, glider guns, and other forms of “life”. Self-printing programs are similarly curious, and – rather surprisingly – have an important place in the theory of computation.

What happens when you combine the two? You are about to find out, but one thing is for sure: the geekiness factor should be pretty high.

I wrote a little C# program that contains a Game-of-Life grid. The program advances the game grid to the next generation and prints out a copy of itself, with the grid updated. You can take the output, compile it with a C# compiler, run it, and you’ll get the next generation of the game. You can iterate the process, or change the initial grid state manually.

Read the rest of this entry »

Tags:

Igor Ostrovsky on October 18th, 2008

Did you know that there are numbers that cannot be computed by any computer program? It is weird, but true.

And by number, I mean just an ordinary real number. As a perhaps unnecessarily simple example, the result of the division 1/7 looks like this:

        0.1428571428571428571428571428571414285714285714285714285714285714…

We can easily implement a program that prints this number. The decimal expansion of 1/7 is infinite, so the program will have to run in an infinite loop to print the “whole” number. Here is a C# implementation:

    static void Main()
    {
        Console.Write("0.");
        while (true) Console.Write("142857");
    }

Read the rest of this entry »

Tags:

Igor Ostrovsky on September 23rd, 2008

SelectMany is a fascinating operator in LINQ to Objects. For one thing, it is not as intuitive as most other LINQ operators. MSDN says that SelectMany “projects each element of a sequence to an IEnumerable(T) and flattens the resulting sequences into one sequence.” I still remember reading this description of SelectMany for the first time, and wondering why that would that be useful.

Of course, SelectMany is not only incredibly useful, but also surprisingly powerful. In fact, a variety of LINQ operators are really just constrained versions SelectMany. Select, Concat, Where, Take, Skip, TakeWhile, SkipWhile and Distinct can all be easily rewritten using a single SelectMany.

Read the rest of this entry »

Tags:

Igor Ostrovsky on September 12th, 2008

I was discussing the little LINQ puzzle with Stephen Toub, and he brought up an idea which lead to another puzzle. I like this one even more than the previous one.

Why does the last line throw StackOverflowException?

IEnumerable<int> q = new int[] { 1, 2 };
q = from x in new int[] { 1, 2 }
    from y in q
    select x + y;
q.ToArray();

And, how come the code sample runs just fine if you switch the order of the from clauses?

Tags:

Igor Ostrovsky on September 12th, 2008

Why does the last line hang?

IEnumerable<int> empty = Enumerable.Empty<int>();
for (int i = 0; i < 40; i++)
{
    empty = empty.Concat(empty);
}
int[] emptyArray = empty.ToArray();

Answer in the comments section. ;-)

For a slightly harder challenge, check out the next puzzle.

Tags:

Igor Ostrovsky on August 29th, 2008

This article is the first one in a series titled Data structure zoo. Each article will give you a “working knowledge” of data structures that solve a particular problem. You won’t necessarily know how to implement each one, but you will have a good idea of the main characteristics of each solution and how to pick among them to solve your particular problem.

Today, I am writing about data structures to represent an ordered set. An ordered set is a common data structure that supports O(log N) lookups, insertions and removals. Ordered set is also sometimes used as an alternative to a hash map, for example in STL’s map.

Read the rest of this entry »

Tags: