Learning C#: Chess Part 1

Visual Studio and Chess

In my spare time, I’ve decided to relearn C#, and for your reading pleasure, I’m going to blog about it along the way. I’m really rusty, and I haven’t used C# seriously and consistently in years. I learned it a few years ago, like back in the era of Visual Studio .NET 2003 and Visual Studio 2005, aka, the era of .NET 2.0.¬† Those are pretty “ancient” technologies; now we’re up to Visual Studio 2008, and .NET 3.5.¬†Visual Studio 2010 has no official release date, but it should be out soon, and .NET 4.0 was announced in September 2008.

In any case, I’m also resurrecting an old love of mine: playing chess. When I was in junior high, and even into high school somewhat, I loved playing chess.

So how am I combining these seemingly different amusements? Well, I’m going to attempt to use C# to write my own chess game. Frankly, I have no idea what I’m doing, and it will certainly be a journey of trial and error.

My first thought was to create an abstract class called Piece, which could be inherited by all of the pieces: pawn, rook, knight, bishop, king, queen.

public abstract class Piece
        string name;
        public const int BLACK = 0;
        public const int WHITE = 1;
        int side;

        abstract public void Move(int rows, int columns);

My second thought was to create a class called Board that encapsulates an 8×8 matrix/array that will hold the pieces, just like a real chess board.

public class Board
        Piece[,] BoardArray;
        public Board()
            BoardArray = new Piece[8, 8];

We’ll see how things work out, and I’ll keep you posted as things progress. I’ve already run across an issue in the first day. I originally wrote the Piece class to contain the location of the particular piece. Every piece inherited that field, and was going to “keep track” of where they were on the board. But then I decided that it would be better for the board to keep track of the piece, so I had to strip out all of the position information from my Piece classes. Oh well. That’s trial and error programming for you.

If you’ve got any thoughts or hints, feel free to leave them in the comments. Otherwise, my primary reference sources will be Safari Books, the MSDN, and everyone’s favorite search engine.

Leave a Reply

Your email address will not be published. Required fields are marked *