Hot answers tagged

43

Having a "Map object that keeps track of all the cells to which it can travel" looks to me like an example of premature optimization. Instead of remembering all those cells for each piece "just in case it could become slow", why not give a piece a method with the board as parameter, which returns the related List<Cell> (or Map<Direction, ...


22

My suggestion is to make things as simple as possible. The more information you give to an object, the more complex it becomes, and the harder it is to keep track of it. Let's break your program down into layers. What is there? The pieces. They only know what kind of piece they are, and what color they are (black or white) The board. It knows only about ...


15

I wish to write a chess AI which simulates the way I think over the board, using C++. Awesome! My focus is on writing the algorithms for choosing moves (decision making), not defining the board and pieces. Er, huh? To my knowledge, most chess programs written to date are focused on taking advantage of the computer's calculating powers Well yes, though ...


13

The answer is to stop designing, since you don't have enough experience to know what to do, and start coding something. Maybe start with just pawns; add functions that can calculate the legal moves if there are only pawns on the board. Then add a king and see what that does to your code. And so on. I have a lot of experience coding, and I still mostly ...


6

The number of all possible possitions involving KBBK and a checkmate is not that great at all: 32 squares for the light-squared bishop * 32 for the dark-squared bishop * 62 for the White king (64 - 2 already taken by both bishops) * all squares where the Black king is checked by one of the bishops (it must be in check, otherwise it's not checkmate yet - or ...


6

Min-Max search (with or without ABP) is not supposed to "build the complete search tree" in memory. It is usually implemented as Depth First Search tree traversal, which traverses the complete search tree, but does not store it completely. In fact, at each point in time, there is only one node per level required in memory, so the memory usage is actually ...


5

"Of course, every solution requires easy switch to another engine" Rather than worry about which design pattern to use, write some code and move on. Does the code in the link work for you? If so, there's no code to write either: just use that. Patterns are supposed to give a commonly understood name to certain patterns of writing code. They aren't ...


5

To my knowledge, most chess programs written to date are focused on taking advantage of the computer's calculating powers (aka brute force method). My program will be different in that the focus is going to be on emulating human thinking (in this case my own way of thinking which is actually highly organized). There are quite a lot of possible chess games (...


4

This is a case of premature over-design. The pieces don't need a pointer to the board. Just pass the board to the 'generateMoves' method. You don't even need objects for the individual chessmen. All pawns are alike, so you only need one Pawn instance. Also, by the laws of chess, there is a game state that is also needed to correctly generate moves. ...


4

Without any precalculated tables, you can just build a standard chess playing engine (not specifically for endgames). Look, for example, here for a starting point. To optimize such an engine for endgames, you can try to automatically adapt your evaluation function whenever you identify a "known endgame" situation for which you have a specific evaluation ...


4

Already a good answer here, but I try to highlight the problem from a slightly different point of view. When you are going to implement a full retrograde analysis for KBBK, you have to generate all KBBK positions, which are less than 32*32*62*61*2. The "32" is the number of squares for the bishops, the "*2" takes into account which player moves next; and not ...


4

Imagine you've got a function that evaluates the game board and returns a score, where large positive scores mean you're doing well and large negative scores means you're doing badly. Now imagine that for each possible position that a piece can move to, for each piece on the board, you calculate what the score would become and choose the move that results ...


4

Sure. Your chessboard (or the unblocked cells) form a graph, where each cell is a vertex of the graph, and each allowed knight-move from one node to another defines an edge. The shortest path for the knight then can be found by a simple breadth-first search. To implement this, use a 2D integer array for representing the grid. Simply assign the value zero ...


3

Questions like this usually boil down to a trade-off between memory usage and speed. A program which uses a highly packed representation for keeping boards in memory will always require some time to unpack the data into a form where it can evaluate and apply the game's semantics. This is especially true when this "unpacking" is done implicitly, by ...


3

The other answers already responded how you could do what you asked for. I want only to point out here that neither OOP nor design patterns (yes, they are quite different things) should be used just for the sake of using them. They are tools and, as such, they should be chosen according to the purpose you want to reach, not the other way around. I can't ...


3

This is one of those situation where the human mind have an advantage over a computer. It is fairly simple to for a human to decide if a position in chess is a check mate or not. We can discard most moves as having no relevance to the position. Creating an algorithm that discards those moves is quite tricky. The simple solution of just going through all ...


2

A bishop can go to the field numbered +7 or +9, and possibly from there to the fields numbered +14 and +18. That last part you can check with a loop: how often can you do a +7, how often a +9? There are two limits to this: other pieces (which can be captured, but must be then removed) and the board edges. For the bishop, that means you can't do a +7 from ...


2

Figuring out the legal moves available for a particular piece on a chess board in a particular chess position is very situational, especially when considering pawns...1 space except when it is on its starting position, then two, and then throw in captures and en passant captures...so building an array of moves that are "mechanically" possible is a bit hairy ...


2

The "mates in 0" would be the positions that the king is in checkmate. Konrad gave a way to brute-force them through 444k positions, but we can do significantly better than that. The black king must be on the edge of the board, so that gives 28 positions. One of the bishops must be giving it check; there are only 7 spots that's possible. The other bishop ...


2

Creativity As you noticed yourself, this way of proceeding slows down your own brain, which puts itself in a comfortable passive mode. We all know that reading great novels, will not make us good writers ! Furthermore, you limit yourself to already existing designs. This will prevent you from making real breakthrough, innovate radically and differentiate ...


2

Doesn't this requirement simply fall out of whatever routine determines the best move to make at each point in the game? You aren't allowed to end your move with your king in check. That's not a valid move, so the movement routine should never pick it. So after the opponent's move, try to determine the best move you can make as normal. If there is no ...


2

When you are generating all possible moves and evaluate them for the best, you are for sure taking into account the value of pieces that might be taken. Just use the same method, and give the King a very high value; that is the usual approach. There is no magic shortcut to recognize a checkmate - remember it is defined as 'the King will be unavoidably ...


2

Best way? No clue. It depends on what you are using this encoding for. For example the best way would for a human would be column/row to column/row. For a computer? Considering that you have a 64bit board it takes exactly 6 bits to identify a location. Even then extra information such the piece being white/black bishop reduces that to 5 bits. 12bit moves, ...


2

You want to store enough information to make the move and to undo the move. So that is start position, end position, piece taken, and piece converted to at the very least. 6 bit + 6 bit + 3 bit + 2 bit. If you want to take it to 16 bit at most, store 1 bit for "conversion" set if a pawn moves to the last row, and you can leave out either the 2 bit ...


1

You're making the design significantly more complicated then it needs to be. The board handles move validation, not the pieces. This is because a piece only knows what kind of piece it is. It doesn't know where it is, or what the board looks like. What benefits do we get? The code ends up being a lot simpler. We don't need a class for each kind of piece We ...


1

This a good design or can this be improved further? For me, a design can always be improved. At least I see some problem with the movement that cannot be solved using the current design. If the Piece doesn't have any abstract method to implement, should it still remain an abstract class or should it become a concrete class? If a class does not have any ...


1

Paradigms are made for people, not for problems - problems don't care (and computers care even less). Compilation and execution strips your program of objects, functions, variables, modules, classes etc. - leaving only electrons moving on a slice of silicon. If you say things like "knowing when to stop search" and "defining goals" - and if you don't have ...


1

This sort of application is perfectly suited for functional programming. Create a few data structures (using prototypal inheritance pseudo classes) that track the state of the game: essentially the board state and the turn state. Board state tracks where all the pieces are. Turn state tracks whose turn it is. Together these two structures make up game ...


1

There are several ways to tackle rotation of a chess board. The easiest way to implement it is the same way we think about it. You create a grid of 8x8, and each position on the grid may or may not have a chess piece on top. To mirror that grid, you simply create another chess board, and place each piece in the old chess board in its mirrored location. ...


1

Let's look at the problem in a more general way. You probably want to have tables for all combinations of pieces. Also, you probably have a chess engine that can generate possible moves, execute moves, detect check and checkmate situations, evaluate positions, etc. I have, and I use the following algorithm: In 4 nested loops, generate all possible positions....


Only top voted, non community-wiki answers of a minimum length are eligible