Minimax with Alpha-Beta Pruning

When trying to understand minimax with alpha-beta pruning I found loads of Computer Science-type material on the web. But very little in the way of practical examples.

Hopefully my java Tic-Tac-Toe game with perfect AI that uses this technique will be useful to someone looking for a practical example.

If you have any questions, please put them in the comments here and I’ll do my best to answer them.

Ruby for Programmers

A minimalist’s guide to Ruby:

"strings", 'strings', :lisplikesymbol
Constant
variable
object.method()
Class::staticmethod()
$global
@instance_var
@@static_var
{ block }
do
       alternative_block
end
{ |block, args| at_beginning_of_block }
(1..3)
('a'..'z')
(0...5) # three dots = excludes last
['array', 'jfdlka']
{'hash' => 'dictionary', 'jfsdkal' => 'jfd9'}
/^regexp$/
nil
false
==
=== # if in range
if else end
"concat" << "enate"
check_for_nil.nil?

Generating Fixture Lists

Update 2007-03-21: If all you want to do is generate a fixtures list you can use my Fixtures Generator

I’ve been trying to find out how to generate fixture lists for some time (it is harder than it sounds!) and I finally worked it out thanks to this page about a fixture generating algorithm. My Java implementation of the algorithm is below.

/* 
 * This code owes an enormous debt to 
 * http://www.barrychessclub.org.uk/berger2001.htm 
 */

import java.util.Arrays;

public class Fixtures2 {

    public static void main(String[] args) {
        
        // Find out how many teams we want fixtures for.
        int teams = 0;
        try {
            teams = Integer.parseInt(args[0]);
        } catch (NumberFormatException e) {
            System.err.println("Please state number of teams as first arg.");
            System.exit(0);
        }
        
        // If odd number of teams add a "ghost".
        boolean ghost = false;
        if (teams % 2 == 1) {
            teams++;
            ghost = true;
        }
        
        // Generate the fixtures using the cyclic algorithm.
        int totalRounds = teams - 1;
        int matchesPerRound = teams / 2;
        String[][] rounds = new String[totalRounds][matchesPerRound];
        
        for (int round = 0; round < totalRounds; round++) {
            for (int match = 0; match < matchesPerRound; match++) {
                int home = (round + match) % (teams - 1);
                int away = (teams - 1 - match + round) % (teams - 1);
                // Last team stays in the same place while the others
                // rotate around it.
                if (match == 0) {
                    away = teams - 1;
                }
                // Add one so teams are number 1 to teams not 0 to teams - 1
                // upon display.
                rounds[round][match] = (home + 1) + " v " + (away + 1);
            }
        }
        
        // Interleave so that home and away games are fairly evenly dispersed.
        String[][] interleaved = new String[totalRounds][matchesPerRound];
        
        int evn = 0;
        int odd = (teams / 2);
        for (int i = 0; i < rounds.length; i++) {
            if (i % 2 == 0) {
                interleaved[i] = rounds[evn++];
            } else {
                interleaved[i] = rounds[odd++];
            }
        }
        
        rounds = interleaved;

        // Last team can't be away for every game so flip them
        // to home on odd rounds.
        for (int round = 0; round < rounds.length; round++) {
            if (round % 2 == 1) {
                rounds[round][0] = flip(rounds[round][0]);
            }
        }
        
        // Display the fixtures        
        for (int i = 0; i < rounds.length; i++) {
            System.out.println("Round " + (i + 1));
            System.out.println(Arrays.asList(rounds[i]));
            System.out.println();
        }
        
        System.out.println();
        
        if (ghost) {
            System.out.println("Matches against team " + teams + " are byes.");
        }
        
        System.out.println("Use mirror image of these rounds for "
            + "return fixtures.");
    }

    private static String flip(String match) {
        String[] components = match.split(" v ");
        return components[1] + " v " + components[0];
    }
}

Update 2005-06-05: Now allows “complementary teams”, that is – teams that never play at home at the same time as each other. For example, try 10 teams — for each pair from 1 & 6, 2 & 7, 3 & 8, 4 & 9, and 5 & 10, both are never both at home. It was meant to be in the original program but a bug in my code was fouling things up.

Update 2007-03-21: You might also want to see the source code to my PHP fixtures generator

Perfect IDE

My perfect IDE/text editor has the following features:

  • Java support. And extensible to support other languages. Preferably has existing libs/files for Perl, C#, Ruby, Python, PHP, ASP/VBScript.
  • Runs on Windows. Preferably cross platform for when (if) I make a jump to Linux or OS X as my main platform, and for when I am dabbling in those platforms.
  • Brilliant regex support.
  • Code completion. Intellisense (as in Visual Studio) where it understands the object model and supplies only methods of an object (for example) that exist on that object.
  • Unlimited undo.
  • Fast. Starts fast and stays fast when running.

To me that doesn’t look an unachievable list but my prime candidates all fall short:

Ultraedit doesn’t have the IDE-defining capability of understanding what the text files it is working on mean, therefore it cannot do Intellisense.

JEdit is too slow and Intellisense support is patchy and from a third party.

Visual Studio is just about there but only supports .NET languages.

IntelliJ IDEA is great, if very large/slow. But it only does Java.

My only hopes are Emacs (which doesn’t feel like a Windows application when running on Windows) and Eclipse (which at least aspires to supporting many languages though it is slow and uninituitive). I will investigate both of these further.

Basically I want Ultraedit (or any other really good text editor) with Intellisense. Or failing that Visual Studio/IntelliJ IDEA with support for all languages. Any suggestions?

