1. 程式人生 > >Use decision tables to write better tests faster

Use decision tables to write better tests faster

Two kings, one pawn

One of my friends is a Chess Grandmaster. He wrote a program for a Computer Science project which determined if a Chess position containing 2 kings and 1 pawn was a win or a draw for the side with the pawn.

These positions are completely solved in the Chess literature, so the program did not need to play Chess moves. It only needed to encapsulate the known results, though those are complex themselves.

My friend wrote an impressive number of nested if statements. He tested his program by having it run through every possible position, followed by printing test results like the simplistic one below. He carefully compared these charts with the Chess literature.

White to move wins if his pawn is on a square with a +

I mentioned to him that instead of writing his ~3,000 lines of code (LOC) program, he could have written one with ~30 LOC which scanned charts like his test output.

I remember a longish silence. My friend had just discovered the power of decision tables.