2
I’m developing an algorithm for checkers and I came across a problem, when one piece eats the other, where I need to know the position of the piece that was eaten. According to the image and the code below, knowing the value of a
which is the current position of the piece and c
the future position, I need to find out the value of b
, which is the position of the food piece, taking into account the case pattern a - c = 7
, then b
is the same or a - 3
or a - 4
or if a - c = 9
, then b
is the same or a - 4
or a - 5
. How can I discover the value of b
?
if (a - c == 7){
if (a - 3 == b){
} else
if (a - 4 == b){
}
} else
if (a - c == 9){
if (a - 4 == b){
} else
if (a - 5 == b){
}
}
I suggest you keep the value of
a - c
in a variable and compare the variable, so the calculation will not be performed at each conditionif
– Costamilam
I don’t understand. You need to understand the value of
b
knowing only the differencea-c
?– Jefferson Quesado
Then b would be the position of the piece that was eaten?
– Sam
That’s right, @dvd.
– user75204
Why don’t you represent the board with two integers for each position? Type, instead of using the position
22
, would use(2, 5)
– Jefferson Quesado
I could, but I’ve developed almost all the logic using an integer number for each position. All that remains is to find out which piece was eaten, to remove it from the board.
– user75204
I suspect this is a XY problem. If you unravel a little more of the code (algorithm) it will be easier to give the appropriate help. It will certainly have far more appropriate and far less enigmatic ways of understanding which pieces were eaten.
– Isac
@Isac, it depends on... In my question I approach the problem X and inform that I am trying to solve the form Y with the pattern I noticed between the positions of the board, but nothing prevents other people try to solve otherwise and/or with another pattern. It’s not impossible, since it’s a popular board game.
– user75204
@This is called refactoring. Magic word used when removing bugs. If you don’t want to refactor, you are inserting more and more bugs, a madelbug
– Jefferson Quesado
@Jeffersonquesado, the way I’m doing maybe not the best, but it’s not wrong. I chose because it’s simple and efficient. Changing the way of representing each position now would interfere with all the logic of the algorithm and I would have to redo everything.
– user75204