Jobfight

I’ve added the ability to compare two keywords (and see which has more jobs attached) to my jobfight page.

Like googlefight, but for jobs.

The statistical record of how many jobs are available by keyword that I have been running for the past two years is still there, also.

Does Anyone Love Java?

Paul Graham says that
nobody loves java:

“No one
loves it. C, Perl, Python, Smalltalk, and Lisp programmers love their
languages. I’ve never heard anyone say that they loved
Java.”

I’m not so sure.

Google Says

Googling for “I love java” gives 5,400 results. Adding the word
programming to the search1 yields
2,680 results. Not all sarcastic, surely. The results include “Why I
Love Java” and a page that says that, “it makes programming quick and
fun”.

These numbers don’t tell us much. Anything as widespread as Java must
have some fans. We need something to compare with: ‘”I hate java”
programming’ gives 1,350 results. Java seems to be more loved than
hated, at least.

Paul Graham contrasts Java with C, Perl, Python and Smalltalk.
Google says:

“I love Java” programming 2,680
“I hate Java” programming 1,350
“I love C” programming 872
“I hate C” programming 506
“I love Perl” programming 1,720
“I hate Perl” programming 426
“I love Python” programming 1,330
“I hate Python” programming 46
“I love Smalltalk” programming 57
“I hate Smalltalk” programming 3
“I love Lisp” programming 185
“I hate Lisp” programming 77

So Java can claim to be the “most loved”. By the better measure
of love/hate ratio Java does approximately as well as Lisp, and better
than C.

Old and New

It is the two most established languages2 that are
most hated: Java and
C. It is this establishment that is key. Because they are established
they are forced on programmers more often, not chosen. Because they are
forced they are hated. It is harder to hate a tool that you selected
than one that is forced on you.

Python is not so well established. It finds its
place in the fringes of organisations and in the open source
community. Where it is used, it is selected. If it is not
ideally
suited for a task, it tends not to be used. Because of its
age3, if a library or feature
is missing it will be excused as “coming soon”. If a library or
feature is missing in Java it is considered a serious shortcoming.

Older languages also have more visible shortcomings. When C was young it
was the most portable language in the world. Now we say that Java
solves some of those portability problems with far greater success;
similar claims can be made for Perl, PHP and others. Older
languages also have the problem that more bad code has been written in
them – you are more likely to have wrestled with someone else’s awful C
than someone else’s awful Python simply because you are far more likely
to have had to edit someone else’s C full stop.

Newer languages seem to be loved more. This might be because a missing
library or functionality in a new language can be excused more easily –
“Generics? We’re working on that.” Also a newer language will be known in
less depth by its average user and therefore its limits will be less well
known. The problems of C and C++ are so obvious to us because hundreds of
thousands of programmers have butted up against them numerous times.
There may be terrible limitations on Python but they are less well
known.

When I wrote the first version of this article my
blog was quickly commented on by a
number of Ruby users all anxious to show that Ruby was the most loved
language. And the results for Ruby are spectacular (love: 1,450, hate: 13).
And Ruby became mainstream (if it can be said to have done so) very recently
indeed4.

It is not just that shortcomings become visible or inexcusable. The
state of the art does move on. Fortran is superior to Assembler5 and C is superior to Fortran. Perhaps it
will one day be possible to say that it is equally evident that Java is
better than C and Python is better than Java6. I suspect that these languages are too much
contemporaries for that distinction to ever be so clear.

Why Love Java?

Let’s look at what Java offers: portability, automatic garbage
collection, object orientation. Yes, Smalltalk offered the OO and the
GC in 1971 but never with such a large standard library and so many
tools, and with C-like syntax too.

If you were forced to work on somebody else’s complex C program
porting it to another OS whilst fixing memory leaks and then allowed to
switch to Java I wouldn’t be at all surprised to hear you say the words,
“I love Java”.

A previous version of this article entitled
Programming Languages That Are Loved previously appeared on this site.

Footnotes

  1. We add the word programming to exclude coffee-related
    results.
  2. Smalltalk and Lisp are both much older than Java but it
    would be difficult to argue that they are more established.
  3. Although Python was initially released in 1991 and Java in
    1996 you can tell by the better measure of when they got their O’Reilly
    books when they “hit primetime”:
    Python in a Nutshell (1st Ed.)
    2003;
    Java in a Nutshell (1st ed.) 1996.
  4. Ruby in a Nutshell (Nov 2001) is one of three O’Reilly Ruby
    books. By contrast Java has more than 150 and Python has 25.
  5. As a general purpose programming language.
  6. Not until after Python has a ternary if-else operatore
    though, I hope.

Ooooooooooooo

o.com,
oo.com,
ooo.com,
oooo.com,
ooooo.com,
oooooo.com,
oooooo.com,
oooooooo.com,
oooooooo.com,
ooooooooo.com,
oooooooooo.com,
ooooooooooo.com,
oooooooooooo.com,
ooooooooooooo.com,
and oooooooooooooo.com
are all registered domain names (although those with 12, 13 and 14 Os have expired).

I am also amused by the fact that
worldcup2006.com,
worldcup2010.com,
worldcup2014.com,
worldcup2018.com,
worldcup2022.com,
worldcup2026.com,
worldcup2030.com,
worldcup2034.com,
worldcup2038.com,
worldcup2042.com,
worldcup2046.com,
worldcup2050.com,
worldcup2054.com,
worldcup2058.com,
worldcup2062.com,
worldcup2066.com and
worldcup2070.com
are all registered too. Quick, grab worldcup 2074.com before it goes!