Last active
August 29, 2015 14:27
-
-
Save nickmessing/959a36c65942e3976b71 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var teren = [] | |
var rind | |
var negru = true | |
var urmatorulAlb = true | |
var focusRind = -1 | |
var focusColoana = -1 | |
for(var i = 1; i <= 8; i++){ | |
rind = [] | |
negru = i % 2 === 0 | |
for(var k = 1; k <= 8; k++) { | |
rind.push({ | |
color: negru, | |
whiteSasca: false, | |
blackSasca: false, | |
focus: false, | |
focusSasca: false | |
}) | |
negru = !negru | |
} | |
teren.push(rind) | |
} | |
var v = new Vue({ | |
el: '#dame', | |
data: { | |
teren: teren | |
}, | |
methods: { | |
click: function (rind, coloana) { | |
if (v.$data.teren[rind][coloana].focus) { | |
if (urmatorulAlb) { | |
v.$data.teren[rind][coloana].whiteSasca = true | |
v.$data.teren[focusRind][focusColoana].whiteSasca = false | |
} else { | |
v.$data.teren[rind][coloana].blackSasca = true | |
v.$data.teren[focusRind][focusColoana].blackSasca = false | |
} | |
if (Math.abs(rind - focusRind) === 2) { | |
var rindBatut = (rind + focusRind)/2 | |
var coloanaBatut = (coloana + focusColoana)/2 | |
v.$data.teren[rindBatut][coloanaBatut].blackSasca = false | |
v.$data.teren[rindBatut][coloanaBatut].whiteSasca = false | |
} | |
for (var i = 0; i <= 7; i++) { | |
for (var k = 0; k <= 7; k++) { | |
v.$data.teren[i][k].focus = false | |
v.$data.teren[i][k].focusSasca = false | |
} | |
} | |
urmatorulAlb = !urmatorulAlb | |
return | |
} | |
if ( | |
(urmatorulAlb && v.$data.teren[rind][coloana].whiteSasca) | |
|| (!urmatorulAlb && v.$data.teren[rind][coloana].blackSasca) | |
) { | |
focus(rind, coloana) | |
} | |
} | |
} | |
}) | |
function focus (rind, coloana) { | |
if (focusRind !== -1 && focusColoana !== -1) { | |
v.$data.teren[focusRind][focusColoana].focusSasca = false | |
} | |
v.$data.teren[rind][coloana].focusSasca = true | |
focusRind = rind | |
focusColoana = coloana | |
for (var i = 0; i <= 7; i++) { | |
for (var k = 0; k <= 7; k++) { | |
v.$data.teren[i][k].focus = false | |
} | |
} | |
if (urmatorulAlb) { | |
if (coloana !== 0 && !v.$data.teren[rind - 1][coloana - 1].whiteSasca && !v.$data.teren[rind - 1][coloana - 1].blackSasca) { | |
v.$data.teren[rind - 1][coloana - 1].focus = true | |
} | |
if (coloana !== 7 && !v.$data.teren[rind - 1][coloana + 1].whiteSasca && !v.$data.teren[rind - 1][coloana + 1].blackSasca) { | |
v.$data.teren[rind - 1][coloana + 1].focus = true | |
} | |
} else { | |
if (coloana !== 0 && !v.$data.teren[rind + 1][coloana - 1].whiteSasca && !v.$data.teren[rind + 1][coloana - 1].blackSasca) { | |
v.$data.teren[rind + 1][coloana - 1].focus = true | |
} | |
if (coloana !== 7 && !v.$data.teren[rind + 1][coloana + 1].whiteSasca && !v.$data.teren[rind + 1][coloana + 1].blackSasca) { | |
v.$data.teren[rind + 1][coloana + 1].focus = true | |
} | |
} | |
var sascaDeBatut = urmatorulAlb ? 'blackSasca' : 'whiteSasca' | |
if (potBate(-1, -1, rind, coloana, sascaDeBatut)) { | |
v.$data.teren[rind - 2][coloana - 2].focus = true | |
} | |
if (potBate(+1, -1, rind, coloana, sascaDeBatut)) { | |
v.$data.teren[rind + 2][coloana - 2].focus = true | |
} | |
if (potBate(-1, +1, rind, coloana, sascaDeBatut)) { | |
v.$data.teren[rind - 2][coloana + 2].focus = true | |
} | |
if (potBate(+1, +1, rind, coloana, sascaDeBatut)) { | |
v.$data.teren[rind + 2][coloana + 2].focus = true | |
} | |
} | |
function potBate (modificareRind, modificareColoana, rind, coloana, sascaDeBatut) { | |
if (modificareColoana === -1 && coloana <= 1) { | |
return false | |
} | |
if (modificareColoana === 1 && coloana >= 6) { | |
return false | |
} | |
if (modificareRind === -1 && rind <= 1) { | |
return false | |
} | |
if (modificareRind === 1 && rind >= 6) { | |
return false | |
} | |
if (!v.$data.teren[rind + modificareRind][coloana + modificareColoana][sascaDeBatut]) { | |
return false | |
} | |
if (v.$data.teren[rind + modificareRind * 2][coloana + modificareColoana * 2].whiteSasca) { | |
return false | |
} | |
if (v.$data.teren[rind + modificareRind * 2][coloana + modificareColoana * 2].blackSasca) { | |
return false | |
} | |
return true | |
} | |
function impleRindu (rindu, culoarea) { | |
var start = 1 | |
if (rindu % 2) { | |
start-- | |
} | |
v.$data.teren[rindu][start][culoarea + 'Sasca'] = true | |
v.$data.teren[rindu][start + 2][culoarea + 'Sasca'] = true | |
v.$data.teren[rindu][start + 4][culoarea + 'Sasca'] = true | |
v.$data.teren[rindu][start + 6][culoarea + 'Sasca'] = true | |
} | |
impleRindu(0, 'black') | |
impleRindu(1, 'black') | |
impleRindu(2, 'black') | |
impleRindu(5, 'white') | |
impleRindu(6, 'white') | |
impleRindu(7, 'white') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment