Showing posts with label Chess. Show all posts
Showing posts with label Chess. Show all posts

Thursday, May 8, 2014

A Chess Project, Part 13 (The Final Chapter)

Intro

I can see the light at the end of the tunnel! It's an alternating black and white checkered light, but still, a light nonetheless. How many of you thought it would take 13 blog posts to get here? Well I sure didn't, but I'm glad we're almost done. Here in week 13 we'll be modifying our Web API that we created months ago. We'll make it call into our spiffy chess AI dll in order to determine the best move from a given board position. After all, that's what we were trying to do from the very start!

 

 Code Changes

First, here is the absolute latest code as it stands right now, before today's modifications. It's not the most optimized, and I dare say the AI isn't really all that great, but hey the point of this blog was learning new technologies not to make the perfect chess AI.

Let's get to work. Open up BlogChessController from the BlogChessApi project. That method named PostBestMove is the one we want to do our modifications in. Here's what it looks like currently:

        public HttpResponseMessage PostBestMove(ChessGame game)
        {
            try
            {
                //validate the game
                var validator = new ChessGameValidator(game);
                if (!validator.Validate())
                    return Request.CreateResponse(HttpStatusCode.BadRequest, validator.ValidationIssues);

                //calculate the best move
                IChessValidMoveCalculator moveCalculator = new ChessValidMoveCalculator(game);
                var validMoves = moveCalculator.CalculateValidMoves();

                //return the best move wrapped in an http "ok" result
                return Request.CreateResponse(HttpStatusCode.OK, validMoves);
            }
            catch (Exception ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
            }
        }


You'll notice we don't yet have a call into the AI dll. At the time we originally made this method we had no such AI dll, so I can forgive us. Before we can do the code though, we need to reference BlogChess.AI.Basic1 from the Web API project. Go ahead and do that now (I'm assuming you've seen that enough that no screenshots are necessary). Add a using statement up at the top of the unit while you're at it.

All we really need to do is add in a call to our Negamax evaluation, then add in a little bit of logic to look for win/draw conditions. All of this is just calling methods we've already written. Your newly modified should look like the following:

        public HttpResponseMessage PostBestMove(ChessGame game)
        {
            try
            {
                //validate the game
                var validator = new ChessGameValidator(game);
                if (!validator.Validate())
                    return Request.CreateResponse(HttpStatusCode.BadRequest, validator.ValidationIssues);

                //calculate the best move
                var bestMove = Negamax.Evaluate(game, 2, game.GameStatus == GameStatus.WhitesTurn);
                var responseGame = bestMove.Item2;
                responseGame.CurrentEvaluation = bestMove.Item1;

                //is a win or draw, set game move accordingly
                IChessValidMoveCalculator moveCalculator = new ChessValidMoveCalculator(game);
                var availableMoves = moveCalculator.CalculateValidMoves();
                if (availableMoves.Count == 0)
                {
                    var isKingInCheck = moveCalculator.IsKingInCheck(game.GameStatus == GameStatus.WhitesTurn, ((IList<ChessBoard>)game.Positions)[0]);
                    //look for checkmate
                    if (availableMoves.Count == 0 && isKingInCheck)
                        responseGame.GameStatus = game.GameStatus == GameStatus.WhitesTurn ? GameStatus.WhiteWin : GameStatus.BlackWin;
                    //look for draw
                    else if (availableMoves.Count == 0 && !isKingInCheck)
                        responseGame.GameStatus = GameStatus.Draw;
                }
                else //just the next turn now
                {
                    responseGame.GameStatus = game.GameStatus == GameStatus.BlacksTurn ? GameStatus.WhitesTurn : GameStatus.BlacksTurn;
                }

                //return the best move wrapped in an http "ok" result
                return Request.CreateResponse(HttpStatusCode.OK, responseGame);
            }
            catch (Exception ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
            }
        }



As advertised, just a couple minor additions to call our evaluator and check for end of game.


Testing It

Remember way back when, that web forms project BlogChessApiFlexer? It's right there in the solution, and it's just itching to call the modified web api method. We had already coded a call into the web api (in default.aspx.cs), but prior to now we were just passing in a dummy request and expecting a dummy response. Well now it's time for an appropriate request and response!

        public ChessGame Game
        {
            get
            {
                return Session["Game"] as ChessGame;
            }
            set
            {
                Session["Game"] = value;
            }
        }

        protected void btnServerTest_Click(object sender, EventArgs e)
        {
            //1. Create and setup client object
            using (var client = new HttpClient() { BaseAddress = new Uri("http://localhost:11482/"), Timeout = TimeSpan.FromSeconds(300) })
            {
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                //2. Create a blank chess game to send up in the post request
                if (Game == null)
                {
                    Game = new ChessGame() { GameStatus = GameStatus.WhitesTurn };
                    var positions = new List<ChessBoard>();
                    var sampleBoard = new ChessBoard(true);
                    sampleBoard.Board = new short[8, 8] { { -4, -2, -3, -5, -6, -3, -2, -4 }, { -1, -1, -1, -1, -1, -1, -1, -1 }, { 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, 1, 1, 1, 1, 1, 1 }, { 4, 2, 3, 5, 6, 3, 2, 4 } };
                    positions.Add(sampleBoard);
                    Game.Positions = positions;
                }
                int numPositions = ((IList<ChessBoard>)Game.Positions).Count;

                //3. Send the request
                var response = client.PostAsJsonAsync("api/BlogChess/BestMove", Game).Result;
                if (response.IsSuccessStatusCode)
                {
                    //4. Read the result from the response, display the "best move"
                    var result = response.Content.ReadAsStringAsync().Result;
                    var resultObject = JsonConvert.DeserializeObject<ChessGame>(result);
                    Game.GameStatus = resultObject.GameStatus;
                    ((IList<ChessBoard>)Game.Positions).Clear();
                    for (int positionIndex = 0; positionIndex < numPositions + 1; positionIndex++)
                    {
                        if (((IList<ChessBoard>)resultObject.Positions).Count > positionIndex)
                            ((IList<ChessBoard>)Game.Positions).Add(((IList<ChessBoard>)resultObject.Positions)[positionIndex]);
                    }
                    lblResult.Text = "Success! :" + result;
                }
                else //5. Request failed; tell the user what happened
                    lblResult.Text = "Failzor'd!: " + response.StatusCode.ToString() + "::" + response.ReasonPhrase;
            }
        }



Let's start with the easiest part, the propert Game of type ChessGame. If you've done webforms before, this shouldn't require much of an explanation. This is just a handy, type-safe way for me to be able to reference the current game object from the session so I can persist information about a single game in memory. Moving on to the button click event...

And here's the meat. Some of this was already here. As I said above, we were already calling the web api. What's new is we're now creating and serializing a chess game object (the one from our session), and we're passing this game object to the web api. Then we're taking the result of this call and updating the Game property. This means that, if you keep clicking that button, the AI will play itself! How cool is that? Well it's moderately cool, as it's incredibly slow. But hey, baby steps.

Wrapping it Up

Thanks for sticking with this series of articles everybody. As can happen with coding endeavors, it got away from me a bit. I thought this might be a 4 or 5 article series, not 13. I hope you learned a few new things, and maybe even gained a little bit of interest in chess in the process of reading these.

Oh and here's the absolutely final code, all fanciful and whatnot.

What's Next?

There are a few things you could do really. You could load AI dlls dynamically, configurable via database entry or config file. This would make it easier for other people to create AI dlls that plug into your web api, and you could have clients configured to choose a specific AI. You could also optimize the existing AI dll further (or just make your own) to make it much much quicker (hint: bitboard!). Or just go have a beer, or martini, or diet dr pepper. Whatever.


Resources

online chess board editor at Apronus.com
Chess.com, a great site for everything chess related 

Thursday, April 17, 2014

A Chess Project, Part 12

Intro

This week we get into some really interesting stuff, namely our algorithm for picking which move is the "best" out of a list of evaluated resulting positions. Last week we planted the seed; we created a few classes to evaluate a position numerically, letting us decide what a specific position is worth to us. Now we decide what's the best position.

First, here's our updated project code.

 

Basics of our Algorithm

You might think that this week should be easy. We already did the hard part last week right? I mean, how hard is it to create a tree of moves and pick the one that gives us the best outcome? Well that doesn't quite work. If all you do is evaluate a tree of potential board positions multiple levels deep to find the leaf node with the highest value, you're going to be disappointed. Such a path requires your opponent to make the worst possible sequence of moves in order for you to arrive at your best possible outcome, which is unlikely to happen. So what do we do instead? The logic behind the algorithm is this:
  • Determine the level of depth you will look ahead of time. For example, if you know you can evaluate 5 moves ahead with relative ease, just pick the #5 outta yer butt.
  • Find every single combination of moves for a depth of 5. 
  • For each leaf node, evaluate the position. 
  • White will be trying to maximize the score on his moves, black will be trying to minimize the score on his turns (remember, a negative positional evaluation equates to an advantage for black). 
