What would be the long form of the following statment?

54 Views Asked by At

I was looking at some code which came with the following short-hand statement

score = ((initialPlayer == player) ? CAPTURE_SCORE : -CAPTURE_SCORE) + recursive_solver(lastMap, initialPlayer, findOpponent(player), 1 + rounds);

I think that I understand the first portion of the code,

if(initialPlayer == player){
   score = CAPTURE_SCORE;
}
else
   score = -CAPTURE_SCORE;
   

but im confused to how the +recursive_solver function is added to this, any help would be greatly appreciated :)

As stated above I tried to write out the statement in a longer from thats easier for me to read. My best guess is that the recursive solver function is then added to the score of the if-else statement?

2

There are 2 best solutions below

5
273K On BEST ANSWER
if(initialPlayer == player)
   score = CAPTURE_SCORE + recursive_solver(lastMap, initialPlayer, findOpponent(player), 1 + rounds);
else
   score = -CAPTURE_SCORE + recursive_solver(lastMap, initialPlayer, findOpponent(player), 1 + rounds);

Explanation:

A = (C ? B : D) + E;

If C is true: A = (B) + E;
If C is false: A = (D) + E;
In sum

if (C)
  A = B + E;
else // if (!C)
  A = D + E;
1
sxu On
score = ((initialPlayer == player) ? CAPTURE_SCORE : -CAPTURE_SCORE) + recursive_solver(lastMap, initialPlayer, findOpponent(player), 1 + rounds);

Equals to

if(initialPlayer == player) {
    score = CAPTURE_SCORE;
} else {
    score = -CAPTURE_SCORE;
}
score += recursive_solver(lastMap, initialPlayer, findOpponent(player), 1 + rounds);