Seriously, I should never be up until 3:30am, but since my wife isn't around I have no self control. I'll turn in early tomorrow.

Why am I up so late? Can't you guess. I pulled another all-nighter. Got it in my head to do this program and couldn't stop until it was done.

Supposedly, making a computer program play Nim is an academic exercise. Don't you believe it. Especially if like me you insist on making Nim (multiple piles) and not pick up sticks (one pile) and insist on making the misere version (loser takes last) that everyone is SUPPOSED to be playing anyways, not the "normal" version (winner takes last) that mathematicians like to talk about because the math is easier. But add to that I set myself the sadistic goal of making a game that not only played a perfect game but would teach YOU, the human, to play a perfect game.

See, if the computer's going to beat you every time you play, what's the fun in that? I'd rather the computer be used as a tool to help me beat my friends later. Maybe your highly honed skills could settle a tab with the bar tender. Maybe not. The point is being beat isn't fun. Learning to beat someone else can be.

So now I present to you the fruits of my depravity. Why? Like I said, it's 3:30 am and it seems like a good idea.

void intro () {
puts("Nim Trainer\n----------\n"
"Nim is a counting game where players take turns removing objects from\n"
"piles of things. You can take as many as you want from whichever pile\n"
"you want. The person who takes the last one loses.\n\n"
"It is possible to play a perfect game if you:\n"
" o Think of the number in each pile in binary.\n"
" o Do an exclusive 'or' (xor) on the numbers.\n"
" or count up the number of 1s in the binary columns and if there are\n"
" an even number write a 1, if an odd number wirte a zero.\n"
" o The number, in binary or integer, is called a 'nimber'.\n"
" o If the nimber is zero, you can't win this round.\n"
" o If the number is not zero find a piece you can take that will leave\n"
" your opponent with a zero nimber.\n"
" o To win the game, when you get to the point where you'd normally leave\n"
" all piles of size 1, make sure there are an odd number of sized 1 piles\n"
" and you won't be taking the last one.\n\n"
"This program is written so help you learn to win nim by showing you the\n"
"piles in binary and doing the xor for you so you can seen how it works.\n");
}

number = 0; /* Use number to count the number of >1 sized piles */
for (c = 0; c < MAXPILES; c++) {
bmoves[c] = 0; if (p[c] > 1) number++; /* clear best move array as well*/
}
if (number == 1) { /* We are in the end game. */

printf ("\nThis is it, the end game. Make sure you leave an odd number of "
"piles of size 1.\n");

The difference? I removed 17 lines of code and now it plays to take that last piece. The strategy is exactly the same, up until the end game. Playing misere means the strategy has to change right at the end, but the method of keeping control is exactly the same up to that point.

Mathematicians are lazy, man. Thorough, but lazy. We'll sum up 20 pages of proof with "from here the solution is academic," which really means, "bugger all, it's going to take us 20 more pages to finish this right. You do it."

What drives me crazy is that lazyness affected the vocabulary. I was under the impression that the misere version of nim was the way to play it, the way everyone played it. Then I run into the mathematical analysis of the game and suddenly the normal way of playing it (misere) is not what they called the normal play. So the variant gets to be called normal because it's solution is shorter.