Check out this link for a great graphical representation of how the algorithm will work. The algorithm in question is called minimax, and has been used for game AI for some time.

And hey, we can improve on it! Don't worry, I'm not smart enough to improve on it myself. This improved version of minimax is called negamax, and it too has been around for a while. It's basically the same as minimax, except it lets you write the same algorithm as above by just negating values rather than using one algorithm for minimization and one for maximization.

Go ahead and take a look at the pseudocode for negamax. One thing you'll notice quickly if you spend a minute reading it, is that it uses recursion. Through recursion we can avoid using a tree structure to hold all the valid moves in memory. It's tough to visualize I admit, it took me hours to really understand how all this worked. If you have the time to spend, google around and read a few more articles about minimax and negamax. Then come back to the graphical representation from above. If you understand how the algorithms work then you'll get a better feel for how you can improve on them yourself should you so desire.

The Code

And now it's time for our code. Within the BlogChess.AI.Basic1 dll, create a new class called Negamax. Here's the code for it:

using System;
using System.Collections.Generic;
using System.Linq;
using BlogChess.Backend;
using System.Diagnostics;

namespace BlogChess.AI.Basic1
{
    public class Negamax
    {
        public static int NumEvaluations = 0;
        public static Stopwatch MoveCalculationTimer = new Stopwatch();
        public static Stopwatch BoardEvaluationTimer = new Stopwatch();

        public static Tuple<double, ChessGame> Evaluate(ChessGame game, int depth, bool isWhite)
        {
            if (game == null || game.Positions == null || game.Positions.Count() == 0)
                return new Tuple<double, ChessGame>(0, game);
            var moveCalculator = new ChessValidMoveCalculator(game);
            MoveCalculationTimer.Start();
            var availableMoves = moveCalculator.CalculateValidMoves();
            MoveCalculationTimer.Stop();
            var colorSign = isWhite ? Constants.WhiteTransform : Constants.BlackTransform;
            if (depth == 0 || availableMoves.Count == 0)
            {
                NumEvaluations++;
                IPositionEvaluator evaluator = new PositionEvaluator(game, availableMoves, moveCalculator);
                BoardEvaluationTimer.Start();
                var evaluation = evaluator.Evaluate();
                BoardEvaluationTimer.Stop();
                return new Tuple<double, ChessGame>(colorSign * evaluation, game);
            }
            var bestValue = new Tuple<double, ChessGame>(-AIConstants.PieceValues[Constants.King], game);
            foreach (var move in availableMoves)
            {
                ChessGame newGame = new ChessGame();
                var positions = new ChessBoard[((IList<ChessBoard>)game.Positions).Count + 1];
                ((IList<ChessBoard>)game.Positions).CopyTo(positions, 0);
                newGame.GameStatus = game.GameStatus == GameStatus.WhitesTurn ? GameStatus.BlacksTurn : GameStatus.WhitesTurn;
                positions[positions.Length - 1] = move;
                newGame.Positions = positions.ToList();
                var val = Evaluate(newGame, depth - 1, !isWhite);
                if (-val.Item1 > bestValue.Item1)
                    bestValue = new Tuple<double, ChessGame>(-val.Item1, val.Item2);
            }
            return bestValue;
        }
    }
}



First let me explain the Stopwatch and NumEvaluations members. Once I had this thing working at a depth of 1 I tried depth 2 and 3. At depth 3 it got incredibly slow; this currently takes 37s to calculate a fairly simple depth 3 board position with an obvious expected outcome (fork the opposing king and queen with a knight). I dropped in these extra member variables in an effort to optimize the code, and I came to the conclusion that I'd have to completely redo the valid move calculation code in order to get a significant improvement. However, we won't be doing that for the blog, so let's move on to the Evaluate method. (disclaimer: it was actually a 60s process to evaluate a depth of 3 on the first try; I made one small improvement to get it down to 37s, though for the sake of conciseness I won't get into what that improvement was).

Our Evaluate method looks very similar to the pseudocode of Negamax from wikipedia. That's because I started with it and modified it to fit our purposes. The main difference between our implementation and that pseudocode, in my opinion, is that I return a Tuple<double, ChessGame> whereas negamax usually returns just the numeric evaluation value. I decided that this would be a bit more intuitive at the potential sacrifice of a small amount of efficiency. Other than that, we recurse the tree to the expected depth, just like every other negamax implementation. Note that only leaf nodes actually have their position evaluated, as denoted by the line "if (depth == 0 || availableMoves.Count == 0)". This is because we don't care about the board evaluation of branch nodes, as they derive their value from their children.

What's Next

I strongly suggest you review all the updated code, especially the new unit tests I created (but did not discuss in the blog post) in our new class BlogChess.AI.Basic1.Test.NegamaxTests. I've created tests in here that make sure we call the Negamax.Evaluate method  properly and validate its parameters, as well as making sure the algorithm can produce the proper expected move for some canned test positions at a depth of 0-3. And guess what, it actually works!! If you are a true fanatic and are excited at the prospect of testing it out further, I encourage you to make your own unit tests, feeding the algorithm some positions where you know the expected outcome. Or hey, feed it some where you don't know the expected outcome and see what it comes up with! If you really want to go nuts, see what you can do to improve the valid move calculator. I didn't write it with efficiency in mind, and it's the biggest drain on CPU and clock time right now.

As for the next blog post...we should have only 1 remaining blog post for this project, yay! We haven't yet modified our Web API from way back when, and we need to make it call our lovely AI dll in order to calculate the "best" move and return it to the client. Once we get that, we've fulfilled the original project goal and can move on with life. See ya next week!


Resources

Wikipedia - Minimax
Wikipedia - Negamax

Thursday, April 10, 2014

A Chess Project, Part 11

Intro

Last week we wrote some more unit tests and fixed up some bugs as a result. Overall we're making great progress but we've still got 3 conceptual tasks left: Evaluate the board position in a quantifiable manner, write an algorithm that emulates a tree structure so we can pick the best possible future outcome, and call all this sweet sweet awesomeness from our web api.


Stalemate and Checkmate

As part of evaluating a board position, we have to check for the end-of-game conditions: stalemate and checkmate. Stalemate should be pretty easy to check. Here are the conditions (based on whose turn it is):
  1. Is the player's king in check? (looking for false)
  2. Does the player have any valid moves available?  (looking for false)
If both of these conditions are met, then we're in luck!

And hey, guess what? Checkmate is just as easy. Here are the conditions for checkmate (based on whose turn it is):
  1. Is the player's king in check? (looking for true)
  2. Does the player have any valid moves available? (looking for false)
If both of these conditions are met, well you get the picture! And best of all, we already have methods that look for check IsKingInCheck() and any valid moves CalculateValidMoves(), so the code is actually done already. Yay us!


AI - Overview

There is such an incredibly body of work out there regarding chess AI; we could go on for years on the topic. Broadly speaking though, we really need 2 parts to our AI (we're keeping things simple):
  1. Evaluate a given board position;
  2. Calculate future board positions, and eventually pick the "best" move.
  3. Strategy: what's that? I wouldn't know a chess strategy if it mated me in 5 moves. We'll skip an attempt at strategy for now, and maybe forever.
We'll start first with evaluating the current board position. Take a couple minutes to think about how you might tackle this and then check back in when you're ready.
...
...
Before we jump into coding, a little background on my level of play. When I play chess, I almost always play against a computer of some sort. My current program of interest is called Shredder (the iPad version), and it has me ranked as "Casual" in human terms (thanks; BTW i own you Mr. computer, so nyah), with an Elo rating of 1290. Why do I tell you this? Well it's certainly not to brag about my rating of casual; that would be like bragging that you've already showered this month when it's only the 10th. I merely wish to begin setting your expectations of what we will achieve. I will be extremely excited if we can get our basic AI here playing anywhere near my level of 1290. The current chess world champion has a rating over 2800, and Master candidate+ ratings start around 2200. And no this doesn't mean 1100 is halfway to becoming a chess master :).

Now, with my 1290 Elo rating in the back of your brain, here are the criteria I would like to use to evaluate the board:
  • Material: more pieces = better odds of winning. Some pieces are better than others.
  • Board control: The more squares you control (threaten), the better your odds are of winning. 
    • Side note: we're going to keep our algorithm uber-simple. Some algorithms treat middle squares as more valuable than edge and corner squares, and really a pawn doesn't "threaten" a square in front of him just because he can move to it; but hey, we have to start somewhere and this blog is already 11 posts long.
