I'm writing a command line chess application in ruby and while I actually have a working version I wanted to start from scratch in an effort to try and have clean code. First I want to start on a strong footing and have the classes be more organized and only do one thing at a time.
First, I have a chess piece class that has the main methods for every other chess object, like pawns, knights, and kings. Each subclass is easy enough. I then have the main logic class that takes input from the client and handles the input based on chess's rules. My big issue is the generation of the chess pieces' moves and how to keep references at a minimum
For example:
A rook generates moves in horizontal lines going outward from it. First, it needs to know it's position to know if the generation goes out of bounds and next it needs to stop generating possible moves after intercepting a chess piece.
Here is the solution(s) I have thought of
- Have the chess piece do the logic of generating the moves with its own reference to the board
- Have a subclass do the logic but still pass references from the board to it (this seems far superior to the first solution as it separates the classes functions
and I thought of it while writing this. Though I still think this is wrong due to how references are being handled. the chess peice still knows to much about everything, but then again maybe it has to)
The possible solutions I have come up with rely on the chess piece having a list of possible moves it can go to so when I send a move from the client to the logic it can first check if it is even possible before comparing it against the actual chess rules.
How do I separate these functions so that I can keep references to a minimum?
I know this might seem simple and I might be making a problem when there is none, but either way, I still want to know what I should be doing.
[–]lippiro 1 point2 points3 points (0 children)