Tuesday, November 30, 2004

Integration sucks!

Well I got JavaChess working easily. There is just one problem...not only is it bad it is buggy! At first I couldn't play a complete game because an array kept going out of bounds. I fixed that and now it is trying to make illegal moves when mate is inevitable! I am thinking seriously about rewriting it using TDD. I will try to fix this bug, but the code is pretty hard to follow, not that it written poorly, it just wasn't written by me and it has NO tests!!!

Wednesday, November 24, 2004

Will I ever make any progress?

I feel like John Kerry! I can't make up my mind (sorry for those Kerryites out there, but I just couldn't help myself) I have scratched the idea of incorporating Crafty. It would be ugly, messy code and what would be the point? Jose already does this. I found a great article on chess programming. The last series has a link to the author's code, and it is written in Java! I have downloaded it and put the javachess package in my project. It compiles, but doesn't run. His code isn't written to be integrated, but that's a challenge I like! We shall see how long it takes to get working code.

Have a happy Thanksgiving! Remember to be thankful!

Thursday, November 18, 2004

Stumbled on Jose's Secret

Jose doesn't actually play chess! It uses crafty (a freeware chess engine that is the best free chess engine out there judging by fifth place showing in the WCCC) The interface to crafty isn't ideal, but it shouldn't be too hard. When I discovered this I had halved to time it took for me to analyze moves, but it was easily becoming apparent that I didn't know the least bit about how to actually do it. When I went looking in the source for Jose I couldn't find any logic, but I stumbled on a comment about converting representations from Jose to Crafty. A few minutes later and Google had found me the link that I needed. Then I simply found the crafty.exe and it was confirmed. So now I think I will ditch my plans for improving my pathetic chess engine. I was further convinced of my move when I stumbled on this page about chess tree searching algorithms. I hardly understand what they are talking about! Interfacing with Crafty it is!!!

Friday, November 12, 2004


I stumbled on http://sourceforge.net/ yesterday and found Jose. It is a Java chess program that loads PGN, has an extensive opening book and plays better than the free version of ChessBase, ChessBase Light. Its only flaw is poor time control. It beats the pants off of me, unless I manage to hold it off and let it use all its time up. The source is available and contains over 400 files and over 3 meg of source! This was all done by one guy!!! Boy do I feel stupid. When you run this thing you can see it work and it does thousands of Nodes (basically a move in a move tree) a second! My stupid program takes 2 seconds for roughly 400 moves! Order of magnitude difference. Mine also seems to get slower because If I look 3 ply ahead (2 for moves for the computer and one for human) it takes over a minute!!! I feel completely discouraged, but I must remember that I have only been working on this two weeks and only have 11 files and 65Kb of source. I will keep on plugging, although I don't feel overly motivated...

Wednesday, November 10, 2004

Decent chess is hard

It took a while, but I finally got it playing "decent." It desperately needs an opening book, but if you don't take advantage of its opening stupidity then it plays OK. It has beaten me a couple of times, even though it only looks one move ahead (this shows you how bad my game is of late). It is pretty slow. My code isn't effecient at all, but each move only take 1-2 seconds. I am not going to call it a success yet. I want to see if I can get it looking 2-3 moves ahaed and do some pruning (not look at move combos that are obviously bad). Then I need a simple opening book, the start of the game really kills its chances.

I have 2125 LOC and 1171 of it test code. One interesting note is that only 117 lines of the code are for the ChessPlayer.

Monday, November 8, 2004

Smooth sailing

Well I haven't got much done. Having two small kids can lead to a busy life! I got the program playing random chess moves late last week (I wanted to post it, but blogger wasn't letting me) I then added some intelligence. If you give it a mate move it will take it and win! It will always takes a piece, even if it is defended (yes it will trade a queen for a pawn). This is caused by the fact that it only looks at the result after the computer moves, not the response. It plays pretty badly, but it plays!