And here are a few more that we'll ignore in order to keep things simple. If you're interested in this topic, I strongly encourage you to branch out on your own and try some of these:
  • Initiative: The player with the initiative is the one dictating the direction and pace of the game, and this gives the player an inherent advantage.
  • Pressure: see initiative. More important as a psychological tool against humans though.
  • Pawn structure: 
    • Pawns generally work best when not stacked together on a single file/column. 
    • Pawns work best with the smallest # of "islands". A pawn island is a contiguous section of pawns. For example, at the start of the game you could be considered to have 1 big pawn island. If you lose the pawn on the b-file and the e-file, you now have 3 pawn islands (a, c-d, f-h).
  • King protection in early and mid-game: in the early/mid game it's usually better to have your king protected behind a shield of pawns. Thus castling and other king protection options should be favored. Other than tweaking the algorithm for threatened squares, this would probably be my #1 choice of things to add to our algorithm.
  • Opening move database: Many of the first 2-10 moves have been mapped out and analyzed quite thoroughly, and all the best chess programs out there use such a database.
  • End-game database: There are databases out there for 5-6 pieces on the board where the entire possible game is completely mapped out. What an advantage! A computer with a hookup to these will be able to know the perfect path to checkmate or stalemate, whichever is better for it given the current board situation.
  • Some people think having 2 bishops is better than having 2 knights or a knight and a bishop. Is it accurate? I dunno, the jury's out.
  • We could get a lot more complicated with things like stacked rooks on a file, rook(s) on the 7th rank, x-rays, forks, etc, but these are also a topic best handled outside the blog. 

Board Evaluation - Material

For now we'll use somewhat of a standard in the chess evaluation world.We'll use a numeric evaluation of the board, where a positive number means white has a better position and a negative number means black has a better position. Pawns will count for 1 point, Knights and Bishops 3, Rooks 5, Queen 9, and the King is immeasurable in terms of value.

I've babbled enough, let's do some code! Funny how a coding blog can have over a full page of text before any code is even discussed huh? :P. If you need to download the source code from the last blog post do so now.

First, it's possible I may want (or you may want) to have multiple algorithms for board evaluation and best move calculation, so I want these driven by interfaces. Start by creating an interface in BlogChess.Backend.dll called IPositionEvaluator.

We'll just have a single property to represent the game, and a single method in there called Evaluate, which accepts no parameters and returns a value of type double.

namespace BlogChess.Backend
{
    public interface IPositionEvaluator
    {
        ChessGame Game { get; set; }
        double Evaluate(ChessGame game);
    }
}



Now create a new DLL in our solution to hold the AI logic. We'll call it BlogChess.AI.Basic1. Create a class in the DLL called PositionEvaluator. In this new dll add a reference to BlogChess.Backend.dll. Make sure the new class implements the new interface. You should have something like this so far:

using System;
using BlogChess.Backend;

namespace BlogChess.AI.Basic1
{
    public class PositionEvaluator : IPositionEvaluator
    {
        public ChessGame Game { get; set; }
        public double Evaluate()
        {
            throw new NotImplementedException();
        }
    }
}


I also want, for now, a new constants class here in the AI dll to hold a few things. Go ahead and create it, call it AIConstants.






For starters there won't be much in here, just some piece values. Hearken back to the BlogChess.Backend.Constants class where pieces on the board have a numeric representation of -6 thru +6 and you can see that these piece evaluation values in the below code snippet line up with those numeric representational values as far as indexing is concerned:

namespace BlogChess.AI.Basic1
{
    public class AIConstants
    {
        public static double[] PieceValues =
        { 
            0, //no piece on square
            1, //pawn
            3, //knight
            3, //bishop
            5, //rook
            9, //queen
            100000, //king
        };
    }
}



As you can see, nothing fancy in here. We're just defining some numeric values to represent how much a piece is worth.

I also wish to create another new class, we'll call this one MaterialEvaluator. Why do I want this separated out from the PositionEvaluator class? It seems like I could just as easily put the different evaluation criteria right there in the Evaluate method. However, there's a catch to putting it all in the same method; it's harder to verify that the code is doing what it should. It will be much easier to unit test this stuff if we separate out the different evaluation criteria into their own classes. I'm all about the unit testing, and I'm the one writing the blog so we're doing it my way! Go ahead and create that MaterialEvaluator class in the BlogChess.AI.Basic1 dll. Make it look like this:

using System;
using System.Linq;
using BlogChess.Backend;

namespace BlogChess.AI.Basic1
{
    public class MaterialEvaluator : IPositionEvaluator
    {
        public ChessGame Game { get; set; }
        protected ChessBoard CurrentPosition
        {
            get { return Game.Positions.Last(); }
        }

        public MaterialEvaluator(ChessGame game)
        {
            if (game == null)
                throw new ArgumentNullException("The game cannot be null", "game");
            if (game.Positions == null || game.Positions.Count() == 0)
                throw new ArgumentException("The game must have at least 1 position");
            Game = game;
        }

        public double Evaluate()
        {
            double result = 0.0;
            for (short row = 0; row < 8; row++)
            {
                for (short col = 0; col < 8; col++)
                {
                    var piece = CurrentPosition.Board[row, col];
                    result += AIConstants.PieceValues[Math.Abs(piece)] * Math.Sign(piece);
                }
            }
            return result;
        }
    }
}


As you can see, I went ahead and implemented the IPositionEvaluator interface on this class even though it's not meant to be a top-level evaluator. Why? Well it just made sense really. It needs a reference to a game object and it needs a single Evaluate() method, so it just fit right.We also gave it a constructor that takes a Game parameter, as the class needs to know what it's evaluating. The property CurrentPosition is just a helper property that we can use instead of having to call Game.Positions.Last repeatedly. Within the evaluate method all we do is loop through the squares of the board, and add up the material values contained therein.

We have one more task to do before we get back to the PositionEvaluator class that we created...we are going to evaluate the game based on the # of moves available to each player. For now we'll keep it simple and just value each available move exactly the same based on a constant. Go ahead and add the new constant to AIConstants.cs as below (new const is named AvailableMoveValue).

namespace BlogChess.AI.Basic1
{
    public class AIConstants
   {
        public static double[] PieceValues =
        { 
            0, //no piece on square
            1, //pawn
            3, //knight
            3, //bishop
            5, //rook
            9, //queen
            100000, //king
        };

        public const double AvailableMoveValue = 0.05;
    }
}


And we of course need to use this new constant somewhere. Guess what? Another class :). Create a new class in the AI dll named AvailableMovesEvaluator. This is also a fairly simple class, though it has slightly more meat than the one before. Here's a look-see at the class, I'll explain it after you take a look at the code:

using System;
using System.Collections.Generic;
using System.Linq;
using BlogChess.Backend;

namespace BlogChess.AI.Basic1
{
    public class AvailableMovesEvaluator : IPositionEvaluator
    {
        public ChessGame Game { get; set; }
        protected IList<ChessBoard> ValidMoves { get; set; }
        protected ChessValidMoveCalculator ValidMoveCalculator { get; set; }
        protected ChessBoard CurrentPosition
        {
            get { return Game.Positions.Last(); }
        }

        public AvailableMovesEvaluator(ChessGame game, IList<ChessBoard> validMoves, ChessValidMoveCalculator validMoveCalculator)
        {
            if (game == null)
                throw new ArgumentNullException("The game cannot be null", "game");
            if (game.Positions == null || game.Positions.Count() == 0)
                throw new ArgumentException("The game must have at least 1 position");
            if (validMoves == null)
                throw new ArgumentNullException("The validMoves cannot be null (it can be empty though)", "validMoves");
            if (validMoveCalculator == null)
                throw new ArgumentNullException("The validMoveCalculator cannot be null", "validMoveCalculator");
            Game = game;
            ValidMoves = validMoves;
            ValidMoveCalculator = validMoveCalculator;
        }

        public double Evaluate()
        {
            double result = 0.0;
            var colorMultiplier = Game.GameStatus == GameStatus.WhitesTurn ? Constants.WhiteTransform : Constants.BlackTransform;
            result += colorMultiplier * ValidMoves.Count() * AIConstants.AvailableMoveValue;
            var originalStatus = Game.GameStatus;
            Game.GameStatus = Game.GameStatus == GameStatus.WhitesTurn ? GameStatus.BlacksTurn : GameStatus.WhitesTurn;
            var oppositeMoves = ValidMoveCalculator.CalculateValidMoves();
            colorMultiplier = Game.GameStatus == GameStatus.WhitesTurn ? Constants.WhiteTransform : Constants.BlackTransform;
            result += colorMultiplier * oppositeMoves.Count() * AIConstants.AvailableMoveValue;
            Game.GameStatus = originalStatus;
            return result;
        }
    }
}



Again, quite similar to the last class. The only difference really, other than the criteria used for evaluation of course, is that we have 2 more parameters in the constructor. These are here purely for the sake of efficiency. We'll be evaluating potentially millions of board positions in a short amount of time, and rather than creating and calculating extra lists of valid moves and move calculation objects, I would rather pass them in. I need them in the Evaluate method anyways, as you can see in the above code.

Now back to the main evaluation class that we created earlier, PositionEvaluator.We need to check for checkmate and stalemate, then call our other 2 evaluation classes. This code is pretty simple:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using BlogChess.Backend;

