Hello, please help with the problem. There is the following task:

Write a program that generates or reads a chess position and determines whether one of the kings is under the check and is not a check. The program should provide 2 options for inputting the initial data: 1. The chess position is generated using random number sensors; 2. Chess position is entered from the computer keyboard

Say, with generation and reading, I'll figure it out. But with the definition of the mat problem. Already wrote the function checking on a step. I started writing a check on the checkmate, I have already written code that will determine whether the king can get out of the step somewhere, possibly beating the enemy. But then it came to the conclusion that there are still such situations as the removal of the attacking figure of one of his own and the king's barrier from the shah, if possible. At first, I thought simply to check if the fields of the attacking opponent's battlefield and the fields between it and the king are beating (with the same check function on the check it would not be very difficult to do everything). But there is another problem, and if the Shah is from several pieces at once? In general, I am in a stupor. I do not even know what to do. Busting all the moves of black pieces is hardly good.
Help, please, can someone faced with a similar task. Or have an idea how beautiful it is to solve it.

PS If someone is interested, he solved the problem. Without busting, in his own way, as he wanted. I believe that it is so much more effective, since with my method, at worst there will be 20 checks for the check (in my opinion ...), and if there is a brute force, very, very much. I consider the effectiveness of checks on the shah, as they are, in this case, the most time consuming. (02/10/14)

  • one
    Apparently you have to do in the forehead - mate, this is a check, from which you can not hide during the next turn. So you have to check all possible moves. - KoVadim
  • DreamChild, you wrote "First, you need to check not all the moves of each piece, but only the ability to attack a specific cell. ..." (Unfortunately, I cannot comment under your note for some reason). In my opinion, this is what I am doing: I check to see if it is possible to attack a specific cell. More precisely, those cells that are between the king and the figure attacking him, including her. - psixdev
  • one
    @ psix-dev, I join @KoVadim, it seems to me that when generating a position using random number sensors, you can get completely unimaginable from that. chess rules (and the logic of the game) position, so you can not do without a complete busting. By the way, do not forget to check that when you make your move, closing from the check, you do not open at the same time another blow to the king. - avp
  • one
    @ psix-dev is not what you do. In any case, as you described it. As I understand it, you want to check whether the cell is under attack, “scanning” all possible approaches to the cell. I mean checking whether a cell is under combat, checking the ability of each enemy piece to attack it. - DreamChild
  • one
    In case you solved a problem, you should talk about its solution in the answer and in more detail. - Qwertiy ♦

3 answers 3

In my opinion, there are no problems at all with the simultaneous check from several pieces. The fact is that if the king is under the check of several figures at once, then the only way to escape is to make a move as a king. There are 9 options to get away (4 "straight" moves, 4 moves diagonally and castling) To deflect a king or beat one of the attacking king pieces does not work, since it is impossible to shield in one direction in one direction, just as with a single stroke, beat several pieces. Therefore, to consider these options for protection makes sense only in case of a threat from the same figure.

  • Similarly, did not take into account. But then the problem is that the IsCheck function would learn from how many pieces of the check ... I think it will not be difficult. The question remains, is my method of checking for a mat normal, and does anyone know if it is more effective. - psixdev
  • four
    @DreamChild: if my memory serves me, castling from the check is forbidden. - VladD
  • one
    Dear @VladD and @DreamChild, and if my memory serves me, then taking one of the attacking pieces may mean leaving several of the pieces from the check. In addition, as a rule, it is not possible to close a single piece from the Shah if it is a horse. - BuilderC
  • one
    @BuilderC: I tried to come up with an example of this, but I could not. Will try? - VladD
  • one
    First, it is necessary to check not all the moves of each piece, but only the ability to attack a specific cell. Secondly, quite a few pieces cannot attack a specific cell even theoretically (for example, 6 pawns out of eight, or one of two bishops). Third, the pieces can be significantly smaller than they were. Fourth, many of the checks can be very short. @BuilderC sounds doubtful. I join @VladD - DreamChild February
  1. You need to check 9 cells - the king's cell and everything around. Any enemy pieces can attack the corresponding cells, make a decision - there is no check, check, checkmate or stalemate (if there are no other pieces, for example).
  2. The most labor-intensive case is when it is necessary to determine whether it is possible to defend oneself from the check by other figures or not. For each alien piece attacking the king's cage get all possible moves from the position of the figure to the king. Go through the array of these cells and determine whether any of their figures can go to these cells. If it can - check, if none can - mat. When determining the possibility of taking a strange piece, consider taking a pawn, taking a pawn on the aisle.

Well, in general, the task as formulated at 90% solves the problem of "playing chess", not a weak task, frankly.

  • Well, as I understand it, I do something like this. Besides taking a pawn on the aisle. - psixdev

I would simply do all the possible moves from this situation for 1 move:

  1. all possible white moves - will it be possible to “eat” the black king with at least one of the options? Yes - the situation is at least a chop;
  2. all possible moves of black - is there at least one position in which the king is not under attack? Yes, the check. No, mat.
  • Busting is too inefficient thing, I think. - psixdev
  • It is possible to optimize, but for a full-fledged chess program, and not in the described situation of analyzing one position. Each figure is an object in which the rules are described according to which it walks. Iterations on all the figures, on all possible moves of each => the next position on the board => the answer to the only question "is the king under attack?", Or "is the king eaten?". Max 32 pieces * max 28 turn options (for the queen in the center) <896 moves total. The smartphone will cope :)) Sergiks
  • If the word "object" was associated with OOP, then no, I write in C, so everything is a bit more complicated. And at the expense of "smartphone cope" - yes, I agree, cope. But my task is to do it as efficiently as possible ... - psixdev