Y
Y
Yuri Yanin2018-12-14 08:30:43
Probability theory
Yuri Yanin, 2018-12-14 08:30:43

How to solve the problem about the chessboard?

On a standard chessboard of eight by eight squares, all the pieces are randomly placed. What are the chances that any of the kings is under attack by an enemy rook?
UPD 1: not program code. Interested in the mathematical/calculative side. What scheme of using formulas should I follow?
This is the initial thought in my head. Calculate the number of all possible combinations of the arrangement of figures. Take away from them those combinations where paired figures of the same color interchange the positions of each other. Then take away the same thing but with pawns. We get the total number of possible combinations. Further, from this number we subtract all the probabilities at which the rook does not encounter pieces on the way. Accordingly with all kinds of combinations of other figures. After that, we take away all the probabilities, where another figure blocks the path of the rook to the king.
Something like this.
But there is a suspicion that this is too complicated a path and everything is much simpler.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
EVGENY T., 2018-12-14
@Beshere

It can be solved by simulating the situation.
1. Write a procedure that knows how to arrange shapes randomly.
2. Write a function that receives a position on the board as input, checks whether the king is under attack from the enemy rook and returns yes / no.
3. Run a loop that starts procedure 1 and checks the condition with function 2. As the number of iterations increases, the number = Yes / All will tend to a certain number, which will be the answer.

L
longclaps, 2018-12-14
@longclaps

discount, p = [1., 1.], 1. - 29. / 61.
for dist in range(1, 7):
    discount.append(discount[-1] * p)
discount += discount[-2:-8:-1]

res, cnt = 0., 0
for king in range(64):
    for rook1 in range(64):
        for rook2 in range(64):
            if king == rook1 or king == rook2 or rook1 == rook2:
                continue
            cnt += 1
            kingx, kingy = divmod(king, 8)
            rook1x, rook1y = divmod(rook1, 8)
            rook2x, rook2y = divmod(rook2, 8)
            attac1hor = 1. if kingy == rook1y else 0.
            attac1ver = 1. if kingx == rook1x else 0.
            attac2hor = 1. if kingy == rook2y else 0.
            attac2ver = 1. if kingx == rook2x else 0.
            if kingx == rook1x == rook2x:
                if kingy < rook1y < rook2y or kingy > rook1y > rook2y:
                    attac2ver = 0.
                elif kingy < rook2y < rook1y or kingy > rook2y > rook1y:
                    attac1ver = 0.
            elif kingy == rook1y == rook2y:
                if kingx < rook1x < rook2x or kingx > rook1x > rook2x:
                    attac2hor = 0.
                elif kingx < rook2x < rook1x or kingx > rook2x > rook1x:
                    attac1hor = 0.
            attac1 = attac1hor * discount[kingx - rook1x] + attac1ver * discount[kingy - rook1y]
            attac2 = attac2hor * discount[kingx - rook2x] + attac2ver * discount[kingy - rook2y]
            res += attac1 + attac2 - attac1 * attac2
print(res / cnt * 2.)

total0.3756044599826443

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question