namespace BlogChess.AI.Basic1
{
    public class PositionEvaluator : IPositionEvaluator
    {
        public ChessGame Game { get; set; }
        protected ChessBoard CurrentPosition
        {
            get { return Game.Positions.Last(); }
        }

        public PositionEvaluator(ChessGame game)
        {
            if (game == null)
                throw new ArgumentNullException("The game cannot be null", "game");
            if (game.Positions == null || game.Positions.Count() == 0)
                throw new ArgumentException("The game must have at least 1 position");
            Game = game;
        }

        public double Evaluate()
        {
            //setup some locals for later use
            double result = 0.0;
            var validMoveCalculator = new ChessValidMoveCalculator(Game);
            var validMoves = validMoveCalculator.CalculateValidMoves();
            var isKingInCheck = validMoveCalculator.IsKingInCheck(Game.GameStatus == GameStatus.WhitesTurn, CurrentPosition);

            //look for checkmate
            if (validMoves.Count() == 0 && isKingInCheck)
                return (Game.GameStatus == GameStatus.BlacksTurn) ? AIConstants.PieceValues[Constants.King] : -AIConstants.PieceValues[Constants.King];
            //look for draw
            if (validMoves.Count() == 0 && !isKingInCheck)
                return 0;

            //evaluate based on non-victory/draw conditions
            result += new MaterialEvaluator(Game).Evaluate();
            result += new AvailableMovesEvaluator(Game, validMoves, validMoveCalculator).Evaluate();

            return result;
        }
    }
}


Just like I said, simple. We look for victory, draw, and then delegate the rest of our activity to those other 2 classes we created.

What's Next?

Regarding the calculation of future board positions, you can take my word for it that we will not be able to figure out the best move by evaluating every possible chess position available. If today's supercomputers can't do it, we're not going to accomplish it with a starter web service. We'll go into this final piece of the AI more in-depth next week, but suffice it to say we will barely scratch the surface of chess AI programming with our simple board evaluation and the decision tree next week.

For those of you who wish to play with a copy of the latest code, I've placed a link to it below here. One thing I didn't cover in the blog this week is unit testing of the new code. Take my word for it that I did unit test the new code and it did in fact reveal some bugs that were eradicated before I put them in the blog post. You can find the new unit tests within the source code in a dll named BlogChess.AI.Basic1.Test.

The Code

Resources

Wikipedia - Computer Chess
Wikipedia - Elo Rating System

Thursday, March 27, 2014

A Chess Project, Part 10

Intro

We got a good start on our unit testing in the Chess Project Part 9 posting last week. We unit tested every possible move for white pieces, created a spiffy new html format for chess boards to ease our unit testing, and we tested many of our helper methods in other classes as well. This time around we will finish up unit testing the current suite of functionality. We have to add unit tests for some of the valid black piece moves (I only deem it worth our time to create tests for moves unique to black; his pawns and king castling moves, make sure black pieces can't put their own king in check) and we have just a few helper methods within ChessValidMoveCalculator to test out. Then we're green to move on to the super-fun part (next week), starting to figure out what the "best" move is for a given chess board!

The Code

Like last week, I think it best to start off by giving you this link to the code. There are too many new unit tests forming too much code to paste it in the blog, so at your leisure please download it from the link, open it up, build it, and take a look at the new unit tests. Then come on back so we can discuss them.

Unit Tests

We'll start with pawn moves. Obviously black's pawns move down the board while white's pawns move up the board, so their move calculations are different. This is why I decided to unit test black pawns separately from white pawns. You can see by looking at the unit test file that I mirrored all the white unit tests with black as far as pawns are concerned, so we should have full coverage.

I'm going to skip unit testing of black Knight, Bishop, Rook, and Queen moves. Why? These pieces move exactly the same whether they are white or black. The only difference is that white pieces can't put the white king in check, and black pieces can't put the black king in check, but I don't have to duplicate all my tests to check 1 situation per color. I did however recreate the majority of the king's unit tests as castling is different (white castles on row 7, black on row 0), so you can see in our unit test code that most but not all of the king testing is duplicated from white to black. I also renamed some of the unit tests to better denote color and direction.

We also added 8 more unit tests (4 each) for ColorThreatensSquare and IsKingInCheck, testing the positive and negative and black/white of each method.

You may have also noticed I had to fix more code. This just reaffirms my love of unit tests. See if you can find the code I had to fix. Here's a hint: it had to do with checking if a move would put the same-color king in check.

What's Next?

That's it for this week folks! We have 1 solution, 4 projects, dozens of classes, 80 unit tests, 1 web API, etc etc. Next week we'll FINALLY get to the whole point of this project, calculating the "best" move. I haven't a clue how long that will take us to do, it could be 1 post or 5, we'll just have to wait and see. 

Thursday, March 20, 2014

A Chess Project, Part 9

Intro

As promised last week in Part 8, this week is all about the unit tests. We've got a pretty sizable chunk of code going in this project and so far we have no way to test any of it. I don't plan on creating a GUI for this solution (at least not in this series of blog posts), so unit testing is the way to go. Please view my unit testing series of blog posts (UT1, UT2, UT3, UT4) for a quick refresher on unit testing if necessary.

The Code

I think it will be easier to understand what's going on if I give you a link to the code at the top of the article this time, so here you go. Once you have it pulled down, extracted and compiled come on back for the remainder of the article.

Unit Tests

As you can see in the screenshot below, I created a new unit test project in the solution named BlogChess.Backend.Test.
This is where all our unit tests for the project are going. As of the time I am writing this, there are 57 unit tests in the solution. That is a bit too many for us to walk through in the blog, so at your leisure please dig through the code and see what's going on. The unit tests at this point cover the vast majority of our functionality from the backend dll. We are testing the validation code, board size, and all the valid moves for white. The only things left to test are the black piece move validation methods, and we'll do that in the next blog post.

As a result of all this unit testing, I found 2 other things necessary. First, I had to fix some bugs. I know what you're thinking: "But Pete, you don't make mistakes!". Well I appreciate the kind words (you were thinking them, admit it!), but yes I actually do make mistakes. I bet I fixed a half-dozen of them thanks to the unit tests. I won't detail all of them (mostly because I don't remember what they were at this point), but suffice it to say they would have made this project rather useless if they had been left to fester. The second thing I found necessary while creating unit tests was to have a way to visualize a chess board so that I could set up specific positions in the unit tests, and validate piece movement. Cue dramatic new bold sub-heading...

New Format, New Formatter/Parser (use of html agility pack)

...So, I created a new chess format. Sure arrays are great for representing a chess board, but which is easier to work with when creating a unit test? This...

{ {-4, -2, -3, -5, -6, -3, -2, -4}, {-1, -1, -1, -1, -1, -1, -1, -1}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}, {1, 1, 1, 1, 1, 1, 1, 1}, {4, 2, 3, 5, 6, 3, 2, 4} }


or this?

So, I invented a new chess format (it sounds a lot more impressive than it really is) and created a class to import the new format. Believe it or not, the above screenshot is pure html/css. There are no images in that. Well the screenshot itself is an image obviously, but it's showing just html/css.

How did we accomplish this? If you're moderately familiar with html/css, it's easier than you might think. It turns out that unicode defines special characters for each of the chess pieces, so as long as the font you are using supports these values, then you can use plain-old-text to represent the pieces. After that it's just a simple matter of putting a board around the pieces you want, and voila! You can look at some of the many samples I put in the solution, using the screenshot below as a guide to finding the files. The essence of the export format though is much simpler than these files allude to. All you need is elements on the page that have a data-col attribute of "a" through "h", and those same elements should have a data-row attribute of "1" through "8". These of course represent the 64 squares of the chess board. For ease of formatting I've elected to use a table (you could use div's, span's, or whatever other html element you feel like using), a modified sample of which you can see below.

    <table>
        <tbody>
<tr>
            <td>8</td>
            <td data-col="a" data-row="8"></td>
            <td data-col="b" data-row="8"></td>
            <td data-col="c" data-row="8"></td>
            <td data-col="d" data-row="8"></td>
            <td data-col="e" data-row="8">♚</td>
            <td data-col="f" data-row="8"></td>
            <td data-col="g" data-row="8"></td>
            <td data-col="h" data-row="8"></td>
        <tr>
</tbody></table>

For some more thorough and much cleaner looking samples with css to format them nicely, take a look at any of the unit test files in the solution as you see highlighted below. These samples all import perfectly fine using our new formatter object, and are also very easy to use the mark-1 eyeball to see what's going on with the board itself by just viewing the file in any modern web browser. Again the above html table is just a sample; obviously you'd need the other 56 squares to make a full board :)




Now, on to the design and code of the formatter. In the future I can see myself wanting to import and export various other chess formats/notations (there are plenty of them), so let's use an interface to represent any type of formatter. We'll call the interface IChessBoardFormatter. Then, because the format we want right now is html based, we'll just call the class HtmlChessBoardFormatter and have the class implement the new interface.


