2016-06-11 16:48:07 +02:00
|
|
|
|
###
|
|
|
|
|
# here we do stuff
|
|
|
|
|
###
|
|
|
|
|
angular.module "tkApp", []
|
2016-06-11 16:59:40 +02:00
|
|
|
|
.controller("MainCtrl", ($scope, $http)->
|
2016-06-11 16:48:07 +02:00
|
|
|
|
|
|
|
|
|
###
|
|
|
|
|
# scoped variables
|
|
|
|
|
###
|
|
|
|
|
$scope.demo = 'WOHOOO angular ça marche'
|
2016-06-15 17:58:27 +02:00
|
|
|
|
$scope.countActive = 0
|
2016-06-11 16:48:07 +02:00
|
|
|
|
|
2016-06-15 17:58:27 +02:00
|
|
|
|
$http.get('json/squares.json').then((data)->
|
2016-06-11 17:09:59 +02:00
|
|
|
|
console.log('data', data)
|
2016-06-11 16:59:40 +02:00
|
|
|
|
$scope.config = data.data
|
|
|
|
|
)
|
2016-06-11 16:48:07 +02:00
|
|
|
|
###
|
|
|
|
|
# scoped functions
|
|
|
|
|
###
|
2016-06-11 17:32:06 +02:00
|
|
|
|
$scope.reinitSquares = ->
|
|
|
|
|
for square in $scope.config.squares
|
|
|
|
|
square.active = 0
|
2016-06-11 16:48:07 +02:00
|
|
|
|
console.log('impressig!')
|
|
|
|
|
$scope.doStuffWithArg = (arg)->
|
|
|
|
|
console.log('wow such function!', arg)
|
|
|
|
|
|
2016-06-15 17:58:27 +02:00
|
|
|
|
|
|
|
|
|
###
|
|
|
|
|
# change a square's activity
|
|
|
|
|
# and run the test to know if we have won
|
|
|
|
|
###
|
|
|
|
|
$scope.toggleSquare = (s)->
|
|
|
|
|
s.active = !s.active
|
|
|
|
|
if s.active
|
|
|
|
|
$scope.countActive++
|
|
|
|
|
else
|
|
|
|
|
$scope.countActive--
|
|
|
|
|
if $scope.countActive >= $scope.config.rows
|
|
|
|
|
$scope.didWeWon()
|
|
|
|
|
|
|
|
|
|
$scope.winReason = ""
|
|
|
|
|
###
|
|
|
|
|
# run the test to know if we have won
|
|
|
|
|
###
|
|
|
|
|
$scope.didWeWon = ->
|
|
|
|
|
if !$scope.config
|
|
|
|
|
console.log('no config')
|
|
|
|
|
return false
|
|
|
|
|
$scope.winReason = ""
|
|
|
|
|
colCurrent = 1
|
|
|
|
|
rowCurrent = 1
|
|
|
|
|
columns = {}
|
|
|
|
|
rows = {}
|
|
|
|
|
squareSide = $scope.config.cols
|
|
|
|
|
for i in [ 1 .. squareSide]
|
|
|
|
|
columns[i]=0
|
|
|
|
|
rows[i]=0
|
|
|
|
|
diagonalSlash = 0 # diagonal /
|
|
|
|
|
diagonalAntiSlash = 0 # diagonal \
|
2016-06-16 12:13:46 +02:00
|
|
|
|
rowsTakenForSlash = []
|
|
|
|
|
###
|
|
|
|
|
# identify all the active squares
|
|
|
|
|
# check their position to find if a line is formed
|
|
|
|
|
###
|
2016-06-15 17:58:27 +02:00
|
|
|
|
for s in $scope.config.squares
|
|
|
|
|
s.position = colCurrent+", "+rowCurrent
|
|
|
|
|
if(s.active)
|
|
|
|
|
columns[colCurrent]++
|
|
|
|
|
rows[rowCurrent]++
|
|
|
|
|
# find if a diagonal has all its squares actives
|
|
|
|
|
if(colCurrent==rowCurrent)
|
2016-06-16 12:13:46 +02:00
|
|
|
|
diagonalAntiSlash++
|
|
|
|
|
# console.log('added slash', colCurrent, rowCurrent)
|
|
|
|
|
if(diagonalAntiSlash==squareSide)
|
|
|
|
|
$scope.winReason = "diagonale antislash \\"
|
|
|
|
|
return true
|
|
|
|
|
# find for diagonal / slash
|
|
|
|
|
console.log('rowsTakenForSlash.indexOf(rowCurrent)', rowsTakenForSlash.indexOf(rowCurrent))
|
|
|
|
|
if(colCurrent+rowCurrent== squareSide+1 && rowsTakenForSlash.indexOf(rowCurrent) is -1)
|
2016-06-15 17:58:27 +02:00
|
|
|
|
diagonalSlash++
|
2016-06-16 12:13:46 +02:00
|
|
|
|
rowsTakenForSlash.push(rowCurrent)
|
|
|
|
|
# console.log('added anti slash ', colCurrent, rowCurrent )
|
2016-06-15 17:58:27 +02:00
|
|
|
|
if(diagonalSlash==squareSide)
|
|
|
|
|
$scope.winReason = "diagonale slash /"
|
|
|
|
|
return true
|
|
|
|
|
# find if a column has all its squares actives
|
|
|
|
|
if(columns[colCurrent]==squareSide)
|
|
|
|
|
$scope.winReason = "colonne "+colCurrent
|
|
|
|
|
return true
|
|
|
|
|
# find if a row has all its squares actives
|
|
|
|
|
if(rows[rowCurrent]==squareSide)
|
|
|
|
|
|
|
|
|
|
$scope.winReason = "ligne "+rowCurrent
|
|
|
|
|
return true
|
|
|
|
|
colCurrent++
|
|
|
|
|
if(colCurrent > squareSide)
|
|
|
|
|
colCurrent=1
|
|
|
|
|
rowCurrent++
|
2016-06-16 12:13:46 +02:00
|
|
|
|
console.log('diagonalSlash', 'diagonalAntiSlash', diagonalSlash, diagonalAntiSlash)
|
2016-06-15 17:58:27 +02:00
|
|
|
|
false
|
2016-06-11 16:48:07 +02:00
|
|
|
|
###
|
|
|
|
|
# initilise everything
|
|
|
|
|
###
|
|
|
|
|
$scope.init = ->
|
|
|
|
|
console.log('mainCtrl initialised');
|
|
|
|
|
$scope.init()
|
|
|
|
|
)
|
2016-06-15 17:58:27 +02:00
|
|
|
|
$(document).ready( ->
|
2016-06-16 12:13:46 +02:00
|
|
|
|
$('.tip').tooltip()
|
2016-06-15 17:58:27 +02:00
|
|
|
|
)
|