Monday, January 13, 2014
Python Chess Game
Games seem to be one of the most commonly used teaching tools in programming courses because they provide a convenient framework of rules and conditions that can be expanded to encompass a broad range of programming problems and techniques. That, and games are fun. And the best learning happens when the you are having fun.
One great thing about games is that they vary in complexity from things like hangman and tic-tac-toe to MMORPGs played by millions of people concurrently all around the world. Off the top of my head, games that have come up in the courses I've taken include battleship, poker, sudoku, boggle, checkers and scrabble. One game that hasn't come up much is Chess, so a while back I decided to flex my Python muscles by writing a my own Chess game just using basic Python.
Turns out Chess is a bit more complex than I had initially anticipated. Setting up a text-based board and pieces wasn't too hard, but getting all the pieces to move around correctly took a bit of trial and error. There are also a lot of special rules in chess like the pawn's ability to capture en passant and the king's ability to castle. I started just by getting the basic functionality working--pieces that could move on a board--and added the other features over time. Now I've finally added in all the basic rules and features of Chess, like castling, checking for check, disallowing moves into check, checkmate, stalemate and draws. The program isn't really meant to actually be used to play chess (the interface is just text) but it was a great exercise. I'm sure there are some bugs lurking around in corner cases and I probably used many more lines than was necessary, but I've been able to play a few full games without incident. You can check out the code on GitHub.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.