diff --git a/js/game.js b/js/game.js index 48e61c1..96ec91f 100644 --- a/js/game.js +++ b/js/game.js @@ -95,6 +95,7 @@ class Game { item.addEventListener("click", this.pickUpFigure); } if(item.classList.contains('black')) { + console.log('TEST'); var possibleMoves = blackPlayer.figures[item.getAttribute('data-figure')][(parseInt(item.getAttribute('id').replace('field-',''))-1)]; item.setAttribute('data-moves', possibleMoves) var bobble = document.createElement('div'); @@ -102,12 +103,11 @@ class Game { bobble.setAttribute('class', 'bobble'); bobble.appendChild(bobbleText); item.append(bobble); - item.addEventListener("click", this.pickUpFigure); + item.addEventListener("click", this.pickupFigure); } }, this) } pickUpFigure() { - console.log(this.classList) if(!this.classList.contains('farmer') && !(this.classList.contains('shogun'))) { return; } @@ -142,7 +142,7 @@ class Game { possibleTargets[5] = new Array(x-1, y-2); possibleTargets[6] = new Array(x+1, y-2); possibleTargets[7] = new Array(x+1, y+2); - possibleTargets[8] = new Array(x-2, y+2); + possibleTargets[8] = new Array(x-1, y+2); possibleTargets[9] = new Array(x-2, y+1); possibleTargets[10] = new Array(x-2, y-1); possibleTargets[11] = new Array(x+2, y-1); @@ -179,6 +179,7 @@ class Game { var allFields = document.querySelectorAll('.field'); allFields.forEach(function(item, index) { item.classList.remove('possibleMove'); + item.classList.remove('active'); }) for (var target in possibleTargets) { var greenField = document.querySelector('[data-x="'+possibleTargets[target][0]+'"][data-y="'+possibleTargets[target][1]+'"]') @@ -187,6 +188,10 @@ class Game { } } } + + releaseFigure() { + + } } class Player { diff --git a/css/styles.css b/web/css/styles.css similarity index 87% rename from css/styles.css rename to web/css/styles.css index 52fa736..d139ed5 100644 --- a/css/styles.css +++ b/web/css/styles.css @@ -30,11 +30,11 @@ background-color: rgba(255,255,255,0.7); text-align:center; } -.field.black { +.field.black.farmer { background-image: url(../img/bauer_black.png); } -.field.white { +.field.white.farmer { background-image: url(../img/bauer_weiss.png); } @@ -55,4 +55,7 @@ } #gamePanel .field.possibleMove { background-color: rgba(100,150,20, 0.8); +} +#gamePanel .field.field.possibleMove:hover { + background-color: rgba(100, 170,20, 0.8); } \ No newline at end of file diff --git a/img/bauer_black.png b/web/img/bauer_black.png similarity index 100% rename from img/bauer_black.png rename to web/img/bauer_black.png diff --git a/img/bauer_weiss.png b/web/img/bauer_weiss.png similarity index 100% rename from img/bauer_weiss.png rename to web/img/bauer_weiss.png diff --git a/img/king_black.png b/web/img/king_black.png similarity index 100% rename from img/king_black.png rename to web/img/king_black.png diff --git a/img/king_weiss.png b/web/img/king_weiss.png similarity index 100% rename from img/king_weiss.png rename to web/img/king_weiss.png diff --git a/index.html b/web/index.html similarity index 100% rename from index.html rename to web/index.html diff --git a/web/js/game.js b/web/js/game.js new file mode 100644 index 0000000..99c8329 --- /dev/null +++ b/web/js/game.js @@ -0,0 +1,737 @@ +class Figure { + constructor(x,y) { + this.x = x; + this.y = y; + } + setX(x) { + this.x = x; + } + setY(y) { + this.y = y; + } + getX() { + return parseInt(this.x); + } + getY() { + return parseInt(this.y); + } + getPosition() { + return new Array(parseInt(this.x), parseInt(this.y)); + } +} +class Game { + constructor() { + this.whitePlayer = new Player('white'); + this.blackPlayer = new Player('black'); + this.chosenFigure = new Figure(0,0); + } + drawGamePanel(x,y, widthAndHeight) { + var boxes = new Array(); + for(var i=0; i this.addBobbles(item)); + } + addBobbles(item) { + var that = this; + if(item.classList.contains('white')) { + var possibleMoves = this.whitePlayer.figures[item.getAttribute('data-figure')][(parseInt(item.getAttribute('id').replace('field-',''))-1)]; + item.setAttribute('data-moves', possibleMoves) + if(!item.childNodes[0]) { + var bobble = document.createElement('div'); + var bobbleText = document.createTextNode(possibleMoves) + bobble.setAttribute('class', 'bobble'); + bobble.appendChild(bobbleText); + item.append(bobble); + } + + } + if(item.classList.contains('black')) { + var possibleMoves = this.blackPlayer.figures[item.getAttribute('data-figure')][(parseInt(item.getAttribute('id').replace('field-',''))-1)]; + item.setAttribute('data-moves', possibleMoves) + if(!item.childNodes[0]) { + var bobble = document.createElement('div'); + var bobbleText = document.createTextNode(possibleMoves); + bobble.setAttribute('class', 'bobble'); + bobble.appendChild(bobbleText); + item.append(bobble); + } + } + } + + pickUpFigure(item) { + if(!item.classList.contains('farmer') && !(item.classList.contains('shogun'))) { + return; + } + var possibleMoves = parseInt(item.getAttribute('data-moves')); + var possibleTargets = new Object(); + var x = parseInt(item.getAttribute('data-x')); + var y = parseInt(item.getAttribute('data-y')); + + switch(possibleMoves) { + case 1: + possibleTargets[1] = new Array(x+possibleMoves, y); + possibleTargets[2] = new Array(x, y+possibleMoves); + possibleTargets[3] = new Array(x-possibleMoves, y); + possibleTargets[4] = new Array(x, y-1); + break; + case 2: + possibleTargets[1] = new Array(x+possibleMoves, y); + possibleTargets[2] = new Array(x, y+possibleMoves); + possibleTargets[3] = new Array(x-possibleMoves, y); + possibleTargets[4] = new Array(x, y-possibleMoves); + + possibleTargets[5] = new Array(x+1, y+1); + possibleTargets[6] = new Array(x-1,y-1); + possibleTargets[7] = new Array(x+1,y-1); + possibleTargets[8] = new Array(x-1, y+1); + break; + case 3: + possibleTargets[1] = new Array(x+possibleMoves, y); + possibleTargets[2] = new Array(x, y+possibleMoves); + possibleTargets[3] = new Array(x-possibleMoves, y); + possibleTargets[4] = new Array(x, y-possibleMoves); + + possibleTargets[5] = new Array(x-1, y-2); + possibleTargets[6] = new Array(x+1, y-2); + possibleTargets[7] = new Array(x+1, y+2); + possibleTargets[8] = new Array(x-1, y+2); + possibleTargets[9] = new Array(x-2, y+1); + possibleTargets[10] = new Array(x-2, y-1); + possibleTargets[11] = new Array(x+2, y-1); + possibleTargets[12] = new Array(x+2, y+1); + break; + case 4: + possibleTargets[1] = new Array(x-possibleMoves, y); + possibleTargets[2] = new Array(x+possibleMoves, y); + possibleTargets[3] = new Array(x, y+possibleMoves); + possibleTargets[4] = new Array(x, y-possibleMoves); + + possibleTargets[5] = new Array(x+1, y-3); + possibleTargets[6] = new Array(x-1, y-3); + possibleTargets[7] = new Array(x+3, y+1); + possibleTargets[8] = new Array(x+3, y-1); + possibleTargets[9] = new Array(x+1, y+3); + possibleTargets[10] = new Array(x-1, y+3); + possibleTargets[11] = new Array(x-3, y-1); + possibleTargets[12] = new Array(x-3, y+1); + + possibleTargets[13] = new Array(x+2, y-2); + possibleTargets[14] = new Array(x+2, y+2); + possibleTargets[15] = new Array(x-2, y+2); + possibleTargets[16] = new Array(x-2, y-2); + break; + } + for(var target in possibleTargets) { + if(possibleTargets[target][0] > 0 && possibleTargets[target][0] < 9 && possibleTargets[target][1] > 0 && possibleTargets[target][1] < 9) { + possibleTargets[target]["fieldsBetween"] = new Array(); + var fieldsBetween = this.calcFieldsBetween(possibleTargets[target], possibleMoves); + var field = document.querySelector('[data-x="'+possibleTargets[target][0]+'"][data-y="'+possibleTargets[target][1]+'"]'); + if(field.classList.contains('farmer') || field.classList.contains('shogun')) { + delete possibleTargets[target]; + } + } else { + delete possibleTargets[target]; + } + } + var allFields = document.querySelectorAll('.field'); + allFields.forEach(function(item, index) { + item.classList.remove('possibleMove'); + item.classList.remove('active'); + }) + var that = this; + for (var target in possibleTargets) { + var way1 = true; + var way2 = true; + var that = this; + if(possibleTargets[target]["fieldsBetween"].hasOwnProperty("way1")) { + possibleTargets[target]["fieldsBetween"]["way1"].forEach(function(item) { + var field = document.querySelector('[data-x="'+item[0]+'"][data-y="'+item[1]+'"]'); + if(!that.isFree(field)) { + way1 = false + } + }); + } + if(possibleTargets[target]["fieldsBetween"].hasOwnProperty("way2")) { + possibleTargets[target]["fieldsBetween"]["way2"].forEach(function(item) { + var field = document.querySelector('[data-x="'+item[0]+'"][data-y="'+item[1]+'"]'); + if(!that.isFree(field)) { + way2 = false; + } + }); + } else { + way2 = false; + } + console.log(way1); + console.log(way2); + if(!way2 && !way1) { + delete possibleTargets[target]; + } + } + for(var target in possibleTargets) { + var x = possibleTargets[target][0]; + var y = possibleTargets[target][1]; + document.querySelector('[data-x="'+x+'"][data-y="'+y+'"]').classList.add('possibleMove') + } + } + + isFree(field) { + if(field.classList.contains('farmer') || field.classList.contains('shogun')) { + return false; + } + return true; + } + + calcFieldsBetween(target, moves) { + var sourceX = this.chosenFigure.getX(); + var sourceY = this.chosenFigure.getY(); + //console.log('SOURCE: '+sourceX+', '+sourceY+' TARGET: '+target[0], ', '+target[1]); + switch(moves) { + case 2: + //T + if( (target[0] == sourceX) && (target[1] == (sourceY-moves) ) ) { + //console.log("T") + target["fieldsBetween"]["way1"] = new Array(); + target["fieldsBetween"]["way1"].push(new Array(sourceX, sourceY-1)); + } + //TR + else if((target[0] == (sourceX+1)) && (target[1] == (sourceY-1))) { + //console.log("TR") + target["fieldsBetween"]["way1"] = new Array(); + target["fieldsBetween"]["way2"] = new Array(); + target["fieldsBetween"]["way1"].push(new Array(sourceX, sourceY-1)); + target["fieldsBetween"]["way2"].push(new Array(sourceX+1, sourceY)); + } + //R + else if((target[0] == (sourceX+moves)) && (target[1] == sourceY)) { + //console.log("R") + target["fieldsBetween"]["way1"] = new Array(); + target["fieldsBetween"]["way1"].push(new Array(sourceX+1, sourceY)); + } + //BR + else if((target[0] == (sourceX+1)) && (target[1] == (sourceY+1))) { + //console.log("BR") + target["fieldsBetween"]["way1"] = new Array(); + target["fieldsBetween"]["way2"] = new Array(); + target["fieldsBetween"]["way1"].push(new Array(sourceX+1, sourceY)); + target["fieldsBetween"]["way2"].push(new Array(sourceX, sourceY+1)); + } + //B + else if((target[0] == sourceX) && (target[1] == (sourceY+moves)) ) { + //console.log("B") + target["fieldsBetween"]["way1"] = new Array(); + target["fieldsBetween"]["way1"].push(new Array(sourceX, sourceY+1)); + } + //BL + else if( (target[0] == (sourceX-1) ) && (target[1] == (sourceY+1) ) ) { + //console.log("BL") + target["fieldsBetween"]["way1"] = new Array(); + target["fieldsBetween"]["way2"] = new Array(); + target["fieldsBetween"]["way1"].push(new Array(sourceX-1, sourceY+1)); + target["fieldsBetween"]["way2"].push(new Array(sourceX-1, sourceY)); + } + //L + else if( (target[0] == (sourceX-moves) && (target[1] == sourceY) ) ) { + //console.log("L") + target["fieldsBetween"]["way1"] = new Array(); + target["fieldsBetween"]["way1"].push(new Array(sourceX-1, sourceY)); + } + //TL + else if( (target[0] == (sourceX-1) && (target[1] == (sourceY-1) ) ) ) { + //console.log("TL") + target["fieldsBetween"]["way1"] = new Array(); + target["fieldsBetween"]["way2"] = new Array(); + target["fieldsBetween"]["way1"].push(new Array(sourceX-1, sourceY)); + target["fieldsBetween"]["way2"].push(new Array(sourceX, sourceY-1)); + } + else { + console.log('2 moves - condition not found'); + } + break; + case 3: + //T + if( (target[0] == sourceX) && (target[1] == (sourceY-moves) ) ) { + //console.log("T") + target["fieldsBetween"]["way1"] = new Array(); + target["fieldsBetween"]["way1"].push(new Array(sourceX, sourceY-1)); + target["fieldsBetween"]["way1"].push(new Array(sourceX, sourceY-2)); + } + //TR - 2 Wege möglich + else if( (target[0] == (sourceX+1)) && (target[1] == (sourceY-2) ) ) { + //console.log("TR"); + target["fieldsBetween"]["way1"] = new Array(); + target["fieldsBetween"]["way2"] = new Array(); + target["fieldsBetween"]["way1"].push(new Array(sourceX, sourceY-1)); + target["fieldsBetween"]["way1"].push(new Array(sourceX, sourceY-2)); + target["fieldsBetween"]["way2"].push(new Array(sourceX+1, sourceY)); + target["fieldsBetween"]["way2"].push(new Array(sourceX+1, sourceY-1)); + } + // RL - 2 Wege möglich + else if( (target[0] == (sourceX+2)) && (target[1] == (sourceY-1) ) ) { + //console.log("RL"); + target["fieldsBetween"]["way1"] = new Array(); + target["fieldsBetween"]["way2"] = new Array(); + target["fieldsBetween"]["way1"].push(new Array(sourceX, sourceY-1)); + target["fieldsBetween"]["way1"].push(new Array(sourceX+1, sourceY-1)); + target["fieldsBetween"]["way2"].push(new Array(sourceX+1, sourceY)); + target["fieldsBetween"]["way2"].push(new Array(sourceX+2, sourceY)); + } + // R + else if( (target[0] == (sourceX+moves) ) && (target[1] == sourceY) ) { + //console.log("R"); + target["fieldsBetween"]["way1"] = new Array(); + target["fieldsBetween"]["way1"].push(new Array(sourceX+1, sourceY)); + target["fieldsBetween"]["way1"].push(new Array(sourceX+2, sourceY)); + } + // RR - 2 Wege möglich + else if( (target[0] == (sourceX+2) ) && (target[1] == (sourceY+1) ) ) { + //console.log("RR") + target["fieldsBetween"]["way1"] = new Array(); + target["fieldsBetween"]["way2"] = new Array(); + target["fieldsBetween"]["way1"].push(new Array(sourceX+1, sourceY)); + target["fieldsBetween"]["way1"].push(new Array(sourceX+2, sourceY)); + target["fieldsBetween"]["way2"].push(new Array(sourceX, sourceY+1)); + target["fieldsBetween"]["way2"].push(new Array(sourceX+1, sourceY+1)); + } + //BR - 2 Wege möglich + else if( (target[0] == (sourceX+1) ) && (target[1] == (sourceY +2) ) ) { + //console.log("BR"); + target["fieldsBetween"]["way1"] = new Array(); + target["fieldsBetween"]["way2"] = new Array(); + target["fieldsBetween"]["way1"].push(new Array(sourceX+1, sourceY)); + target["fieldsBetween"]["way1"].push(new Array(sourceX+1, sourceY+1)); + target["fieldsBetween"]["way2"].push(new Array(sourceX, sourceY+1)); + target["fieldsBetween"]["way2"].push(new Array(sourceX, sourceY+2)); + } + // B + else if( (target[0] == sourceX ) && (target[1] == (sourceY+moves) ) ) { + //console.log("B"); + target["fieldsBetween"]["way1"] = new Array(); + target["fieldsBetween"]["way1"].push(new Array(sourceX, sourceY+1)); + target["fieldsBetween"]["way1"].push(new Array(sourceX, sourceY+2)); + } + // BL - 2 Wege möglich + else if( (target[0] == (sourceX-1) ) && ( target[1] == (sourceY+2) ) ) { + //console.log("BL"); + target["fieldsBetween"]["way1"] = new Array(); + target["fieldsBetween"]["way2"] = new Array(); + target["fieldsBetween"]["way1"].push(new Array(sourceX-1, sourceY)); + target["fieldsBetween"]["way1"].push(new Array(sourceX-1, sourceY+1)); + target["fieldsBetween"]["way2"].push(new Array(sourceX, sourceY+1)); + target["fieldsBetween"]["way2"].push(new Array(sourceX, sourceY+2)); + } + // LL - 2 Wege möglich + else if( (target[0] == (sourceX-2) ) && (target[1] == (sourceY+1) ) ) { + //console.log("LL"); + target["fieldsBetween"]["way1"] = new Array(); + target["fieldsBetween"]["way2"] = new Array(); + target["fieldsBetween"]["way1"].push(new Array(sourceX-1, sourceY)); + target["fieldsBetween"]["way1"].push(new Array(sourceX-2, sourceY)); + target["fieldsBetween"]["way2"].push(new Array(sourceX, sourceY+1)); + target["fieldsBetween"]["way2"].push(new Array(sourceX-1, sourceY+1)); + } + // L + else if( (target[0] == (sourceX - moves) ) && (target[1] == sourceY) ) { + //console.log("L"); + target["fieldsBetween"]["way1"] = new Array(); + target["fieldsBetween"]["way1"].push(new Array(sourceX-1, sourceY)); + target["fieldsBetween"]["way1"].push(new Array(sourceX-2, sourceY)); + } + // LR - 2 Wege möglich + else if( (target[0] == ( sourceX-2) ) && (target[1] == (sourceY-1) ) ) { + //console.log("LR"); + target["fieldsBetween"]["way1"] = new Array(); + target["fieldsBetween"]["way2"] = new Array(); + target["fieldsBetween"]["way1"].push(new Array(sourceX-1, sourceY)); + target["fieldsBetween"]["way1"].push(new Array(sourceX-2, sourceY)); + target["fieldsBetween"]["way2"].push(new Array(sourceX, sourceY-1)); + target["fieldsBetween"]["way2"].push(new Array(sourceX-1, sourceY-1)); + } + // TL - 2 Wege möglich + else if ( (target[0] == (sourceX-1) ) && (target[1] == (sourceY-2) ) ) { + //console.log("TL"); + target["fieldsBetween"]["way1"] = new Array(); + target["fieldsBetween"]["way2"] = new Array(); + target["fieldsBetween"]["way1"].push(new Array(sourceX-1, sourceY)); + target["fieldsBetween"]["way1"].push(new Array(sourceX-1, sourceY-1)); + target["fieldsBetween"]["way2"].push(new Array(sourceX, sourceY-1)); + target["fieldsBetween"]["way2"].push(new Array(sourceX, sourceY-2)); + } else { + //console.log(" 3 moves - condition not met"); + } + break; + case 4: + // T + if( (target[0] == sourceX) && (target[1] == (sourceY-moves) ) ) { + //console.log("T"); + target["fieldsBetween"]["way1"] = new Array(); + target["fieldsBetween"]["way1"].push(new Array(sourceX, sourceY-1)); + target["fieldsBetween"]["way1"].push(new Array(sourceX, sourceY-2)); + target["fieldsBetween"]["way1"].push(new Array(sourceX, sourceY-3)); + } + // TR - 2 Wege möglich + else if( (target[0] == (sourceX+1) ) && (target[1] == (sourceY-3) ) ) { + //console.log("TR"); + target["fieldsBetween"]["way1"] = new Array(); + target["fieldsBetween"]["way2"] = new Array(); + target["fieldsBetween"]["way1"].push(new Array(sourceX, sourceY-1)); + target["fieldsBetween"]["way1"].push(new Array(sourceX, sourceY-2)); + target["fieldsBetween"]["way1"].push(new Array(sourceX, sourceY-3)); + + target["fieldsBetween"]["way2"].push(new Array(sourceX+1, sourceY)); + target["fieldsBetween"]["way2"].push(new Array(sourceX+1, sourceY-1)); + target["fieldsBetween"]["way2"].push(new Array(sourceX+1, sourceY-2)); + } + // TL - 2 Wege möglich + else if( (target[0] == (sourceX-1) ) && (target[1] == (sourceY-3) ) ) { + //console.log("TL"); + target["fieldsBetween"]["way1"] = new Array(); + target["fieldsBetween"]["way2"] = new Array(); + target["fieldsBetween"]["way1"].push(new Array(sourceX, sourceY-1)); + target["fieldsBetween"]["way1"].push(new Array(sourceX, sourceY-2)); + target["fieldsBetween"]["way1"].push(new Array(sourceX, sourceY-3)); + + target["fieldsBetween"]["way2"].push(new Array(sourceX-1, sourceY)); + target["fieldsBetween"]["way2"].push(new Array(sourceX-1, sourceY-1)); + target["fieldsBetween"]["way2"].push(new Array(sourceX-1, sourceY-2)); + } + // L + else if( (target[0] == (sourceX-moves) ) && (target[1] == sourceY) ) { + //console.log("L"); + target["fieldsBetween"]["way1"] = new Array(); + target["fieldsBetween"]["way1"].push(new Array(sourceX-1, sourceY)); + target["fieldsBetween"]["way1"].push(new Array(sourceX-2, sourceY)); + target["fieldsBetween"]["way1"].push(new Array(sourceX-3, sourceY)); + } + // LR - 2 Wege möglich + else if( (target[0] == (sourceX-3) ) && (target[1] == (sourceY-1) ) ) { + //console.log("LR"); + target["fieldsBetween"]["way1"] = new Array(); + target["fieldsBetween"]["way2"] = new Array(); + target["fieldsBetween"]["way1"].push(new Array(sourceX-1, sourceY)); + target["fieldsBetween"]["way1"].push(new Array(sourceX-2, sourceY)); + target["fieldsBetween"]["way1"].push(new Array(sourceX-3, sourceY)); + + target["fieldsBetween"]["way2"].push(new Array(sourceX, sourceY-1)); + target["fieldsBetween"]["way2"].push(new Array(sourceX-1, sourceY-1)); + target["fieldsBetween"]["way2"].push(new Array(sourceX-2, sourceY-1)); + } + // LL - 2 Wege möglich + else if( (target[0] == (sourceX-3) ) && (target[1] == (sourceY+1) ) ) { + //console.log("LL"); + target["fieldsBetween"]["way1"] = new Array(); + target["fieldsBetween"]["way2"] = new Array(); + target["fieldsBetween"]["way1"].push(new Array(sourceX-1, sourceY)); + target["fieldsBetween"]["way1"].push(new Array(sourceX-2, sourceY)); + target["fieldsBetween"]["way1"].push(new Array(sourceX-3, sourceY)); + + target["fieldsBetween"]["way2"].push(new Array(sourceX, sourceY+1)); + target["fieldsBetween"]["way2"].push(new Array(sourceX-1, sourceY+1)); + target["fieldsBetween"]["way2"].push(new Array(sourceX-2, sourceY+1)); + } + // B + else if( (target[0] == sourceX ) && (target[1] == (sourceY+moves) ) ) { + //console.log("B"); + target["fieldsBetween"]["way1"] = new Array(); + target["fieldsBetween"]["way1"].push(new Array(sourceX, sourceY+1)); + target["fieldsBetween"]["way1"].push(new Array(sourceX, sourceY+2)); + target["fieldsBetween"]["way1"].push(new Array(sourceX, sourceY+3)); + } + // BL - 2 Wege möglich + else if ( (target[0] == (sourceX-1) ) && (target[1] == (sourceY+3) ) ) { + //console.log("BL"); + target["fieldsBetween"]["way1"] = new Array(); + target["fieldsBetween"]["way2"] = new Array(); + target["fieldsBetween"]["way1"].push(new Array(sourceX, sourceY+1)); + target["fieldsBetween"]["way1"].push(new Array(sourceX, sourceY+2)); + target["fieldsBetween"]["way1"].push(new Array(sourceX, sourceY+3)); + + target["fieldsBetween"]["way2"].push(new Array(sourceX-1, sourceY)); + target["fieldsBetween"]["way2"].push(new Array(sourceX-1, sourceY+1)); + target["fieldsBetween"]["way2"].push(new Array(sourceX-1, sourceY+2)); + } + // BR - 2 Wege möglich + else if ( (target[0] == (sourceX+1) ) && (target[1] == (sourceY+3) ) ) { + //console.log("BR"); + target["fieldsBetween"]["way1"] = new Array(); + target["fieldsBetween"]["way2"] = new Array(); + target["fieldsBetween"]["way1"].push(new Array(sourceX, sourceY+1)); + target["fieldsBetween"]["way1"].push(new Array(sourceX, sourceY+2)); + target["fieldsBetween"]["way1"].push(new Array(sourceX, sourceY+3)); + + target["fieldsBetween"]["way2"].push(new Array(sourceX+1, sourceY)); + target["fieldsBetween"]["way2"].push(new Array(sourceX+1, sourceY+1)); + target["fieldsBetween"]["way2"].push(new Array(sourceX+1, sourceY+2)); + } + // R + else if ( (target[0] == (sourceX+moves) ) && (target[1] == sourceY) ) { + //console.log("R"); + target["fieldsBetween"]["way1"] = new Array(); + target["fieldsBetween"]["way1"].push(new Array(sourceX+1, sourceY)); + target["fieldsBetween"]["way1"].push(new Array(sourceX+2, sourceY)); + target["fieldsBetween"]["way1"].push(new Array(sourceX+3, sourceY)); + } + //RR - 2 Wege möglich + else if ( (target[0] == (sourceX+3) ) && (target[1] == (sourceY+1) ) ) { + //console.log("RR"); + target["fieldsBetween"]["way1"] = new Array(); + target["fieldsBetween"]["way2"] = new Array(); + target["fieldsBetween"]["way1"].push(new Array(sourceX+1, sourceY)); + target["fieldsBetween"]["way1"].push(new Array(sourceX+2, sourceY)); + target["fieldsBetween"]["way1"].push(new Array(sourceX+3, sourceY)); + + target["fieldsBetween"]["way2"].push(new Array(sourceX, sourceY+1)); + target["fieldsBetween"]["way2"].push(new Array(sourceX+1, sourceY+1)); + target["fieldsBetween"]["way2"].push(new Array(sourceX+2, sourceY+2)); + } + //RL + else if( (target[0] == (sourceX+3) ) && (target[1] == (sourceY-1) ) ) { + //console.log("RL"); + target["fieldsBetween"]["way1"] = new Array(); + target["fieldsBetween"]["way2"] = new Array(); + target["fieldsBetween"]["way1"].push(new Array(sourceX+1, sourceY)); + target["fieldsBetween"]["way1"].push(new Array(sourceX+2, sourceY)); + target["fieldsBetween"]["way1"].push(new Array(sourceX+3, sourceY)); + + target["fieldsBetween"]["way2"].push(new Array(sourceX, sourceY-1)); + target["fieldsBetween"]["way2"].push(new Array(sourceX+1, sourceY-1)); + target["fieldsBetween"]["way2"].push(new Array(sourceX+2, sourceY-1)); + } + //TRC + else if( (target[0] == (sourceX+2) ) && (target[1] == (sourceY-2) ) ) { + //console.log("TRC"); + target["fieldsBetween"]["way1"] = new Array(); + target["fieldsBetween"]["way2"] = new Array(); + target["fieldsBetween"]["way1"].push(new Array(sourceX, sourceY-1)); + target["fieldsBetween"]["way1"].push(new Array(sourceX, sourceY-2)); + target["fieldsBetween"]["way1"].push(new Array(sourceX+1, sourceY-2)); + + target["fieldsBetween"]["way2"].push(new Array(sourceX+1, sourceY)); + target["fieldsBetween"]["way2"].push(new Array(sourceX+2, sourceY)); + target["fieldsBetween"]["way2"].push(new Array(sourceX+2, sourceY-1)); + } + //TLC + else if( (target[0] == (sourceX-2) ) && (target[1] == (sourceY-2) ) ) { + //console.log("TLC"); + target["fieldsBetween"]["way1"] = new Array(); + target["fieldsBetween"]["way2"] = new Array(); + target["fieldsBetween"]["way1"].push(new Array(sourceX, sourceY-1)); + target["fieldsBetween"]["way1"].push(new Array(sourceX, sourceY-2)); + target["fieldsBetween"]["way1"].push(new Array(sourceX-1, sourceY-2)); + + target["fieldsBetween"]["way2"].push(new Array(sourceX-1, sourceY)); + target["fieldsBetween"]["way2"].push(new Array(sourceX-2, sourceY)); + target["fieldsBetween"]["way2"].push(new Array(sourceX-2, sourceY-1)); + } + // BRC + else if( (target[0] == (sourceX+2) ) && (target[1] == (sourceY+2) ) ) { + //console.log("BRC"); + target["fieldsBetween"]["way1"] = new Array(); + target["fieldsBetween"]["way2"] = new Array(); + target["fieldsBetween"]["way1"].push(new Array(sourceX, sourceY+1)); + target["fieldsBetween"]["way1"].push(new Array(sourceX, sourceY+2)); + target["fieldsBetween"]["way1"].push(new Array(sourceX+1, sourceY+2)); + + target["fieldsBetween"]["way2"].push(new Array(sourceX+1, sourceY)); + target["fieldsBetween"]["way2"].push(new Array(sourceX+2, sourceY)); + target["fieldsBetween"]["way2"].push(new Array(sourceX+2, sourceY+1)); + } + // BLC + else if( (target[0] == (sourceX-2) ) && (target[1] == (sourceY+2) ) ) { + //console.log("BLC"); + target["fieldsBetween"]["way1"] = new Array(); + target["fieldsBetween"]["way2"] = new Array(); + target["fieldsBetween"]["way1"].push(new Array(sourceX, sourceY+1)); + target["fieldsBetween"]["way1"].push(new Array(sourceX, sourceY+2)); + target["fieldsBetween"]["way1"].push(new Array(sourceX-1, sourceY+2)); + + target["fieldsBetween"]["way2"].push(new Array(sourceX-1, sourceY)); + target["fieldsBetween"]["way2"].push(new Array(sourceX-2, sourceY)); + target["fieldsBetween"]["way2"].push(new Array(sourceX-2, sourceY+1)); + } else { + //console.log("4 moves - condition not met"); + } + break; + default: + //console.log('not implemented yet') + } + } + + releaseFigure(targetField, fromField) { + var source = document.querySelector('[data-x="'+fromField.getX()+'"][data-y="'+fromField.getY()+'"]'); + // klassen die übergeben werden müssen, bzw. vom alten feld runter müssen + // farbe, figur + var colorClass = source.classList.contains('white') ? 'white' : 'black'; + var figureClass = source.classList.contains('farmer') ? 'farmer' : 'shogun'; + var figureData = source.getAttribute('data-figure'); + source.removeChild(source.childNodes[0]); + source.classList.remove('shogun','farmer', 'black', 'white'); + source.setAttribute('data-figure', ''); + source.setAttribute('data-moves', ''); + targetField.classList.add(figureClass, colorClass); + targetField.setAttribute('data-figure', figureData); + this.removeIndicator(); + this.showPossibleMoves(); + this.chosenFigure.setX(0); + this.chosenFigure.setY(0); + + } + + removeIndicator() { + var greenFields = document.querySelectorAll('.possibleMove'); + greenFields.forEach(item => { + item.classList.remove('possibleMove') + }) + } +} + +class Player { + constructor(color) { + this.figures = { + positions: new Array(), + b1: new Array(), + b2: new Array(), + b3: new Array(), + b4: new Array(), + b5: new Array(), + b6: new Array(), + b7: new Array(), + sh: new Array(), + } + if(color == 'white') { + this.figures.positions["b1"] = new Array(1,1); + this.figures.positions["b2"] = new Array(2,1); + this.figures.positions["b3"] = new Array(3,1); + this.figures.positions["b4"] = new Array(5,1); + this.figures.positions["b5"] = new Array(6,1); + this.figures.positions["b6"] = new Array(7,1); + this.figures.positions["b7"] = new Array(8,1); + this.figures.positions["sh"] = new Array(4,1); + } + if(color == 'black') { + this.figures.positions["b1"] = new Array(1,8); + this.figures.positions["b2"] = new Array(2,8); + this.figures.positions["b3"] = new Array(3,8); + this.figures.positions["b4"] = new Array(4,8); + this.figures.positions["b5"] = new Array(6,8); + this.figures.positions["b6"] = new Array(7,8); + this.figures.positions["b7"] = new Array(8,8); + this.figures.positions["sh"] = new Array(5,8); + } + } + calculateMoves() { + for(var figure in this.figures) { + if(figure != 'positions') { + if(figure != 'sh') { + for(var i=1; i<65; i++) { + this.figures[figure].push(Math.floor(Math.random() * (5 - 1)) + 1); + } + } else { + for(var i=1; i<65; i++) { + this.figures[figure].push(Math.floor(Math.random() * (3 - 1)) + 1); + } + } + } + } + } +} \ No newline at end of file