Browse Source

refactoring: Spielbrett Aufbau geteilt und optimiert

main
Richard Halsall 2 years ago
parent
commit
48abed3bea
  1. 75
      schlangen_und_leitern/js/makeBoard.js

75
schlangen_und_leitern/js/makeBoard.js

@ -1,33 +1,65 @@
function buildBoard(){
function drawBoard(){
let boardArr = [];
let width = 10;
let height = 10;
let count = 100
let direction = 0 //0 = even row (left to right) ; 1 = odd row (right to left)
let count = 100;
let tbl = document.getElementById("board");
for(var y = 0; y < height ; y++){
for (let y = 0; y < 10; y++){
let row = [];
let myRow = document.createElement("tr");
myRow.id = "row" + (9 -y);
tbl.appendChild(myRow);
let row = document.getElementById("row" + (9 -y));
if(y == 0 || (y % 2 == 0) ){
for(let x = 0; x < 10; x++){
if(direction == 0){
for(var x = 0; x < width; x++ ){
row.push(count);
count--; count--;
let cell = document.createElement("td");
cell.id = "cell" + x
row.appendChild(cell);
cell.innerHTML = (count)
} }
direction = 1;
}
else if (direction == 1) {
for(var x = 0; x < width; x++ ){
row.unshift(count);
count --
}
direction = 0;
} }
else { else {
for(let x = 0, z = 9; x < 10; x++, z--){
count--;
console.log("board Build error, direction corrupted")
}
boardArr.push(row)
}
console.log(boardArr)
return boardArr;
}
function drawBoard(arr){
let tbl = document.getElementById("board");
for(let y = 0; y < arr.length; y++){
let myRow = document.createElement("tr");
myRow.id = "row" + y;
tbl.appendChild(myRow);
let row = document.getElementById("row" + y );
for(let x = 0; x < arr[y].length; x++){
let cell = document.createElement("td"); let cell = document.createElement("td");
cell.id = "cell" + x
cell.id = "cell" + arr[y][x];
row.appendChild(cell); row.appendChild(cell);
cell.innerHTML = ((count - z) + x)
}
cell.innerHTML = (arr[y][x]);
} }
} }
} }
@ -44,3 +76,6 @@ function drawBoard(){
Loading…
Cancel
Save