IChessBoardFormatter defines 4 methods: Import, Export, ExternalPieceToNative, NativePieceToExternal. Import and Export should be pretty self-explanatory. ExternalPieceToNative converts an external representation of a piece (in whatever format) to a native piece (a short value from -6 (black king) to +6 (white king). NativePieceToExternal just goes the other direction.

HtmlChessBoardFormatter is the class we'll use to translate boards between our internal representation(arrays of short) and the new human-friendly format (html). For the moment I didn't implement the capability to export; just import. For now I only need this class in order to read in html files for unit testing. It's too much code to put in this blog post, so open up HtmlChessBoardFormatter and take a look at the Import and ExternalPieceToNative methods; they're where the magic is.

If you've never used the Html Agility Pack I highly recommend it for your HTML parsing needs in .net. I've used it in the Import method of the formatter class in order to find html elements with a data-row and data-col attribute, as they are the elements that contain our chess pieces. Here's a sample of it:

            var document = new HtmlDocument();
            document.LoadHtml(sourceBoard);
            foreach (var node in document.DocumentNode.SelectNodes("//*[@data-row]"))
            {
                var sourceRow = node.Attributes["data-row"].Value;
                var sourceCol = node.Attributes["data-col"].Value;


Spiffy huh? Resilient html parsing made easy in .net.

What's Next?

As you may have guessed, we'll have to hit up the unit tests for black pieces/movement and any other public methods of the valid move calculator next week. I can finally see the light at the end of the space-time continuum though, we're almost ready to start coding our "best move" logic! And, thanks to these unit tests, we'll actually be able to suggest valid moves.

Resources

Wikipedia Chess
Wikipedia en Passant
Wikipedia Chess Symbols in UnicodeHtml Agility Pack

Thursday, March 13, 2014

A Chess Project, Part 8

Intro

We're moving right along with this sizable project, a Web API that will tell you the "best" move to make for a given chess position/game. We've created a test page, a Web API for clients to call, and we're still in the middle of calculating valid moves. If you need a refresher, look back through the history of this blog and find parts 1-7; they'll give you the info you need. On to part 8!

Can't Put Your Own King in Check

As I mentioned last week, there is still one major restriction we need to code: you cannot make a move that puts your own king in check. Here is an illustration of such an illegal move:

(white's turn)


Normally the white pawn at d2 (the one with the red arrow pointing at it) could move forward/up one square to d3, or two squares to d4. However, because the black bishop on b4 would threaten the king if the pawn moved out of the way, the pawn cannot in fact move from its current location. This is the type of move we are trying to prevent, so let's figure out how to do that.

Logically speaking, the best thing I can come up with is to pretend that the white piece has moved, and then check the valid moves of all the black pieces. If a black piece would be capable of "taking" the white king, then the previous move was invalid. Keep in mind that this invalidity could count for any type of piece; a pawn move can't put your king in danger, a rook move can't, and so on. This means a logical place to put the code would be in AddMoveToList, as it's already called every single time we add a move to, well, the list.

This isn't where we'll start though. We'll first create a method that tells us if the king is in check. We'll be creative and call it IsKingInCheck. Here's the code for it:

        protected bool IsKingInCheck(bool colorIsWhite, ChessBoard board)
        {
            var kingPosition = FindKing(colorIsWhite, board);
            return ColorThreatensSquare(!colorIsWhite, board, kingPosition.Item1, kingPosition.Item2);
        }


Pretty short and sweet; we find the king, and then return a boolean telling us if the opposite color threatens the square that the king is on. We haven't yet created the FindKing function though, so let's do that now:

        protected Tuple<short, short> FindKing(bool colorIsWhite, ChessBoard board)
        {
            short kingPieceValue = (short)(Constants.King * (colorIsWhite ? Constants.WhiteTransform : Constants.BlackTransform));
            for (short row = 0; row < 8; row++)
            {
                for (short col = 0; col < 8; col++)
                {
                    if (board.Board[row, col] == kingPieceValue)
                        return new Tuple<short, short>(row, col);
                }
            }
            throw new Exception("King not found");
        }


This method determines what the short constant value is for the king based on the color we are looking for, then loops through the board to find the king on it. If there is no king we throw an exception.

It's worth pointing out now that we only need to traverse 1 branch into the tree to check for checks; after all, when you're checking if a piece threatens the king, you don't really care if the piece can actually make the move; you only care if the square is threatened. Because of this we now need to create a new field of class ChessValidMoveCalculator that tells us whether we should allow multi-layer traversal of the tree:

protected bool m_allowCheckSubTrees = true;


If you'll recall, IsKingInCheck calls the pre-existing function ColorThreatensSquare. It is in this function that we calculate the next color's valid moves to see if a square is threatened, so it's in this method that we need to set m_allowCheckSubTrees to false. Here's the updated version of this function:

        public bool ColorThreatensSquare(bool colorIsWhite, ChessBoard board, short row, short col)
        {
            var game = new ChessGame();
            var positions = new List<ChessBoard>();
            positions.Add(new ChessBoard(true) { Board = (short[,])board.Board.Clone() });
            game.Positions = positions.ToList();
            game.GameStatus = colorIsWhite ? GameStatus.WhitesTurn : GameStatus.BlacksTurn;
            var futureCalculator = new ChessValidMoveCalculator(game);
            futureCalculator.m_allowCheckSubTrees = false;
            var futurePositions = futureCalculator.CalculateValidMoves();
            var colorSign = colorIsWhite ? Constants.WhiteTransform : Constants.BlackTransform;
            foreach (var position in futurePositions)
                if (Math.Sign(position.Board[row, col]) == colorSign)
                    return true;
            return false;
        }


What did we do to this method? We first set the game.GameStatus to the turn of the appropriate color, based on who we are checking using the parameter colorIsWhite. I realized when I opened up this unit that I had never set whose turn it is in this "future" game, so I needed to do that in order to keep things from splodin. I then set futureCalculator.m_allowCheckSubTrees to false, so that we don't check to make sure the piece can actually move to determine if it threatens a square, as we don't want a nigh-infinite traversal of our logic tree.

The last thing we need to do is call our new method IsKingInCheck. As I said a few paragraphs ago, the best place to do this is in AddMoveToList.

        protected IList<ChessBoard> AddMoveToList(ChessBoard startingBoard, IList<ChessBoard> boards, short oldRow, short oldCol, short newRow, short newCol)
        {
            var resultArray = new ChessBoard[boards.Count];
            boards.CopyTo(resultArray, 0);
            var result = resultArray.ToList();
            if (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8)
            {
                //check the sign (color) of the current square and future square; if same, don't allow the move
                var startingPieceSign = Math.Sign(startingBoard.Board[oldRow, oldCol]);
                var futurePieceSign = Math.Sign(startingBoard.Board[newRow, newCol]);
                if (startingPieceSign != futurePieceSign)
                {
                    var futureBoard = (short[,])startingBoard.Board.Clone();
                    var piece = futureBoard[oldRow, oldCol];
                    futureBoard[oldRow, oldCol] = Constants.Empty;
                    futureBoard[newRow, newCol] = piece;
                    var newBoard = new ChessBoard(true);
                    newBoard.Board = futureBoard;
                    //check if new board and starting board are the same; if so don't add to valid moves list
                    var equal = futureBoard.Rank == startingBoard.Board.Rank &&
                        Enumerable.Range(0, futureBoard.Rank).All(dimension => futureBoard.GetLength(dimension) == startingBoard.Board.GetLength(dimension)) &&
                        futureBoard.Cast<short>().SequenceEqual(startingBoard.Board.Cast<short>());
                    //make sure the move wouldn't put the current color's king in check
                    var wouldPutSameColorKingInCheck = false;
                    if (m_allowCheckSubTrees)
                    {
                        var futureBoardContainer = new ChessBoard(true);
                        futureBoardContainer.Board = futureBoard;
                        wouldPutSameColorKingInCheck = IsKingInCheck(m_game.GameStatus == GameStatus.BlacksTurn, futureBoardContainer);
                    }
                    //add move to list of valid moves
                    if (!equal && !wouldPutSameColorKingInCheck)
                        result.Add(newBoard);
                }
            }
            return result;
        }


The addition to this function starts with the comment "make sure the move wouldn't put the current color's king in check". Here we first determine if we're supposed to check sub-trees, because if not then we don't care if the move would put the king in check. If we are allowed to check sub-trees we set a local variable wouldPutSameColorKingInCheck to the appropriate value by calling IsKingInCheck, passing in the potential future board. Lastly we add the move to our list of valid moves only if it would not put the king in check.

What's Next?

We're moving right along. Next week we'll create our unit tests. We won't make any actual progress on the "best" move calculation while creating our tests, but we'll give ourselves better confidence that what we've done so far is actually correct and most likely we'll be able to find and fix some bugs.

Here's the link to the current source code.

Resources

  • Online chess board editor "Apronus"

Thursday, March 6, 2014

A Chess Project, Part 7

Intro

Here we are in part 7 of the chess project. Last week we coded most of the valid moves available to our chess pieces. This week we'll tackle the remainder. What's left? En passant pawn captures and castling.


En Passant

I'm going to try not to plagiarize here, but I'm sure quite a bit has been written about en-passant so if you recognize these words, sorry. An en-passant capture is a pawn move where it captures an enemy pawn that has just moved two squares forward, pretending the target pawn has moved only a single square forward. The capturer must be on it's color's 5th rank. It's kind of hard to picture so let me refer you to this Wikipedia article which has some nice pictures.

Open up ChessValidMoveCalculator. Skip down to CalculatePawnValidMoves, as this is where we'll be doing our work. We already have calculations in there for moving forward-left and forward-right, and an en passant capture is a special case of these 2 moves. I won't bore you with too much detail, nor will I claim that this code is 100% perfect (darn my lack of unit testing!). Below you will see the modified portion ChessValidMoveCalculator.

                //forward-left diagonal (only if capturing opponent's piece)
                rowMovementAmount = (short)(1 * rowModifier);
                newRow = (short)(currentRow + rowMovementAmount);
                newCol = (short)(currentCol - 1);
                if (newRow >= 0 && newRow <= 7 && newCol >= 0 && newCol <= 7)
                {
                    if (Math.Sign(m_game.Positions.Last().Board[newRow, newCol]) == -Math.Sign(m_game.Positions.Last().Board[currentRow, currentCol]))
                    {
                        if (newRow == 0 || newRow == 7) //back rank promotions
                        {
                            for (short promotionPieceIndex = Constants.Knight; promotionPieceIndex <= Constants.Queen; promotionPieceIndex++)
                            {
                                short promotionPiece = (short)(promotionPieceIndex * rowModifier);
                                boards = AddMoveToList(m_game.Positions.Last(), boards, currentRow, currentCol, newRow, newCol);
                            }
                        }
                        else
                            boards = AddMoveToList(m_game.Positions.Last(), boards, currentRow, currentCol, newRow, newCol);
                    }
                    else if (m_game.Positions.Count() > 1) //big chunk of logic to see if en passant is possible
                    {
                        if ((currentRow == 3 && currentStatus == GameStatus.WhitesTurn) || (currentRow == 4 && currentStatus == GameStatus.BlacksTurn))
                        {
                            var priorPosition = m_game.Positions.ToArray()[m_game.Positions.Count() - 1].Board;
                            //pawn of opposite color moved up 2 squares next to the current piece
                            if (priorPosition[newRow + rowModifier, newCol] == Constants.Pawn * rowModifier && m_game.Positions.Last().Board[currentRow, newCol] == Constants.Pawn * rowModifier) 
                            {
                                boards = AddMoveToList(m_game.Positions.Last(), boards, currentRow, currentCol, newRow, newCol);
                            }
                        }
                    }
                }
                //forward-right diagonal (only if capturing opponent's piece)
                rowMovementAmount = (short)(1 * rowModifier);
                newRow = (short)(currentRow + rowMovementAmount);
                newCol = (short)(currentCol + 1);
                if (newRow >= 0 && newRow <= 7 && newCol >= 0 && newCol <= 7)
                {
                    if (Math.Sign(m_game.Positions.Last().Board[newRow, newCol]) == -Math.Sign(m_game.Positions.Last().Board[currentRow, currentCol]))
                    {
                        if (newRow == 0 || newRow == 7) //back rank promotions
                        {
                            for (short promotionPieceIndex = Constants.Knight; promotionPieceIndex <= Constants.Queen; promotionPieceIndex++)
                            {
                                short promotionPiece = (short)(promotionPieceIndex * rowModifier);
                                boards = AddMoveToList(m_game.Positions.Last(), boards, currentRow, currentCol, newRow, newCol);
                            }
                        }
                        else
                            boards = AddMoveToList(m_game.Positions.Last(), boards, currentRow, currentCol, newRow, newCol);
                    }
                    else if (m_game.Positions.Count() > 1) //big chunk of logic to see if en passant is possible
                    {
                        if ((currentRow == 3 && currentStatus == GameStatus.WhitesTurn) || (currentRow == 4 && currentStatus == GameStatus.BlacksTurn))
                        {
                            var priorPosition = m_game.Positions.ToArray()[m_game.Positions.Count() - 1].Board;
                            //pawn of opposite color moved up 2 squares next to the current piece
                            if (priorPosition[newRow + rowModifier, newCol] == Constants.Pawn * rowModifier && m_game.Positions.Last().Board[currentRow, newCol] == Constants.Pawn * rowModifier)
                            {
                                boards = AddMoveToList(m_game.Positions.Last(), boards, currentRow, currentCol, newRow, newCol);
                            }
                        }
                    }
                }


There are 2 additions, and both are else chunks that have a comment with "en passant" in it. If the current piece is a pawn, and the pawn is on the 5th rank for it's appropriate color, and if the prior move saw a pawn move 2 squares to get next to the current pawn, then en passant is available.


Castling

Castling is an important defensive maneuver in chess whereby the king gets to move 2 squares towards the edge of the board and the rook to that same side jumps 1 square to the opposite side of the king. In the early part of the game this has 2 important purposes: the first part is this gets the king nestled into the easily-defensible corner, usually behind a shield of protective pawns. This gives the king disposable cover that is tough to break through cheaply. The second purpose is it brings the rook closer into play. Rooks are more valuable in the center of the board where they have more room to roam.

 The king can castle in either direction (left and right), but with these limitations:
  1. there can be no other pieces between the king and the rook.
  2. the king and rook must still be on their starting square (they cannot have moved yet during the game).
  3. None of the opponents pieces may threaten squares that the king has to move through, to, or from.
  4. And of course you can't make any move that puts your king in check, including castling.

As you've guessed, the logic for this maneuver will go into the method CalculateKingValidMoves. As you can see below, I've split the castling logic out into 2 sections: 1 for black and 1 for white. It just looks a little cleaner this way:

        protected IList<ChessBoard> CalculateKingValidMoves(short currentRow, short currentCol, IList<ChessBoard> boards, GameStatus currentStatus)
        {
            short newRow;
            short newCol;
            short rowMovementAmount;
            short colMovementAmount;
            bool isWhitePiece = m_game.Positions.Last().Board[currentRow, currentCol] > 0;
            bool isBlackPiece = m_game.Positions.Last().Board[currentRow, currentCol] < 0;
            if ((currentStatus == GameStatus.BlacksTurn && isBlackPiece) || (currentStatus == GameStatus.WhitesTurn && isWhitePiece))
            {
                //+1+1,+1-1,-1+1,-1-1
                for (rowMovementAmount = -1; rowMovementAmount <= 1; rowMovementAmount++)
                {
                    for (colMovementAmount = -1; colMovementAmount <= 1; colMovementAmount++)
                    {
                        newRow = (short)(currentRow + rowMovementAmount);
                        newCol = (short)(currentCol + colMovementAmount);
                        boards = AddMoveToList(m_game.Positions.Last(), boards, currentRow, currentCol, newRow, newCol);
                    }
                }
                //black castle
                if (currentStatus == GameStatus.BlacksTurn && isBlackPiece && currentCol == 4 && currentRow == 0)
                {
                    //castle west
                    if (m_game.Positions.Last().Board[0, 0] == Constants.Rook * Constants.BlackTransform &&
                        m_game.Positions.Last().Board[0, 1] == Constants.Empty &&
                        m_game.Positions.Last().Board[0, 2] == Constants.Empty &&
                        m_game.Positions.Last().Board[0, 3] == Constants.Empty)
                    {
                        var futureBoard = (short[,])m_game.Positions.Last().Board.Clone();
                        futureBoard[0, 2] = Constants.King * Constants.BlackTransform;
                        futureBoard[0, 3] = Constants.Rook * Constants.BlackTransform;
                        boards = boards.ToList();
                        bool anySquareThreatened = false;
                        anySquareThreatened = anySquareThreatened || ColorThreatensSquare(true, m_game.Positions.Last(), 0, 0);
                        anySquareThreatened = anySquareThreatened || ColorThreatensSquare(true, m_game.Positions.Last(), 0, 1);
                        anySquareThreatened = anySquareThreatened || ColorThreatensSquare(true, m_game.Positions.Last(), 0, 2);
                        anySquareThreatened = anySquareThreatened || ColorThreatensSquare(true, m_game.Positions.Last(), 0, 3);
                        if (!anySquareThreatened)
                            boards.Add(new ChessBoard(true) { Board = futureBoard });
                    }
                    //castle east
                    if (m_game.Positions.Last().Board[0, 7] == Constants.Rook * Constants.BlackTransform &&
                        m_game.Positions.Last().Board[0, 6] == Constants.Empty &&
                        m_game.Positions.Last().Board[0, 5] == Constants.Empty)
                    {
                        var futureBoard = (short[,])m_game.Positions.Last().Board.Clone();
                        futureBoard[0, 6] = Constants.King * Constants.BlackTransform;
                        futureBoard[0, 5] = Constants.Rook * Constants.BlackTransform;
                        boards = boards.ToList();
                        bool anySquareThreatened = false;
                        anySquareThreatened = anySquareThreatened || ColorThreatensSquare(true, m_game.Positions.Last(), 0, 7);
                        anySquareThreatened = anySquareThreatened || ColorThreatensSquare(true, m_game.Positions.Last(), 0, 6);
                        anySquareThreatened = anySquareThreatened || ColorThreatensSquare(true, m_game.Positions.Last(), 0, 5);
                        anySquareThreatened = anySquareThreatened || ColorThreatensSquare(true, m_game.Positions.Last(), 0, 4);
                        if (!anySquareThreatened)
                            boards.Add(new ChessBoard(true) { Board = futureBoard });
                    }
                }
                //white castle
                if (currentStatus == GameStatus.WhitesTurn && isWhitePiece && currentCol == 4 && currentRow == 7)
                {
                    //castle west
                    if (m_game.Positions.Last().Board[7, 0] == Constants.Rook &&
                        m_game.Positions.Last().Board[7, 1] == Constants.Empty &&
                        m_game.Positions.Last().Board[7, 2] == Constants.Empty &&
                        m_game.Positions.Last().Board[7, 3] == Constants.Empty)
                    {
                        var futureBoard = (short[,])m_game.Positions.Last().Board.Clone();
                        futureBoard[7, 2] = Constants.King;
                        futureBoard[7, 3] = Constants.Rook;
                        boards = boards.ToList();
                        bool anySquareThreatened = false;
                        anySquareThreatened = anySquareThreatened || ColorThreatensSquare(false, m_game.Positions.Last(), 7, 0);
                        anySquareThreatened = anySquareThreatened || ColorThreatensSquare(false, m_game.Positions.Last(), 7, 1);
                        anySquareThreatened = anySquareThreatened || ColorThreatensSquare(false, m_game.Positions.Last(), 7, 2);
                        anySquareThreatened = anySquareThreatened || ColorThreatensSquare(false, m_game.Positions.Last(), 7, 3);
                        if (!anySquareThreatened)
                            boards.Add(new ChessBoard(true) { Board = futureBoard });
                    }
                    //castle east
                    if (m_game.Positions.Last().Board[7, 7] == Constants.Rook &&
                        m_game.Positions.Last().Board[7, 6] == Constants.Empty &&
                        m_game.Positions.Last().Board[7, 5] == Constants.Empty)
                    {
                        var futureBoard = (short[,])m_game.Positions.Last().Board.Clone();
                        futureBoard[7, 6] = Constants.King;
                        futureBoard[7, 5] = Constants.Rook;
                        boards = boards.ToList();
                        bool anySquareThreatened = false;
                        anySquareThreatened = anySquareThreatened || ColorThreatensSquare(false, m_game.Positions.Last(), 7, 7);
                        anySquareThreatened = anySquareThreatened || ColorThreatensSquare(false, m_game.Positions.Last(), 7, 6);
                        anySquareThreatened = anySquareThreatened || ColorThreatensSquare(false, m_game.Positions.Last(), 7, 5);
                        anySquareThreatened = anySquareThreatened || ColorThreatensSquare(false, m_game.Positions.Last(), 7, 4);
                        if (!anySquareThreatened)
                            boards.Add(new ChessBoard(true) { Board = futureBoard });
                    }
                }
            }
            return boards;
        }

It's a pretty fair amount of code here; we have to check both directions since the kings and rooks can castle both left and right. We also checked to make sure no pieces are between king and rook, and made sure no opposing pieces threaten the squares they are on or that they will be passing through.

Summary and What's Next

That's it for this week folks. This stuff is getting harder each week! The good news is that there are no more movement types left to code. The bad news is we still have one major restriction to code, and that being that you cannot make a move which puts your own king in check. We will code this one in the next blog post and apply it to all of the move calculations that we have done so far. I'm also becoming more and more wary of how complicated the code is getting; I'm not really trusting the accuracy of the code. We will need to start unit testing this stuff pretty soon, possibly even in the next post. Normally I would do the unit testing alongside the actual coding, but for the sake of keeping these posts reasonable and focused I have foregone unit testing to date.

And finally, here is the code

Resources

Castling

Thursday, February 27, 2014

A Chess Project, Part 6

Intro

Here in part 6 we won't go very in-depth into the code. There were just too many changes, so I'll post a link to the full solution at the bottom of this post for you to download and peruse at your pleasure. I'll cover some of the highlights below though so you know what's different from last week.

ChessValidMoveCalculator

A number of files have been modified to make this week possible. First and foremost, ChessValidMoveCalculator. It has been very heavily modified to include the following types of chess piece movement:
  1. basic knight moves
  2. basic bishop moves
  3. basic rook moves
  4. basic queen moves
  5. basic king moves
  6. can't move a piece on top of another piece of the same color
  7. only the person whose turn it is can move
  8. pawns can't jump over pieces
  9. knights CAN jump over pieces
  10. bishops can't jump over pieces
  11. rooks can't jump over pieces
  12. queens can't jump over pieces
  13. pawns can go 2 squares only if they start on row1 or row7
  14. pawns can move diagonal only to capture piece
And I also refactored some code and weeded out a couple bugs in this unit. Have a look, you'll see this unit is starting to look pretty close to complete (at least until we code in victory conditions).

Default.aspx and Default.aspx.cs In BlogChessApiFlexer

I modified these pages only to facilitate testing of the Web API. But hey, it's still useful!

BlogChessController (the Web API Main Unit)

I also modified this unit to facilitate testing. The method PostBestMove, which is supposed to calculate and return the best move, has now been tasked with calculating and returning the complete list of valid moves. Later on when we can evaluate boards we'll set it back to its rightful purpose, but for now it's a great way to debug the code that's currently available.

Action Shot

I kinda feel like I need to show this product in action, so here's a screenshot of it doing it's thing:

The above shows the valid moves, as calculated by our code, assuming a standard chess starting position. Recall those piece values from a couple blogs ago? 0=empty square,1=pawn,2=knight,3=bishop,4=rook,5=queen,6=king, with black pieces being the negative value. Pretty cool huh? I hope you're enjoying this project, I certainly am. Lotta work though!

What's Next?

We still haven't attempted victory conditions, so we'll still have to do those at some point. We also skipped 2 types of movement that I just didn't have time for this week: pawn capture en-passant, and castling. We'll probably tackle at least those 2 next week.

And as promised, here is the solution file.

Thursday, February 20, 2014

A Chess Project, Part 5

Intro

We're on Part 5 now of this large project, and it's time for a little reflection of what's been done as well as a summary of what's left. We've covered the requirements of the project, basic project design, we created the backend web service (WebAPI), we created a sample web-based front-end to exercise the web service, and we have performed request data validation. We're well on the way to stardom! What do we have left? We need to calculate the valid moves that each piece has, we need to discover if any victory or draw conditions have been met, and we will need to do the hardest part so far...write some cheesy AI to figure out a "good" move! We'll crack open this post with valid move calculation. We can't do victory conditions without valid move calculations and vice versa, but we gotta start somewhere so let's calculate basic move actions and we'll put off victory conditions for another time. Onward and forward and such and stuff!

Basic Move Calculation

First step: we need somewhere to put these calculations. You might think that ChessBoard, or even ChessGame are a good place for this, and you'd be right. But hey so am I, I'm going to make a new class for it. I like "The Offspring", so we're gonna keep 'em separated! Go ahead and add a new class called ChessValidMoveCalculator to the BlogChess.Backend project.

This new class will need access to a ChessGame object. Otherwise how will it know of a game and board(s) for which to calculate moves? We'll give it a single private member of type ChessGame as well as a constructor where the caller can pass in a game. Here's the code:

using System;
using System.Collections.Generic;
using System.Linq;

namespace BlogChess.Backend
{
    public class ChessValidMoveCalculator
    {
        private ChessGame m_game;

        public ChessValidMoveCalculator(ChessGame game)
        {
            if (game == null)
                throw new ArgumentNullException("game", "game cannot be null");
            if (game.Positions == null || game.Positions.Count() == 0)
                throw new ArgumentException("game must contain some positions", "game");
            m_game = game;
        }
    }
}



Short and sweet, the code above has just what I said it would have. Note that in the constructor we make sure that the game isn't null and that it has some positions, otherwise there's not much point in trying to calculate moves; there wouldn't be any!

Now it's time to calculate valid moves. I think that sounds like a good method name, so add a method to this class called CalculateValidMoves. I think we're best off with having a return type that is a list of ChessBoards, so give it a return type of IList<ChessBoard>. The method needs no parameters. You should have something like this:

        public IList<ChessBoard> CalculateValidMoves()
        {
            IList<ChessBoard> result = new List<ChessBoard>();
            return result;
        }



I went ahead and created the result variable and returned it, just so the code would compile. Go ahead and try to compile yours too before we get too far.

We're getting closer to the fun part here folks! In my convoluted meat-based thinkin-tool, I believe it would be best to loop through all the squares on the board and see what's there. Then we can base our calculations on what's on the board. So, we need some sort of looping structure in the code that checks what's on what square. We'll need to do this for the last board position of the game, as we don't really care what the valid moves were in the past; we only care right now! Give it a shot yourself first (just create the basic loop structure), then drop on back here to see what I've got:

        
public IList<ChessBoard> CalculateValidMoves()
        {
            IList<ChessBoard> result = new List<ChessBoard>();
            var currentBoard = m_game.Positions.Last().Board;
            var currentStatus = m_game.GameStatus;
            if (currentStatus == GameStatus.BlacksTurn || currentStatus == GameStatus.WhitesTurn)
            {
                //calculate and return moves
                for (short row = 0; row < 8; row++)
                {
                    for (short col = 0; col < 8; col++)
                    {
                    }
                }
            }
            return result;
        }


The next step is determining what type of piece we're looking at. Each piece has its own method of movement which I will assume you know or will look up. But, we need to know the type of piece to know the type of movement, so add that to the loop. Here's my attempt:

                    for (int col = 0; col < 8; col++)
                    {
                        var piece = currentBoard[row, col];
                        switch (piece)
                        {
                            case Constants.Pawn:
                                break;
                            case Constants.Knight:
                                break;
                            case Constants.Bishop:
                                break;
                            case Constants.Rook:
                                break;
                            case Constants.Queen:
                                break;
                            case Constants.King:
                                break;
                        }
                    }


And now our first attempts at determining moves. We're going to start simple here, and pretend there are no other pieces on the board. With that in mind, what can a pawn do? It's got a few options:
  1. Move forward 1 square 
  2. Move forward 2 squares, if haven't moved before
  3. Take a piece forward and left/right
  4. Take another pawn "en passant". This is a weird rule and I might not even both with it in the blog, but hey I have to mention it.
  5. Turn into any type of chess piece when reaching the back rank.
Starting with part 1, how do we code that? First we need to know what color we're looking at. Forward means a different direction for black than it does for white. Picture the board like this:

    col
row  0 1 2 3 4 5 6 7
     1
     2
     3
     4
     5
     6
     7


With the assumption of white starting on rows 6 and 7 (it's 0-based; if we were using 1-based it would be rows 7 and 8) then forward means a decrease in the row. For black starting on rows 0 and 1, forward means an increase in the row. Let's go ahead and code moves 1 and 2 (forward 1 square, forward 2 squares).

                        var piece = currentBoard[row, col];
                        short newRow;
                        short newCol;
                        switch (piece)
                        {
                            case Constants.Pawn:
                                //1 square forward
                                short rowModifier = currentStatus == GameStatus.BlacksTurn ? Constants.BlackTransform : Constants.WhiteTransform;
                                short rowMovementAmount = (short)(1 * rowModifier);
                                newRow = (short)(row + rowMovementAmount);
                                newCol = col;
                                if (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8)
                                {
                                    var futureBoard = (short[,])currentBoard.Clone();
                                    futureBoard[row, col] = Constants.Empty;
                                    futureBoard[newRow, newCol] = piece;
                                    var newBoard = new ChessBoard(true);
                                    newBoard.Board = futureBoard;
                                    result.Add(new ChessBoard(true));
                                }
                                //2 squares forward
                                rowMovementAmount = (short)(2 * rowModifier);
                                newRow = (short)(row + rowMovementAmount);
                                newCol = col;
                                if (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8)
                                {
                                    var futureBoard = (short[,])currentBoard.Clone();
                                    futureBoard[row, col] = Constants.Empty;
                                    futureBoard[newRow, newCol] = piece;
                                    var newBoard = new ChessBoard(true);
                                    newBoard.Board = futureBoard;
                                    result.Add(new ChessBoard(true));
                                }
                                break;
                            case Constants.Knight:
                                break;
                            case Constants.Bishop:
                                break;
                            case Constants.Rook:
                                break;
                            case Constants.Queen:
                                break;
                            case Constants.King:
                                break;
                        }


As you can see, the code is getting stringier. I've got a bit of nearly-duplicated code, but maybe we'll refactor that later. For now you can see that I've got 2 new local variables, newRow and newCol. We'll use these to put our movements in. In the Pawn section of our case statement we've got some logic for 1-square forward movement and 2-square forward movement. We first determine whose turn it is, then we move the piece forward a square. We then check to see if the new square is within the bounds of our board, and if so we add the new position to our result set. I can already tell that the duplicated code is going to bug the crap outta me, and we're going to need this same code for the other pieces' valid moves too, so let's clean this up:

        protected IList<ChessBoard> AddMoveToList(IList<ChessBoard> boards, short oldRow, short oldCol, short newRow, short newCol)
        {
            var resultArray = new ChessBoard[boards.Count];
            boards.CopyTo(resultArray, 0);
            var result = resultArray.ToList();
            if (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8)
            {
                var futureBoard = (short[,])boards.Last().Board.Clone();
                var piece = futureBoard[oldRow, oldCol];
                futureBoard[oldRow, oldCol] = Constants.Empty;
                futureBoard[newRow, newCol] = piece;
                var newBoard = new ChessBoard(true);
                newBoard.Board = futureBoard;
                result.Add(new ChessBoard(true));
            }
            return result;
        }


This is our new method that adds a new board to a list of boards. Nothing fancy. We just took some of the code from CalculateValidMoves and pushed it in here, since we're going to need it many times. Now this is what you have left in CalculateValidMoves:

        public IList<ChessBoard> CalculateValidMoves()
        {
            IList<ChessBoard> result = new List<ChessBoard>();
            var currentBoard = m_game.Positions.Last().Board;
            var currentStatus = m_game.GameStatus;
            if (currentStatus == GameStatus.BlacksTurn || currentStatus == GameStatus.WhitesTurn)
            {
                //calculate and return moves
                for (short row = 0; row < 8; row++)
                {
                    for (short col = 0; col < 8; col++)
                    {
                        var piece = currentBoard[row, col];
                        short newRow;
                        short newCol;
                        switch (piece)
                        {
                            case Constants.Pawn:
                                //1 square forward
                                short rowModifier = currentStatus == GameStatus.BlacksTurn ? Constants.BlackTransform : Constants.WhiteTransform;
                                short rowMovementAmount = (short)(1 * rowModifier);
                                newRow = (short)(row + rowMovementAmount);
                                newCol = col;
                                result = AddMoveToList(result, row, col, newRow, newCol);
                                //2 squares forward
                                rowMovementAmount = (short)(2 * rowModifier);
                                newRow = (short)(row + rowMovementAmount);
                                newCol = col;
                                result = AddMoveToList(result, row, col, newRow, newCol);
                                break;
                            case Constants.Knight:
                                break;
                            case Constants.Bishop:
                                break;
                            case Constants.Rook:
                                break;
                            case Constants.Queen:
                                break;
                            case Constants.King:
                                break;
                        }
                    }
                }
            }
            return result;
        }


Looks a little cleaner neh? We replaced the duplicated code with a couple calls to AddMoveToList. Now we need to add in the forward-left and forward-right diagonals. I won't bore you with too much detail as you're getting the hang of it now, so here's the code:

                                //forward-left diagonal
                                rowMovementAmount = (short)(1 * rowModifier);
                                newRow = (short)(row + rowMovementAmount);
                                newCol = (short)(col - 1);
                                result = AddMoveToList(result, row, col, newRow, newCol);
                                //forward-right diagonal
                                rowMovementAmount = (short)(1 * rowModifier);
                                newRow = (short)(row + rowMovementAmount);
                                newCol = (short)(col + 1);
                                result = AddMoveToList(result, row, col, newRow, newCol);



This is all getting pretty easy huh? Well unfortunately as usual, I've taken up enough of your time without getting terribly far into the code. It's a lot of code and I guess I'm just a little too wordy!

I can't thank you all enough for reading this far, especially if you started from post #1. I really appreciate it folks. Hang in there through a few more posts and we'll have us a working chess api web service!

What's Next?

Next week we'll have to do some more move calculations. I won't go nearly as much into the mechanics of the movement of pieces or the code of it next week, at least not for the basic movements. That horse is already quite dead. I'll probably just push the basic code up here for the rest of the pieces and then we can move on to discussing and coding the funky movements (promoting pawns, taking pieces, castling, etc). I also think I'll end up cleaning up the code even more next week, as I think CalculateValidMoves is going to get a little unwieldy. We'll see how it goes first, but I bet we'll end up putting each individual piece type's calculation into its own method.

If you'd like to skip ahead of the class, try it out yourself! The basic movements are pretty easy for most pieces and I bet you can all get the code working yourself for the remaining pieces if you have the time. Heck even pawn movement #5 (which we didn't cover) isn't that difficult, just remember that the pawn can turn into whatever it wants to (other than a king) when it gets to the back rank!

Resources

No  special resources used this week. Have fun coding!