﻿queensEight.controller("BoardController", ['$scope',function($scope) {

  $scope.positions = [];
  $scope.rowIndicies = [0, 1, 2, 3, 4, 5, 6, 7];
  $scope.columnIndicies = [0, 1, 2, 3, 4, 5, 6, 7];
  
  $scope.toggleQueen = function (row, column) {
    if (!$scope.isInteractive) return;
    var position = { row: row, column: column };
    if ($scope.hasQueen(row, column)) {
      $scope.$apply(function () {
        $scope.positions.splice($scope.positions.indexOf(position), 1);
      });
    } else {
      $scope.$apply(function() { $scope.positions.push(position); });
    }
    console.log('handle click in controller with row: ' + row + ' column: ' + column);
    console.log('hasQueen now: ' + $scope.hasQueen(row, column));
  };

  $scope.hasQueen = function (row, column) {
    return _($scope.positions).any(function (position) {
      return position.row === row && position.column === column;
    });
  };
  
}]);



@@ -401,24 +401,179 @@
 nction () %7B%0A
+        var existingPosition = _($scope.positions).find(function(p) %7B%0A          return p.row === position.row && p.column === position.column;%0A        %7D);%0A
         $sco
@@ -617,17 +617,25 @@
 indexOf(
-p
+existingP
 osition)
@@ -691,16 +691,17 @@
 function
+ 
 () %7B $sc
@@ -744,161 +744,8 @@
   %7D%0A
-    console.log('handle click in controller with row: ' + row + ' column: ' + column);%0A    console.log('hasQueen now: ' + $scope.hasQueen(row, column));%0A
   %7D;

fix indexing issue with locating positions