I have 1871 LOCs and 1035 of it tests (divided between testing the ChessBoard and the ChessPlayer) I have been closing my mind to ideas such as efficiency of memory usage and processor utilization, but that hasn't been a factor so why worry now! Let the chips fall where they may. Next is to have the ChessPlayer play out my moves, so he doesn't do stupid trades. Then I need to give it some algorithm about space (at least controlling the center and activating pieces).

This is going to easy...there must be a hidden troll under the bridge.

Thursday, November 4, 2004

Stalemate was easy, but code duplication is a problem

Well implementing stalemate wound up being an easy task. It did uncover some interesting bugs that had been lingering around without detection. The test suite (44 tests strong) is doing its job magnificently! Lines of code is at 1163 with 637 of it test. All I have left to handle is three repeated positions and 50 moves without a pawn move or a piece captured. Then on to the hard part ;-) Remember my goal is to have this thing play decent chess (able to give me a game...not that hard these days ;-)

I guess I should say something about the election. Howard Fineman has an intelligent post mortem. The thoughts I take away from the election are simple: the news coverage was pathetic and this country is definitely of two minds.

The news coverage was bad because of their fear of predicting it wrong. Doesn't prediction always have a chance of being wrong? Funny thing is even Kerry admitted defeat before ANY news network had predicted Bush the winner. The closest were FOX and NBS with Bush at 269 electoral votes! Give me a break, it was over when Bush has a 140k lead in Ohio and the prediction of provisional ballots was at most 150k and Bushes lead in Nevada was ~3% with 99% of the vote in! Kerry new it, why couldn't any of the wimps that supposedly give us news tell us that plainly?

The second thought is about our country being of two minds. It isn't really political, meaning republican v Democrat (they simply change their platforms to meet the opinions of the masses). Gone are the days that the parties stand for anything. The two minds in America are secular and religious. The vast majority of urban population is secular and votes against anything religious. The vast majority of the rural population is religious and votes against anything secular. The parties have simply aligned themselves towards that. I think this issue is HUGE and will shape our future. I personally do not want a secular country, but I only get one vote ;-)

Tuesday, November 2, 2004

Checkmate done, but not much else

Well I finally solved all checkmates that I could think up. The weekend had me too busy to get much else done. So not much else got done. I now have 956 LOC of which 534 is test code for 38 tests. My next test is stalemate. This is tricky because I currently don't have any way of determining available moves for a piece. This will cause me to rewrite more code, but in the end I still would have done it the way I did it. Coding is all about learning, not writing code.

Friday, October 29, 2004

Progress...but daunting challenges loom

I finished the transition to class based pieces and got all 32 tests passing! I then started on checkmate. After some false starts I got most cases done without much effort. After talking to a friend I realized that I was still missing quite a few tests (checkmate because a piece is pinned, stalemate, 3 repeated positions anytime, 50 moves without a pawn move or capture). Whew and I thought I was almost done with the chess rules!!! These aren't going to real easy. I am especially worried about the repetition one because I must keep copies of boards. If I just copy my current ChessBoard then I have to copy the pieces as well...that isn't going to work. All I really need is a simple Position class I think...enough designing I don't have any tests failing yet ;-)

