title: Calculating margin-of-victory for a game
body: <p>I'm trying to make a program that calculates scores for a sports prediction game. One of the penalties associated with the game is that for every point you miss the correct margin, one point is subtracted from your score. </p>
  
  <p>For example, you predict that Team A will defeat Team B by a score of 17-0, but the final score is 10-14. This means you lose 21 points because you predicted a margin of victory of plus 17 points for Team B, when in fact the true margin saw Team A trailing by 4 points.</p>
  
  <p>Another example, you predict that Team B will beat Team A by a score of 35-3. In this case, also using the final score from above, you only lose 28 points.</p>
  
  <p>When I test the program out, I find that it usually works when the player predicts the right team to win. I know the problem in the code deals with this margin-of-victory penalty, but <strong>I can't figure out why it gives me the right score sometimes, and other times it doesn't.</strong> </p>
  
  <pre><code>// Calculate actual margin of victory 
  margin1 = team1score - team2score;
              if (margin1 &lt; 0) {
                  margin1 = margin1 * -1;
              }
  // Calculate predicted margin of victory
  margin2 = prediction2 - prediction1;
              if (margin2 &lt; 0) {
                  margin2 = margin2 * -1;
              }
  
  // Calculate difference
  if (margin1 == margin2) {
        totalMargin = 0;
  } else {
        totalMargin = margin1 - margin2;
        if (totalMargin &lt; 0) {
             totalMargin = totalMargin * -1;
     }
  }
  </code></pre>
  
  <p>Thanks.</p>