r/dailyprogrammer • u/rya11111 3 1 • Jun 22 '12
[6/22/2012] Challenge #68 [difficult]
Implement a program you can use to play the classic game of chess. White and black alternate inputting their moves using some form of chess notation. The computer checks if the moves are legal and if so, executes them. The program should be able to tell whenever a player is in check or check-mate. You can represent the chessboard in the terminal in ascii form.
Bonus: implement a simple AI that can play chess against you.
22
Upvotes
7
u/_Daimon_ 1 1 Jun 24 '12 edited Jun 24 '12
SUCCESS
I've made a solution. It took 559 lines of Python code. But it is done. It doesn't check for stalemates and pawn tranformations are always into queens, but beyond that it is complete. It is in MVC format, so expanding it later on should be fairly easy and I intend to do so. It's got 2 AIs, multiple potential setups and even a simple menu.
5
u/drb226 0 0 Jun 23 '12
This is quite a task! Hardly a 1-day exercise, this is essentially the same as BYU's CS 240 final project, but without a few of the bells and whistles.
3
u/[deleted] Jun 22 '12
Incidentally, I am in the early stages of implementing a chess engine (The AI part). I'm basing the entire engine off Bitboards rather than the more conventional methods, but it's not complete enough to show off here.
I have a bit of experience with chess engines (this is my third, and largest) so I'm up for answering any questions anyone might have.
6
u/skeeto -9 8 Jun 22 '12 edited Jun 22 '12
This is probably the most involved dailyprogrammer challenge so far.
Here's the chess engine I wrote a couple years ago: October (applet version). It's got a mulithreaded AI that will use all available CPU cores. The engine is generalized so it can automatically play any variant that I add, of which there is currently one (Gothic).
3
u/herpderpdoo Jun 26 '12 edited Jun 26 '12
Warning: extremely long
I've spent 2 days on this now. If I do any more work I'll update this comment, but for now I'll just paste my code. as it stands, I have a 240 or so line, 2 player Unicode chess game with no special moves (long jump for the pawn, king/castle switch, pawn trades for queen), which only checks for check, not checkmate (checkmate is hard, details below), and only works on Linux with Python 3 unless you have better luck changing the charset of the Windows command prompt.
I did this in Python in the terminal, and I initally had wonderfully Pythonic code that slowly devolved into more of an imperative approach as the weight of the problem began to dawn on me. The rules definitions for each piece, I thought, were pretty good, but the Game class is a little rough. It wasn't so much hard, it was just the combination of small deviations from mathematical simplicity (pawns, for instance, are party poopers) coupled with the various implementation methods i could use, all with their own set of annoying drawbacks.
implementation:
I decided early on that I would have a base Piece class, and extend this class to redefine a single function, availableMoves, much like you would in a simple 2d game with Update(). This allowed me to define the possible squares a piece could move based on where it is and the gameboard itself, and then I could easily check if a piece could move to a place by seeing if said place was in its list of available moves. I had a bit of an existential problem with having a Single Point of Truth for the position of the pieces; I could either store the position in the piece itself (nice because the piece knows where it is, not nice because you would have to query every single piece for its position for every square you wanted to check if you could move into, or more likely violate the Single Point of Truth), or I could make some kind of board and have that store the position of the pieces (nice because checking if a square is open is easy, not nice because unless you already know the position of the piece, you don't know the position of the piece, so check and checkmate become hard). I settled on using a dictionary to store the pieces, using dictionary.get(position,None) and doing bounds checking to make sure I don't check a location that isn't on the board. This would allow me to loop through all the pieces every turn to ascertain their new positions, well better than O(64) every time with an array, while still allowing for O(1) "is this space open" checks.
the structure of the program is as follows:
there are a few functions not covered in that but they're helper functions. I had fun doing this project, but others be warned (if you're still here), it's not to be taken lightly. There is hardly a right answer, or a best practice, for this project, and mine is itself far from both. also, it's going to take you a while :p
gist: https://gist.github.com/2993225
edit: whoops, forgot checkmate. So, a naive (not heuristic) approach to checkmate being true is as follows:
given two sets of pieces, checkmate for one king is comprised of two parts:
how to implement this would be as follows: 1. make an isCheck function that takes in a color and a game board (and a king position and a list of opposing pieces if you dont want to divine each of those from the board). This function finds the corresponding king, and makes sure at least one piece from the opposing side can take him next turn. Then, create a list of all possible moves for every piece. Apply them one at a time to the gameboard, and redo isCheck on this new gameboard. if isCheck is ever false return false; otherwise revert to the original gameboard and try the next move til all are exhausted.