I now have 914 lines of code (BTW this doesn't include the GUI) of which 532 lines are test code. 38 tests and they all pass (for now ;-)

Thursday, October 28, 2004

In a groove...then not!

Well I finished the check tests for all pieces and managed to remove quite a bit of redundancy last night. The code had 32 tests and they all passed. The only missing piece was checkmate. I was feeling good. My productivity was great and then BAM! When I wrote checkmate I realized that I desperately needed subclasses for each piece (I know it is hard to believe that up until this point all code for the chess rules was contained in ChessBoard! But I was trying to do the simplest design that worked.) The signs where everywhere...switches, methods with the names of the pieces in them. How blind I had been.

This wasn't going to be easy. I first yanked the type field out of ChessPiece and then created a subclass for each piece. Nothing compiled! I decided that I would start by just getting one test to pass and then add the code for each test. Incremental refactoring! So I commented out all code that didn't involve setting up the board. My first goal was getting Pawn working then so on and so forth. I didn't get done, but 17 out of 32 tests are passing and all piece move logic is located in the respective class. The quality and amount of my tests gave me confidence to embark on a massive change without much fear. This confidence was fed as I was fairly easily able to get tests to pass with just a little work at a time. I foresee challenges coming with certain rules, but given the track record of my past experiences with TDD I am not afraid at all.

I'm back down to 491 non-commented source lines and 201 of them are test. I have commented out tests 18-32! I also have commented out all code for castling, enpassant and check.

Wednesday, October 27, 2004

Sometimes coding is a Slog

Well last night I got most of the check rules working. It was not easy, but I had so many tests that every time I broke something I found it right away. The biggest time sink was debugging a test that was bad. I was testing for check when no check existed, but I was sure it did. Visualizing chess positions without a graphic has always been difficult for me. I am still enamored with TDD. I have a total of 692 NCLOC with a whopping 384 of it in test code!

The most difficult thing for me is still writing code that I know needs to be refactored. When writing the check rules for bishop the straight forward way was so bloated. I had over a page worth of code just for bishop. Once I got all the tests passing, I focused on refactoring and came up with a function that could be used for both bishop and rook. The rook code took about a dozen lines and these were just minor changes from copying the bishop logic. Only time will tell, but I have gotten further than before with less code (not counting tests). The key is that I did it in less time. I don't remember how long it took me before, but I know I spent more then the dozen or so hours I have expended this time. Tonight I hope to get queen checks (just a combo of rook and bishop) and then moves into check and castling across check.

Tuesday, October 26, 2004

Rediscovering coding through TDD

Last week I was reading a series of articles by Ron Jefferies on Discovering Better Code and it got me thinking. I had read several posts on Test Driven Development (TDD) in the past, but I had been busy enough to simply say, "seems like a good idea, I guess." Last week I wasn't so busy and I spent my free time thinking about TDD and how I hadn't programmed any code for a long time. I pulled up my last coding experiment, a failed attempt at a chess program that played decent chess. I had stopped with all chess rules accounted for except check. I had given up trying to debug check because the code was so hard to follow and understand.

So why had I failed? One could argue that I didn't do enough design, but design wasn't the real issue. There just aren't that many classes in my code, the real issue was the actual code. It started out in good shape, but entropy had the gotten the better of it. It had grown to be a Big Ball of Mud. Also I had ZERO tests! So any major change to the code base was likely to result in more errors that would be even harder to run down and fix. Thus the reason for me giving up.

What does TDD have to do with any of this. The main point of TDD is that you start development with a test that doesn't run, then you write the simplest code that will make the test pass. Repeat until done. I had never really given TDD much thought. I thought it might work for some, but probably not for intelligent, experienced coders like myself! Haw, was I full of it. The real problem is getting over the fear of writing code that you know will be rewritten. I hate waste and I thought that TDD generated way too much waste. Wouldn't it be better just to write the correct code the first time..but I know no one can write correct code the first time. It always requires feedback and iterations to get it to work. So what was my problem again? The more I thought, the more I realized that I just didn't want to change the way I developed software. It had worked for 20 years, so why change it? This is funny, because my biggest obstacle in my current job is getting people to change what they always do. Funny isn't it!

So I was able to get past my fear of change and my fear of doing nothing and try it out. This occurred over the weekend. I downloaded a new Java IDE and started TDD Friday afternoon. So I began writing a test. The first thing I could think up was setting up the chess board. That is going to be a too big, so I decided to start smaller. How about an empty chess board. That was easy and some thoughts of waste started to creep in my head, but I beat them down and continued. Next, setup the pawns and so on and so forth. It wasn't long before the whole board was setup. Well at least my tests told me so, but I didn't trust them so I wanted to see it of myself.

I tried to generate a GUI and quickly decided that would take too long and imported my old GUI that wasn't too bad. It's biggest virtue was it was decoupled completely from the implementation. In no time I had a viewable chess board with all the pieces in the correct place! So then I started on movement of the pieces and then captures and viola. After ~7 hours I was back to the same spot I was before. I had a chess board that one could play a game against oneself except that check wasn't understood and neither was the end of a game.

To an outsider this would seem like a huge waste of time, but it was not. I was so productive. I generated 543 lines of code and 333 of it was test code. My old code, which took me much longer that 7 hours to write, had 495 lines of code with ZERO tests! Plus, I never got bogged down. I had bugs, but I could quickly find them because I was working in such short cycles of write small test, write code to make it pass, run all tests and see if I broke anything. Only one bug slipped though which I caught by trying to play a game. I went to fix it and told myself wait! I must have a failed test first. This discipline might seem silly, but it gave me such a good body of tests that when I went to clean up a section (to get the to queen move, I had cut and pasted the bishop and rook code) I wasn't worried because I knew the tests would show me any problems I might inject.

So that was a long post. I should have posted it in chunks over the weekend, but I spent much of the weekend disconnected from the web (my wife refuses to allow me to go wireless ;-) The next question is how hard will check be?? We shall see.

Tuesday, October 19, 2004

Jon Stewart on Crossfire

I just read Stewart Caught in the Crossfire on Slate. I missed this live, but with the internet, do you really miss anything. I have never seen a more honest person on a political talk show. I wish Jon Stewart did a show that was serious. I don't think many people would watch it, but I would. For those like me (with a short attention span) the transcript offers some of what really happened (although I think you have to see it!)

STEWART: See, the thing is, we need your help. Right now, you're helping the politicians and the corporations. an we're left out there to mow our lawns.

BEGALA: By beating up on them? You just said we're too rough on them when they make mistakes.

STEWART: No, no, no, you're not too rough on them. You're part of their strategies. You are partisan, what do you call it, hacks.
STEWART: ... is that the news organizations look to Comedy Central for their cues on integrity.
STEWART: I would love to see a debate show.

BEGALA: We're 30 minutes in a 24-hour day where we have each side on, as best we can get them, and have them fight it out.

STEWART: No, no, no, no, that would be great. To do a debate would be great. But that's like saying pro wrestling is a show about athletic competition.

STEWART: But the thing is that this -- you're doing theater, when you should be doing debate, which would be great.

BEGALA: We do, do...

STEWART: It's not honest. What you do is not honest. What you do is partisan hackery. And I will tell you why I know it.

STEWART: You know, the interesting thing I have is, you have a responsibility to the public discourse, and you fail miserably.

CARLSON: You need to get a job at a journalism school, I think.

STEWART: You need to go to one.

Enough is enough. I could have quoted the whole thing! Sorry for the length. Watch it or read it for yourself.

Friday, October 15, 2004

A true maverick

I came across this review of Maverick by Ricardo Semler and although I have not read the book, I found this review fascinating. I bet Mr. Semler is an interesting fellow.


I came across Two Great Wastes in Organizations earlier this week. Although I have long believed most waste comes from people not talking to each other, this paper lays out the case in a convincing manner.

Tax Stats

I was reading Lay Off Massachusetts and came across Fiscal Facts. This page has the ratio of federal funds paid versus how much federal revenue is generated per state for 1992 and 2002. The most interesting thing is that the state with the highest ratio (gets more than it gives) for 2002 is North Dakota and the lowest is New Jersey. I would have guess my state (VA) would be bad (11th, 1.47) given its proximity to DC, but I would not have figured the top five (SD, NM, MS, AK, WV). Goes to show that you don't always know what you think you know.

First Post

After thinking about doing this for a number of months, I have finally done it! Who knows if I will actually keep posting, but a journey begins with a first step. I have to give credit to the Google Bar. I was looking at the options and they had a BlogThis! option. When I turned it on, I wound up starting a blog. Funny thing this internet is.