<commit_before>'use strict';

function Point(i, j) {
  return { row: i, col: j };  
}

const directionToShift = {
  '': Point(0, 0),
  'stay': Point(0, 0),
  'up': Point(-1, 0),
  'down': Point(1, 0),
  'left': Point(0, -1),
  'right': Point(0, 1),
};

class Direction {
  constructor(direction) {
    this.direction = direction;
    this.shift = directionToShift[direction];
  }

  nextPoint(point) {
    return Point(point.row + this.shift.row, point.col + this.shift.col);  
  }
}

function keyToDirection(keyCode) {
  let ans = '';
  switch (keyCode) {
    case 87: ans = 'up'; break;
    case 83: ans = 'down'; break;
    case 65: ans = 'left'; break;
    case 68: ans = 'right'; break;
    case 32: ans = 'stay'; break;
  }
  
  return new Direction(ans);
}

function incrementPoints() {
  ++globalPoints;
  document.getElementById('points-output').innerHTML = globalPoints;
}

function pointsDistance(p1, p2) {
  return Math.max(Math.abs(p1.row - p2.row),
    Math.abs(p1.col - p2.col));
}

function scoreFormula() {
  //hyperbola
  return Math.floor((currentLevel.maxPoints / (currentLevel.maxPoints - globalPoints + 1)) *
    9.354 * globalPoints + (currentLevel.field.width * currentLevel.field.height / globalSteps) * globalPoints);
}

function loseCheck() {
  for (let i = 0; i < currentLevel.wolves.length; ++i) {
    let prevPig = currentLevel.pig.prevPosition();
    let curPig = currentLevel.pig.position();

    let prevWolf = currentLevel.wolves[i].prevPosition();
    let curWolf = currentLevel.wolves[i].position();

    if (pointsDistance(curPig, curWolf) == 0 ||
      pointsDistance(prevPig, curWolf) == 0 && pointsDistance(curPig, prevWolf) == 0)
    {
      $("#gameDiv").hide();
      $(".centerDiv").show();
      $("#scoreDiv").show();

      globalPoints = parseInt(document.getElementById("points-output").innerText);
      globalSteps  = parseInt(document.getElementById("steps-output").innerText);
      // alert(globalSteps);
      //hyperbola
      let score = scoreFormula();
      score += ResultScore;
      document.getElementById("userScore").innerText = score;
      if (Username !== "" && UserResultID !== "-1") {
        document.getElementById("Username").value = Username;
      }
      ResultScore = 0;
      return true;
    }
  }

  return false;
}

function winCheck() {
  if (globalPoints === Carrots) {
    $("#gameDiv").hide();
    $(".centerDiv").show();
    $("#win").show();

    globalPoints = parseInt(document.getElementById("points-output").innerText);
    globalSteps  = parseInt(document.getElementById("steps-output").innerText);
    //hyperbola
    ResultScore += scoreFormula();
  }
}

function winLoseCheck() {
  if (!loseCheck())
    winCheck();
}

function alertPoint(point) {
  // alert('(' + point.row + ', ' + point.col + ')');
}

let di = [1, -1, 0, 0, 1, 1, -1, -1];
let dj = [0, 0, -1, 1, 1, -1, 1, -1];

// return points in bfs-order
function getPointsFromRange(point, range) {
  let ans = [];<commit_msg>Fix lose and win showing bug<commit_after>'use strict';

function Point(i, j) {
  return { row: i, col: j };  
}

const directionToShift = {
  '': Point(0, 0),
  'stay': Point(0, 0),
  'up': Point(-1, 0),
  'down': Point(1, 0),
  'left': Point(0, -1),
  'right': Point(0, 1),
};

class Direction {
  constructor(direction) {
    this.direction = direction;
    this.shift = directionToShift[direction];
  }

  nextPoint(point) {
    return Point(point.row + this.shift.row, point.col + this.shift.col);  
  }
}

function keyToDirection(keyCode) {
  let ans = '';
  switch (keyCode) {
    case 87: ans = 'up'; break;
    case 83: ans = 'down'; break;
    case 65: ans = 'left'; break;
    case 68: ans = 'right'; break;
    case 32: ans = 'stay'; break;
  }
  
  return new Direction(ans);
}

function incrementPoints() {
  ++globalPoints;
  document.getElementById('points-output').innerHTML = globalPoints;
}

function pointsDistance(p1, p2) {
  return Math.max(Math.abs(p1.row - p2.row),
    Math.abs(p1.col - p2.col));
}

function scoreFormula() {
  //hyperbola
  return Math.floor((currentLevel.maxPoints / (currentLevel.maxPoints - globalPoints + 1)) *
    9.354 * globalPoints + (currentLevel.field.width * currentLevel.field.height / globalSteps) * globalPoints);
}

function loseCheck() {
  for (let i = 0; i < currentLevel.wolves.length; ++i) {
    let prevPig = currentLevel.pig.prevPosition();
    let curPig = currentLevel.pig.position();

    let prevWolf = currentLevel.wolves[i].prevPosition();
    let curWolf = currentLevel.wolves[i].position();

    if (pointsDistance(curPig, curWolf) == 0 ||
      pointsDistance(prevPig, curWolf) == 0 && pointsDistance(curPig, prevWolf) == 0)
    {
      $("#gameDiv").hide();
      $(".centerDiv").show();
      $("#win").hide();
      $("#scoreDiv").show();

      globalPoints = parseInt(document.getElementById("points-output").innerText);
      globalSteps  = parseInt(document.getElementById("steps-output").innerText);
      // alert(globalSteps);
      //hyperbola
      let score = scoreFormula();
      score += ResultScore;
      document.getElementById("userScore").innerText = score;
      if (Username !== "" && UserResultID !== "-1") {
        document.getElementById("Username").value = Username;
      }
      ResultScore = 0;
      return true;
    }
  }

  return false;
}

function winCheck() {
  if (globalPoints === Carrots) {
    $("#gameDiv").hide();
    $(".centerDiv").show();
    $("#scoreDiv").hide();
    $("#win").show();

    globalPoints = parseInt(document.getElementById("points-output").innerText);
    globalSteps  = parseInt(document.getElementById("steps-output").innerText);
    //hyperbola
    ResultScore += scoreFormula();
  }
}

function winLoseCheck() {
  if (!loseCheck())
    winCheck();
}

function alertPoint(point) {
  // alert('(' + point.row + ', ' + point.col + ')');
}

let di = [1, -1, 0, 0, 1, 1, -1, -1];
let dj = [0, 0, -1, 1, 1, -1, 1, -1];

// return points in bfs-order
function getPointsFromRange(point, range) {
  let ans = [];
{'fraction_non_alphanumeric': 0.17768183327798073, 'fraction_numerical': 0.018266356692128862, 'mean_word_length': 3.302142857142857, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1159217', 'n_tokens_mistral': 2079, 'n_tokens_neox': 2008, 'n_words': 439}