Finding an intermediate value between two variables

2

I'm developing an algorithm for the game of 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 code below, knowing the value of a which is the current position of the part and c the future position, I need to find out the value of b , which is the position of the part food, leading in the case of a - c = 7 , then b is equal or a - 3 or a - 4 or if a - c = 9 , then b is equal or a - 4 or a - 5 . How can I find out the value of b ?

if(a-c==7){if(a-3==b){}elseif(a-4==b){}}elseif(a-c==9){if(a-4==b){}elseif(a-5==b){}}

    
asked by anonymous 01.03.2018 / 01:16

3 answers

1

The logic I noticed is as follows:

When a - c = 7 , then a and c could have 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 . Also note if I divide each value from a to 4 , I will have a logical sequence of each group with odd or even result of 3 in 3 numbers .

So,% w / w% would be:

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

In the case of b , the same logic applies to the value of a - c = 9 instead of c 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

Logo:

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);
    
01.03.2018 / 12:01
4

You can use the calculaB function below:

  • This function receives the two numbers a and c and then chooses the two values in the middle that can be b .

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

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

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><labelfor="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.

    
01.03.2018 / 02:38
1

First, let's imagine that the board had all 64 numbered houses instead of just the 32 dark ones. For this we need a function that converts its numbering format to a numbering format with the 64 houses. 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 inverse:

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

And then just figure out 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><labelfor="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.

    
01.03.2018 / 15:13