Visual Basic Chess Game

This source code for a chess game written in visual basic is another tutorial that is dumped into the intermediate category, however, I think it could really fit into the beginner and advanced categories too. The reason for this is that there aren’t really any advanced things going on here in terms of strict visual basic code, but the logic may be a little tough, especially as this was one of my early projects, so in some cases the naming conventions suck, ie, are really non existent, and some things are just done the long way round. However, having said that, it still works and that’s pretty cool.

This is not a tutorial in the sense that I am going to ramble on here about writing a chess game in visual basic. This is rather another tutorial where I say, here is the source code, step though it and see what you like, see what you don’t, see where you can improve, but learn from each of those steps. There really is just too much in the project to talk about here.

I will just mention a few things:

1) One of the things to learn from this code is drag and drop. This visual basic chess game uses drag and drop to drag chess pieces around the board and drop them into the square they are moving to. This is a little trickier than it may sound at first because often times the piece is not dragged and dropped cleanly into a single square. It may be 90% in a square, but not exactly. You will see that there is a little calculation that goes on to determine which square it was dropped into “the most”.

2) Many routines simplify logic. I think that I probably could have even created more routines to simplify even further. Now I am not talking about arbitarily adding routines, I am talking about drawing a flow diagram with pen and paper (remember those??), and then coding the flow diagram. By making each routine as small as possible, ie, one job per routine, you simplify your logic because at a high level, you get code that looks like this:

private sub MovePiece()
IsThisAValidMove()
MovePieceToLocation(x,y)
IsPlayerInCheck()
HasPieceTakenOpponent()
End Sub

This is NOT actual code from my source, but it illustrates the type of things I was trying to achieve (as I mentioned before, this was one of my first, so I might not have succeeded entirely).

3) Move highlighting. Because of the modular style of writing this softwareas described in the point above, it was very easy for me to add “move highlighting”. For the colour whose turn it is to move, right-click on any piece, and it highlights all the possible moves that piece can make at that time, so this could potentially help someone to learn the game.

4) There is no AI in this game yet. This game currently requires 2 people to play. There has not been any effort yet to add artificial intelligence for a computer player.

PLEASE, IF YOU START THE PROCESS OF ADDING AI TO THIS GAME, I’D LOVE TO SEE IT.

If you use this code as the start of your own project (with or without AI), PLEASE send it back to me. I will NEVER ask you for anything, ie, I don’t want your money, I don’t want royalties, I don’t even want acknowledgement in your product (although it would be nice). ALL I want is just to see where this code is going, and please, if you find this useful, please tweet about it @softsmart!

Anyway, having said that, please download the code and happy coding / gaming….

Leave a Reply

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