From 592b1fa3133997335f476d7d34efdd058c6def8a Mon Sep 17 00:00:00 2001 From: Richard Halsall <–richard-glyn.halsall@informatik.hs-fulda.e> Date: Thu, 9 Feb 2023 18:51:42 +0100 Subject: [PATCH] =?UTF-8?q?die=20Positionen=20von=20Schlangen=20und=20Leit?= =?UTF-8?q?ern=20werden=20jedes=20Mal=20zuf=C3=A4llig=20ausgew=C3=A4hlt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- schlangen_und_leitern/js/boardHandling.js | 87 +++++++++++++++++++++-- 1 file changed, 82 insertions(+), 5 deletions(-) diff --git a/schlangen_und_leitern/js/boardHandling.js b/schlangen_und_leitern/js/boardHandling.js index b379d70..76d6fd1 100644 --- a/schlangen_und_leitern/js/boardHandling.js +++ b/schlangen_und_leitern/js/boardHandling.js @@ -2,6 +2,11 @@ function buildBoard(objectArr,canvasPointArr){ // build board array let boardArr = buildBoardArray() + //leiter Objekte(anfang , ende) + let leitern = [[7, 33],[24, 44],[31, 55],[60, 77]]; + //schlange Objekte (anfang , ende) + let schlangen = [[99, 56], [86, 64],[60, 28],[18,1]]; + dynamicObjects(leitern, schlangen) // draw board on screen using board array drawBoard(boardArr); //draw canvas overlay @@ -12,8 +17,8 @@ function buildBoard(objectArr,canvasPointArr){ generateBoardObjects(leitern, schlangen, objectArr) //draw board objects drawBoardObjects(canvasPointArr, objectArr) - } + function buildBoardArray(){ let boardArr = []; let width = 10; @@ -47,6 +52,82 @@ function buildBoardArray(){ } + + function dynamicObjects(ladderArr, snakeArr){ + + //get the difficulty settings + //normal , intermediate, hard + //dicide number of snakes and ladders + //decide their positions + let snakeCount = 5 ; + let ladderCount = 5; + //everage number of rows moved + let snakeLength = 1.0 + let ladderLength = 1.0 + + for (let i = 0; i < snakeCount; i++){ + //pick random number between 11 and 100 + let minStart = Math.ceil(11); + let maxStart = Math.floor(100); + let start = Math.floor(Math.random() * (maxStart - minStart + 1) + minStart) + //create an end point for this number + let minEnd = 10 + let maxEnd = 30 + + //pick a seccond number not in the same decimal as the first number + let end = start - (Math.floor(Math.random() * (maxEnd - minEnd + 1) + minEnd))*snakeLength + + //check if its below 1 + if(end <=0){ + end = 1 + } + //add these as an [] to corisponding array + snakeArr[i] = [start,end] + } + for (let i = 0; i < ladderCount; i++){ + //pick random number between 1 and 90 + let minStart = Math.ceil(1); + let maxStart = Math.floor(90); + let start = Math.floor(Math.random() * (maxStart - minStart + 1) + minStart) + //create an end point for this number + let minEnd = 10 + let maxEnd = 30 + + //pick a seccond number not in the same decimal as the first number + let end = start + (Math.floor(Math.random() * (maxEnd - minEnd + 1) + minEnd))*ladderLength + + //check if its below 1 + if(end >=100){ + end = 99 + } + //add these as an [] to corisponding array + ladderArr[i] = [start,end] + } + console.log(snakeArr) + console.log(ladderArr) + + + + /////forbid + //ladders at top + //snakes on bottom + //6 snake starts next to each other + //neither leading to same level (not in the same decimal) + //cannot have two starts in one spot + } + + + + + + + + + + + + + function drawBoard(arr){ //Tabele Element auswählen let tbl = document.getElementById("board"); @@ -76,10 +157,6 @@ function boardObject(){ } -//leiter Objekte(anfang , ende) -let leitern = [[7, 33],[24, 44],[31, 55],[60, 77]]; -//schlange Objekte (anfang , ende) -let schlangen = [[99, 56], [86, 64],[60, 28],[18,1]];