Java is an object oriented programming language, so let's get some objects (classes really). I've plotted down the basic outline of Mediocre Chess in the diagram to the left.
Here are the classes I will use:
Mediocre: This is basically where everything happens. At startup it will create an engine (from the engine class), and a board on which it will input the moves. It will keep track of the player's moves, and reply with the appropriate answer.
Engine: Contains the algorithm for searching every possible move and deciding which move is the best reply in the particular position. It will also contain an evaluation method that takes a position and decides who is ahead.
Board: Really the foundation of the program. Not only does it represent a position on the board, but it will also be able to generate possible moves that can be made.
Move: I've decided to make a separate class which represents a move. Basically where a piece is moving, what kind of move it is, and what piece is moving. The advantages of having every move as it's own object are:
- Easy acces to the type of move for sorting in the future. Maybe we want to calculate all captures first (this is very important in quiescence search, more on that later on), or perhaps we need to find all moves where the king is moving.
- We're able to extract the notation of the move (e.g. Ne5, e8Q etc.) without any hassle.
- Having an object for every move will be expensive in terms of memory and speed. Considering the engine will create millions of moves. As suggested in the comments a better approach would be keeping the Move-class as a static class with utility-methods, and store the actual moves as integers.
Java has a garbage collecting feature which takes care of abandoned objects, and as stated there will be millions of them.
But after some thinking I decided going for easy approach with an object for every move will be easier and produce clearer code, at the cost of some speed. The result will not be the fastest engine in the world, but fast enough.
Definitions: This is actually an interface which is used by all the above classes. It holds things like constants (if we put say KNIGHT_VALUE=300 in the Definitions interface, we only need to change one number if we want another value).
So this is the framework I'm gonna try working with. Perhaps more is needed later. Who knows. But it's a good start.
The UML Diagram was made using Gliffy, which I can highly recommend.