changed code -> You can now move pieces, removed temp. debug code

master
ElGurkus 4 years ago
parent 1bf296a382
commit 73a2703746

@ -12,6 +12,7 @@ let bottomRight;
let bottomLeft; let bottomLeft;
let rowMin; let rowMin;
let rowMax; let rowMax;
let selectedPiece;
let somethingIsSelected = false; let somethingIsSelected = false;
for(let i = 0; i < 64; i++){ for(let i = 0; i < 64; i++){
@ -29,11 +30,6 @@ for(let i = 0; i < 8; i++){
document.getElementById("field"+fieldId).piece = 2; //Setzt die piece Variable von Feld 0 bis 8 auf 1 -> Diese Felder haben einen Spielstein von Spieler 1 document.getElementById("field"+fieldId).piece = 2; //Setzt die piece Variable von Feld 0 bis 8 auf 1 -> Diese Felder haben einen Spielstein von Spieler 1
} }
/* ---------- DEBUG, NICHT PERMANENT ---------- */
document.getElementById("field41").piece = 1;
document.getElementById("field34").piece = 2;
/* ---------- DEBUG, NICHT PERMANENT ---------- */
drawPieces(); drawPieces();
resetValidMoves(); resetValidMoves();
@ -62,20 +58,28 @@ function selectField(field){
field.style.backgroundColor = "rgb(16,43,202, 0.3)"; field.style.backgroundColor = "rgb(16,43,202, 0.3)";
field.selected = 1; field.selected = 1;
if(field.piece === 1){ if(field.piece === 1){
selectedPiece = field;
validMovesPlayer1(field); validMovesPlayer1(field);
} else if (field.piece === 2){ } else if (field.piece === 2){
selectedPiece = field;
validMovesPlayer2(field); validMovesPlayer2(field);
} }
} else if (field.selected === 1){ } else if (field.selected === 1){
field.selected = 0;
somethingIsSelected = false;
resetValidMoves();
} else if (field.validMove !== 0){
field.piece = selectedPiece.piece;
selectedPiece.piece = 0;
drawPieces();
resetValidMoves();
for(let i = 0; i < 64; i++){ for(let i = 0; i < 64; i++){
fieldId = i.toString(); fieldId = i.toString();
document.getElementById("field" + fieldId).style.backgroundColor = "rgb(255,255,255, 1)"; document.getElementById("field"+fieldId).selected = 0;
document.getElementById("field" + fieldId).validMove = 0;
}
field.selected = 0;
somethingIsSelected = false; somethingIsSelected = false;
} }
} }
}
function validMovesPlayer1(field){ function validMovesPlayer1(field){
if(field.fieldNum < 8 && field.fieldNum >= 0){ if(field.fieldNum < 8 && field.fieldNum >= 0){
rowMin = 0; rowMin = 0;
@ -136,13 +140,13 @@ function validMovesPlayer1(field){
right.validMove = 2; right.validMove = 2;
} }
} }
if((field.fieldNum - 9) >= (rowMin - 8)) { if((field.fieldNum - 9) >= (rowMin - 8) && (field.fieldNum - 9) >= 0) {
topLeft = document.getElementById("field" + (field.fieldNum - 9).toString()); topLeft = document.getElementById("field" + (field.fieldNum - 9).toString());
if (topLeft.piece === 2){ if (topLeft.piece === 2){
topLeft.validMove = 2; topLeft.validMove = 2;
} }
} }
if((field.fieldNum - 7) <= (rowMax - 8)) { if((field.fieldNum - 7) <= (rowMax - 8) && (field.fieldNum - 7) >= 0) {
topRight = document.getElementById("field" + (field.fieldNum - 7).toString()); topRight = document.getElementById("field" + (field.fieldNum - 7).toString());
if (topRight.piece === 2){ if (topRight.piece === 2){
topRight.validMove = 2; topRight.validMove = 2;
@ -217,13 +221,13 @@ function validMovesPlayer2(field){
right.validMove = 2; right.validMove = 2;
} }
} }
if((field.fieldNum + 7) >= (rowMin + 8)) { if((field.fieldNum + 7) >= (rowMin + 8) && (field.fieldNum + 7) < 64) {
bottomLeft = document.getElementById("field" + (field.fieldNum + 7).toString()); bottomLeft = document.getElementById("field" + (field.fieldNum + 7).toString());
if (bottomLeft.piece === 1){ if (bottomLeft.piece === 1){
bottomLeft.validMove = 2; bottomLeft.validMove = 2;
} }
} }
if((field.fieldNum + 9) <= (rowMax + 8)) { if((field.fieldNum + 9) <= (rowMax + 8) && (field.fieldNum + 9) < 64) {
bottomRight = document.getElementById("field" + (field.fieldNum + 9).toString()); bottomRight = document.getElementById("field" + (field.fieldNum + 9).toString());
if (bottomRight.piece === 1){ if (bottomRight.piece === 1){
bottomRight.validMove = 2; bottomRight.validMove = 2;
@ -242,5 +246,6 @@ function resetValidMoves(){
for(let i = 0; i < 64; i++){ for(let i = 0; i < 64; i++){
let fieldId = i.toString(); let fieldId = i.toString();
document.getElementById("field"+fieldId).validMove = 0; document.getElementById("field"+fieldId).validMove = 0;
document.getElementById("field" + fieldId).style.backgroundColor = "rgb(255,255,255, 1)";
} }
} }
Loading…
Cancel
Save