Week 8 – Python Chess

Hour 1 – Debugging and Modularizing

Yes modularizing is a word.  Look it up language Nazis.

Last week I was having an issue with a pesky error after creating the Game class to control aspects of game play and call the other functions.  The error actually resulted from mistyping a variable when I changed the calling line in the StartGame module.  Let this be a lesson to you kiddies, if you can’t figure out why a program that you are writing is being difficult most of the time it turns out to be something really simple that you’ve overlooked.  Before you wrack your brains for an hour looking for something complicated, go back and check over your code with a fine tooth comb.

I decided since the chess module was getting long and unwieldy, to split the legality and the piece definitions off into their own modules.  That is one of the big ideas about OOP: modularity.  In other words there is no good reason to pack everything into one long module if it can logically be split into separate modules.

After readjusting some of the parameters due to shifting operations to the Game class I have game initialization drawing the board, and player initialization placing the pieces.  The white pieces are in the right locations, however interaction is messed up now.  The user can select a piece, but can’t move it.

Hour 2 – Basic User Interaction

Alright, for some reason I had to move the starting positions for the black pieces up one square, but all of the pieces are on the board, and the interaction with the Legal module is working fine.  Hopefully now that some OOP house keeping has been done the turn specifications should go pretty smoothly.

Hour 3 – Defining Turns.

With the Game class running the show defining which side is in play is a snap.  I’ve been wanting to flip the board around with each move to facilitate hot-seat play (both players are using the same computer) so I set up a turnChange() module in the Game class and a flip() module in the Piece class.  The most complex part was figuring out where the pieces should end up on the other side of the board when it flipped.  There is probably some math trick that I could have worked out to transpose the position, but I settled for creating a list (or array for you VB folks) like so: [7,5,3,1,-1,-3,-5,-7].  The current position horizontally or vertically  indicates the slot in the list for how many squares the piece will move.

At first I couldn’t figure out why the game was losing track of pieces after the first move.  It took a minute to remember that I had to save the new coordinates to the .position variable for the piece.

I started working on redrawing the square labels, but it will be a little more complex than I had imagened, so that’s a project for another day.

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*