Finding an intermediate value between two variables

Asked

Viewed 249 times

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?

imagem do algoritmo

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){

   }
}

inserir a descrição da imagem aqui

  • 1

    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 condition if

  • 2

    I don’t understand. You need to understand the value of b knowing only the difference a-c?

  • Then b would be the position of the piece that was eaten?

  • That’s right, @dvd.

  • Why don’t you represent the board with two integers for each position? Type, instead of using the position 22, would use (2, 5)

  • 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.

  • 2

    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.

  • 1

    @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.

  • @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

  • 1

    @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.

Show 5 more comments

3 answers

4

You can use the function calculaB down below:

  • This function receives the two numbers a and cand then choose the two values that are in the middle that can be b.

  • These values are obtained by averaging a and c, where one of them is the mean rounded down and the other is the mean rounded up.

  • If both values match, only one is returned. If they do not match both are returned, the smallest being first.

function calculaB(a, c) {
    var media = (c + a) / 2;
    var b1 = Math.floor(media);
    var b2 = Math.ceil(media);
    return b1 === b2 ? [b1] : [b1, b2];
}

$("#calcular").click(function() {
    var a = parseInt($("#a").val());
    var c = parseInt($("#c").val());
    var bs = calculaB(a, c);
    $("#b").val(bs);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div><label for="a">a:</label><input type="text" id="a" value="" /></div>
<div><label for="c">c:</label><input type="text" id="c" value="" /></div>
<div><button id="calcular">Calcular</button></div>
<div><label for="b">b:</label><input type="text" id="b" value="" readonly="true" /></div>

Click the blue "Run" button to test the above code.

0


The logic I understand is this::

When a - c = 7, then a and c could have the values:

a   c       b
----------------
9   2 --> a - 3 |
10  3 --> a - 3 | --> qualquer "a" dividido por 4 resulta em par (descartando as decimais)
11  4 --> a - 3 |

13  6 --> a - 4 |
14  7 --> a - 4 | --> qualquer "a" dividido por 4 resulta em ímpar (descartando as decimais)
15  8 --> a - 4 |
...
31  24 --> a - 4

For each group of 3 values of a, the value of b can be a - 3 or a - 4. Note also if I divide each value of a for 4, I will have a logical sequence of each group with result unique or par alternately 3 in 3 numbers.

Soon, b would be:

var b = ~~(a/4)%2 == 0 ? a - 3 : a - 4;

In the case of a - c = 9, the same logic applies to the value of c instead of a of the previous case:

a   c       b
----------------
10  1 --> a - 4 |
11  2 --> a - 4 | --> qualquer "c" dividido por 4 resulta em par (descartando as decimais)
12  3 --> a - 4 |

14  5 --> a - 5 |
15  6 --> a - 5 | --> qualquer "c" dividido por 4 resulta em ímpar (descartando as decimais)
16  7 --> a - 5 |
...
32 23 --> a - 5

Soon:

var b = ~~(c/4)%2 == 0 ? a - 4 : a - 5;

Example:

var a = 13,
    c = 6;

if (a - c == 7){
   
   var b = ~~(a/4)%2 == 0 ? a - 3 : a - 4;
   
   if (a - 3 == b){
      console.log("b igual a-3");
   } else
   if (a - 4 == b){
      console.log("b igual a-4");
   }
} else
if (a - c == 9){

   var b = ~~(c/4)%2 == 0 ? a - 4 : a - 5;

   if (a - 4 == b){
      console.log("b igual a-4");
   } else
   if (a - 5 == b){
      console.log("b igual a-5");
   }
}

console.log("b = "+b);

0

First, let’s imagine that the board had all 64 squares numbered instead of just the dark 32. For this we need a function that converts its numbering format to a numbering format with 64 squares. Here is this function:

function recalcula32to64(casa) {
    return casa * 2 - (Math.floor((casa - 1) / 4) % 2 === 0 ? 0 : 1);
}

And also a function that does the opposite:

function recalcula64to32(casa) {
    return casa % 2 === 0 ? casa / 2 : (casa + 1) / 2;
}

And then just calculate which is the middle house:

function calculaB(a, c) {
    return recalcula64to32((recalcula32to64(a) + recalcula32to64(c)) / 2);
}

function recalcula32to64(casa) {
    return casa * 2 - (Math.floor((casa - 1) / 4) % 2 === 0 ? 0 : 1);
}

function recalcula64to32(casa) {
    return casa % 2 === 0 ? casa / 2 : (casa + 1) / 2;
}

function calculaB(a, c) {
    return recalcula64to32((recalcula32to64(a) + recalcula32to64(c)) / 2);
}

$("#calcular").click(function() {
    var a = parseInt($("#a").val());
    var c = parseInt($("#c").val());
    var bs = calculaB(a, c);
    $("#b").val(bs);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div><label for="a">a:</label><input type="text" id="a" value="" /></div>
<div><label for="c">c:</label><input type="text" id="c" value="" /></div>
<div><button id="calcular">Calcular</button></div>
<div><label for="b">b:</label><input type="text" id="b" value="" readonly="true" /></div>

Click the blue "Run" button to test the above code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.