<commit_before>
angular.module("lightsout", ["ionic", "sql", "dictionary"])

.controller("LevelCtrl", function ($scope, $rootScope, $ionicPopup, $state, $stateParams, sqlfactory, dictionary) {
  // Check for invalid state
  if ($rootScope.levelData === undefined) {
    console.log("Level loaded but level list undefined, going to main")
    $state.go("main")
  } else {
    try {
      // Redefined so that they can be used in the HTML
      $scope.levelNum = $stateParams.levelNum
      $scope.levelName = $rootScope.levelData[$scope.levelNum]["name"]
      console.log("Now in " + $scope.levelName)
    } catch (err) {
      console.log("Tried to go to " + $scope.levelNum + ", redirecting to level select.")
      $state.go("level_select")
    }
  }

  // Used by the HTML to name the buttons
$scope.buttons = []
for (var i=0; i<6; i++) {
  var row = []
  for (var j=0; j<6; j++) {
    row[j] = i+"_"+j
  }
  $scope.buttons[i] = row
}
// $scope.buttons = [
//   ["0_0", "0_1", "0_2", "0_3", "0_4", "0_5"],
//   ["1_0", "1_1", "1_2", "1_3", "1_4", "1_5"],
//   ["2_0", "2_1", "2_2", "2_3", "2_4", "2_5"],
//   ["3_0", "3_1", "3_2", "3_3", "3_4", "3_5"],
//   ["4_0", "4_1", "4_2", "4_3", "4_4", "4_5"],
//   ["5_0", "5_1", "5_2", "5_3", "5_4", "5_5"]
// ]

function convertTo(longState) {
  arr = []
  for (var i = 0; i < 6; i++) {
    row = []
    for (var j = 0; j < 6; j++) {
      row.unshift(longState%2)
      longState = Math.floor(longState/2)
    }
    arr.unshift(row)
  }
  return arr
}

function convertFrom(arr) {
  longState = 0
  for (var i = 0; i < 6; i++) {
    for (var j = 0; j < 6; j++) {
      longState *= 2
      longState += arr[i][j]
    }
  }
  return longState
}

$scope.beforeLeave = function () {
  var state = convertFrom($scope.buttonsList)
  //console.log("State before leave: " + $scope.buttonsList)
  sqlfactory.setLevelState($scope.levelNum, state)
}

$scope.initializeLevel = function () {
  if ($rootScope.levelData[$scope.levelNum]["state"] == "Unsolved" ||
      $rootScope.levelData[$scope.levelNum]["state"] == 0) {
    var defaultState = [
      18123587584, 18119623680, 480887296, 8738341376, 315360000,
       2684356643,      524288,  34873344,  807600128, 137561088,
                0,           0,         0,          0,         0,
                0,           0,         0,          0,         0
    ][$scope.levelNum]
    sqlfactory.setLevelState($scope.levelNum, defaultState)
  }
  $scope.buttonsList = convertTo($rootScope.levelData[$scope.levelNum]["state"])
  console.log("State for "+$scope.levelName+": "+$scope.buttonsList)
  for (var row=0; row<6; row++) {
    for (var col=0; col<6; col++) {
      var name = $scope.levelNum + "_" + row + "_" + col
      var button = document.getElementById(name)
      if (button === null) {
        console.log("Couldn't find button "+name)
      } else if ($scope.buttonsList[row][col] == 1) {
        button.className = "button button-energized"
      } else {
        button.className = "button button-dark"
      }
    }
  }
}

$scope.toggleButton = function (row, col) {
  var name = $scope.levelNum + "_" + row + "_" + col
  var button = document.getElementById(name)
  if (button === null) {
    return
  }
  //console.log(button.className)
  if ($scope.buttonsList[row][col] == 1) {
    button.className = "button button-dark"
    $scope.buttonsList[row][col] = 0
  } else if ($scope.buttonsList[row][col] == 0) {<commit_msg>Implement all Lights Out games<commit_after>
angular.module("lightsout", ["ionic", "sql", "dictionary"])

.controller("LevelCtrl", function ($scope, $rootScope, $ionicPopup, $state, $stateParams, sqlfactory, dictionary) {
  // Check for invalid state
  if ($rootScope.levelData === undefined) {
    console.log("Level loaded but level list undefined, going to main")
    $state.go("main")
  } else {
    try {
      // Redefined so that they can be used in the HTML
      $scope.levelNum = $stateParams.levelNum
      $scope.levelName = $rootScope.levelData[$scope.levelNum]["name"]
      console.log("Now in " + $scope.levelName)
    } catch (err) {
      console.log("Tried to go to " + $scope.levelNum + ", redirecting to level select.")
      $state.go("level_select")
    }
  }

  // Used by the HTML to name the buttons
$scope.buttons = []
for (var i=0; i<6; i++) {
  var row = []
  for (var j=0; j<6; j++) {
    row[j] = i+"_"+j
  }
  $scope.buttons[i] = row
}
// $scope.buttons = [
//   ["0_0", "0_1", "0_2", "0_3", "0_4", "0_5"],
//   ["1_0", "1_1", "1_2", "1_3", "1_4", "1_5"],
//   ["2_0", "2_1", "2_2", "2_3", "2_4", "2_5"],
//   ["3_0", "3_1", "3_2", "3_3", "3_4", "3_5"],
//   ["4_0", "4_1", "4_2", "4_3", "4_4", "4_5"],
//   ["5_0", "5_1", "5_2", "5_3", "5_4", "5_5"]
// ]

function convertTo(longState) {
  arr = []
  for (var i = 0; i < 6; i++) {
    row = []
    for (var j = 0; j < 6; j++) {
      row.unshift(longState%2)
      longState = Math.floor(longState/2)
    }
    arr.unshift(row)
  }
  return arr
}

function convertFrom(arr) {
  longState = 0
  for (var i = 0; i < 6; i++) {
    for (var j = 0; j < 6; j++) {
      longState *= 2
      longState += arr[i][j]
    }
  }
  return longState
}

$scope.beforeLeave = function () {
  var state = convertFrom($scope.buttonsList)
  //console.log("State before leave: " + $scope.buttonsList)
  sqlfactory.setLevelState($scope.levelNum, state)
}

$scope.initializeLevel = function () {
  if ($rootScope.levelData[$scope.levelNum]["state"] == "Unsolved" ||
      $rootScope.levelData[$scope.levelNum]["state"] == 0) {
    var defaultState = [
      18123587584,   18119623680,   349703572,   8738341376,   315360000,
      20186262387,   46182173418,   484093340,  32774690910, 26754334360,
      20087122770,   35738666145, 13103002380,  13099807500, 67956511935,
      20186189010,   67645734912, 32980810590,  68518150143, 68719476735
    ][$scope.levelNum]
    sqlfactory.setLevelState($scope.levelNum, defaultState)
  }
  $scope.buttonsList = convertTo($rootScope.levelData[$scope.levelNum]["state"])
  console.log("State for "+$scope.levelName+": "+$scope.buttonsList)
  for (var row=0; row<6; row++) {
    for (var col=0; col<6; col++) {
      var name = $scope.levelNum + "_" + row + "_" + col
      var button = document.getElementById(name)
      if (button === null) {
        console.log("Couldn't find button "+name)
      } else if ($scope.buttonsList[row][col] == 1) {
        button.className = "button button-energized"
      } else {
        button.className = "button button-dark"
      }
    }
  }
}

$scope.toggleButton = function (row, col) {
  var name = $scope.levelNum + "_" + row + "_" + col
  var button = document.getElementById(name)
  if (button === null) {
    return
  }
  //console.log(button.className)
  if ($scope.buttonsList[row][col] == 1) {
    button.className = "button button-dark"
    $scope.buttonsList[row][col] = 0
  } else if ($scope.buttonsList[row][col] == 0) {
{'fraction_non_alphanumeric': 0.18689425719658614, 'fraction_numerical': 0.07362939389555909, 'mean_word_length': 2.9328782707622296, 'pattern_counts': {'":': 2, '<': 19, '<?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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7113311', 'n_tokens_mistral': 2811, 'n_tokens_neox': 2587, 'n_words': 576}