From 48abed3bea119aaad0471dec581d26c155b96242 Mon Sep 17 00:00:00 2001 From: Richard Halsall <–richard-glyn.halsall@informatik.hs-fulda.e> Date: Mon, 30 Jan 2023 16:36:33 +0100 Subject: [PATCH] refactoring: Spielbrett Aufbau geteilt und optimiert --- schlangen_und_leitern/js/makeBoard.js | 83 +++++++++++++++++++-------- 1 file changed, 59 insertions(+), 24 deletions(-) diff --git a/schlangen_und_leitern/js/makeBoard.js b/schlangen_und_leitern/js/makeBoard.js index 799392f..da59e0a 100644 --- a/schlangen_und_leitern/js/makeBoard.js +++ b/schlangen_und_leitern/js/makeBoard.js @@ -1,37 +1,72 @@ +function buildBoard(){ + + 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) + + for(var y = 0; y < height ; y++){ + + let row = []; + + if(direction == 0){ + for(var x = 0; x < width; x++ ){ + row.push(count); + count--; + } + direction = 1; + } + + else if (direction == 1) { + for(var x = 0; x < width; x++ ){ + row.unshift(count); + count -- + } + direction = 0; + } + else { + + console.log("board Build error, direction corrupted") + } + + boardArr.push(row) + } + + console.log(boardArr) + return boardArr; + } + + + + + + + +function drawBoard(arr){ -function drawBoard(){ - - let count = 100; let tbl = document.getElementById("board"); - for (let y = 0; y < 10; y++){ - + for(let y = 0; y < arr.length; y++){ + let myRow = document.createElement("tr"); - myRow.id = "row" + (9 -y); + myRow.id = "row" + y; tbl.appendChild(myRow); - let row = document.getElementById("row" + (9 -y)); + let row = document.getElementById("row" + y ); - if(y == 0 || (y % 2 == 0) ){ - for(let x = 0; x < 10; x++){ - count--; + for(let x = 0; x < arr[y].length; x++){ + let cell = document.createElement("td"); - cell.id = "cell" + x + cell.id = "cell" + arr[y][x]; row.appendChild(cell); - cell.innerHTML = (count) - } - } - else { - for(let x = 0, z = 9; x < 10; x++, z--){ - count--; - let cell = document.createElement("td"); - cell.id = "cell" + x - row.appendChild(cell); - cell.innerHTML = ((count - z) + x) - } - } + cell.innerHTML = (arr[y][x]); + } } } - + + + +