I've been learning programming for about half a month, although I haven't had much time to work on it, so progress has been slow. As a practice project, I'm building a chess game that runs in the terminal. So far, it can accept moves, move pieces on the board, switch turns, and handle the basic game flow.
The code is currently fairly hardcoded, and I'm sure some parts are inefficient or could be designed better. I'd appreciate constructive criticism on the project, especially suggestions that will help me learn rather than simply rewriting everything for me. I'm also currently investigating a segmentation fault in a function called remove_piece. Piece movement uses an array of piece objects to map pieces to board positions, along with a stack of piece objects intended to help manage removed pieces and memory.
1 Answer
Getting a terminal chess game to accept moves and alternate turns after only a few weeks is a solid start. The main thing I’d watch out for is hardcoding movement rules with huge conditionals. Represent each piece with coordinates and calculate possible moves using offsets. For example, a knight can use the offsets (+1,+2), (+2,+1), and their negative equivalents, then you only need to check whether the destination is still on the board.
It would also help to create a simple text file containing test moves and feed those moves into the program automatically. That way you can test changes without replaying an entire game manually. Refactor one piece type at a time instead of trying to redesign the whole board at once.
For the segmentation fault, inspect remove_piece carefully for invalid or dangling pointers, accessing an empty stack, and using an array index that is outside the board. Make sure the object is not being accessed after it has been removed or freed, and check every pointer before dereferencing it.

The movement data is currently stored in an array of piece objects, while I’m also using a stack of piece objects when pieces are removed. The remove_piece function is the part that is currently causing the segmentation fault, so I’m tracing it step by step to find where the invalid access occurs. Thanks for the suggestions.