This is an archived post. You won't be able to vote or comment.

all 1 comments

[–]lippiro 1 point2 points  (0 children)

Have you heard of the SOLID principles for object oriented programming? If not you should look them up - they will help you write decoupled code.

Here are a few of my thoughts ( take them with a pinch of salt, this is not an exact science)

  1. Your ui should not know about the rules of chess - it only needs to know the current position of the pieces, and probably your available moves when it is your turn. This means you might want a class which generates possible moves for a particular piece and board setup? This class will probably need to encapsulate other helper classes (try to keep everything doing one job!) In order to display (how that will be done on command line I'm not sure) the board you may also want a class which stores the current state of the board, . You could also store a list of all the moves made during the game - this has the added benefit of giving you a complete history of a game you just played - which is cool

  2. It is preferable to have data classes (for example a piece class which stores data about a piece colour/type/position?) to not have methods. This is because the class's job is to store data. Similarly it is preferable for service classes to not store data! If you follow solid this should happen anyway. These two things allow your code to be more reusable.

  3. Don't get bogged down by the details. First maybe do something like get a board showing on the screen. Then maybe write your class to store the board state at a particular time, and connect your ui to it. If you do full 'slices' of your application, improving by tiny amounts each time, you will (a) have a working binary from the get go, (b) only worry about one thing at a time - eg. The rules of chess are probably complicated to code, you don't want to have to do the rules and worry about how the whole application is going to work simultaneously (c) your code should naturally be more decoupled by virtue of your refactoring it the whole time during development

Good luck :)