diff --git a/schlangen_und_leitern/HTML.html b/schlangen_und_leitern/HTML.html index 88d001f..3c8d892 100644 --- a/schlangen_und_leitern/HTML.html +++ b/schlangen_und_leitern/HTML.html @@ -6,10 +6,9 @@ - + - Document @@ -26,12 +25,20 @@ -
+
+
+
- +
+

Spieler 1 ist dran

+ + + +
diff --git a/schlangen_und_leitern/css/style.css b/schlangen_und_leitern/css/style.css index 2a3c4eb..7ac4df5 100644 --- a/schlangen_und_leitern/css/style.css +++ b/schlangen_und_leitern/css/style.css @@ -1,13 +1,21 @@ h1{ text-align: center; - font-size: 50px; + font-size: 65px; + text-decoration: underline; } .playerSelect{ display: flex; justify-content: center; + + +} + +.playerButton{ + + outline: auto; } @@ -16,7 +24,7 @@ h1{ height: 170px; width: 170px; - + background-color: rgb(255, 255, 255); display: grid; grid-template-columns: 30px 100px 30px; grid-template-rows: 30px 100px 30px; @@ -49,9 +57,9 @@ width: auto; } .th, td{ - width: 80px; - height: 80px; - outline: 2px dashed black; + width: 50px; + height: 50px; + outline: 2px solid black; } @@ -81,4 +89,49 @@ width: auto; border-radius: 50%; background-color: rgb(255, 221, 0); +} +#canvas{ +z-index: 2; +position: fixed; +} + +#leftSide{ + + width: 50%; + height: 537px; + padding-left: 10%; + padding-right: 10%; + float: left; +} +#rightSide{ + + margin-left: 50%; + height: 537px; + padding-right: 10%; +} + +#isUp{ + margin: auto; + + font-size: 50px; + display: none; + +} + +#replay { + + + + + + +} + +#continue { + + + + + + } \ No newline at end of file diff --git a/schlangen_und_leitern/js/Bridge.js b/schlangen_und_leitern/js/Bridge.js index c97f558..3af95c3 100644 --- a/schlangen_und_leitern/js/Bridge.js +++ b/schlangen_und_leitern/js/Bridge.js @@ -2,57 +2,86 @@ let turnCount = 1; function init(playerCount){ - //playerselect versctecken - document.getElementById("2Player").style.display = "none"; - document.getElementById("3Player").style.display = "none"; - document.getElementById("4Player").style.display = "none"; - - // Wufelbereich zeigen. - document.getElementById("würfelBereich").style.display = "grid"; - + //hide menue + hideStartMenu() + //arrays deklaren + var canvasPointArr= []; + let objectArr = []; // Spielbrett Aufbauen - boardArr = buildBoard(); - drawBoard(boardArr); + buildBoard(objectArr,canvasPointArr); //spielbret objekte genererien - let objectArr = []; - generateBoardObjects(leitern, schlangen, objectArr); + // spieler Strukturen aufbauen mit hilfe spieler Anzahl let playerArray = buildPlayerArray(playerCount); //Haupt Spielschleife aufrufen - document.getElementById("rollButton").addEventListener("click", function(){gameloop(boardArr,playerArray, playerCount);}); + document.getElementById("rollButton").addEventListener("click", function(){gameloop(playerArray, playerCount, objectArr);}); playerInit(playerArray); -} + + + ////////experiemnt + + canvasPointGen(canvasPointArr); + drawBoardObjects(canvasPointArr, objectArr) + -function gameloop (boardArr, playerArray, playerCount){ - console.log("player " + turnCount + " ist dran"); - //würfle function aufrufen - let roll = rollDice(); - //spieler auswählen + +} + +function gameloop (playerArray, playerCount, objectArr){ + console.log("player " + turnCount + " ist dran"); player = playerArray[turnCount-1]; - //spieler newPosition mit roll Aktualisierung - movePlayer(player, roll); - //TODO checken ob die Spieler an eine leiter oder Schlange gelandet hat - eraseOLd(playerArray,turnCount); - // setzen newPostion auf position um und neuPosition leeren - resetNewOld(player); - //TODO spieler Stelle auf dem Brett zeichnen - drawNew(playerArray,turnCount); - //TODO alter Stelle löschen + if(player.won == false){ + //würfle function aufrufen + let roll = rollDice(); + + //spieler auswählen + //spieler newPosition mit roll Aktualisierung + movePlayer(player, roll); + //TODO checken ob die Spieler an eine leiter oder Schlange gelandet hat + //TODO prüfen ob jemand gewonnen hat und wenn ja spiele beenden // weiter spielen + checkPosition(player, objectArr); - //TODO prüfen ob jemand gewonnen hat und wenn ja spiele beenden // weiter spielen + //TODO checken ob die Spieler an eine leiter oder Schlange gelandet hat + + + //alter Stelle löschen + eraseOLd(playerArray,turnCount); + // setzen newPostion auf position um und neuPosition leeren + resetNewOld(player); + //spieler Stelle auf dem Brett zeichnen + drawNew(playerArray,turnCount); + + + //TODO prüfen ob jemand gewonnen hat und wenn ja spiele beenden // weiter spielen + // bei gewinn, zwei optionen anbieten; spiel wieter oder beenden + //TODO verstekte new game taste der alles new setzt + //TODO falls spiel weiter, taste verstecken und gewohnen spieler ignorieren (schware) + + - // Reihezähler inkrementieren bzw. zurücksetzen - if(turnCount >= playerCount){ - turnCount = 1; + // Reihezähler inkrementieren bzw. zurücksetzen + if(turnCount >= playerCount){ + turnCount = 1; + } + else { + turnCount ++ + } + //show who is up + turnIndicater(turnCount); } - else { - turnCount ++ + else{ + if(turnCount >= playerCount){ + turnCount = 1; + } + else { + turnCount ++ + } } } \ No newline at end of file diff --git a/schlangen_und_leitern/js/boardHandling.js b/schlangen_und_leitern/js/boardHandling.js new file mode 100644 index 0000000..137473e --- /dev/null +++ b/schlangen_und_leitern/js/boardHandling.js @@ -0,0 +1,377 @@ +//ein Array erstellen, aus dem die Spilebrett gezeichnet werden kann +function buildBoard(objectArr,canvasPointArr){ + // build board array + let boardArr = buildBoardArray() + //leiter Objekte(anfang , ende) + let leitern = []; + //schlange Objekte (anfang , ende) + let schlangen = []; + dynamicObjects(leitern, schlangen) + // draw board on screen using board array + drawBoard(boardArr); + //draw canvas overlay + drawCanavas() + // cast points to center of board cells + canvasPointGen(canvasPointArr); + // add snakes and ladders objects to object array + generateBoardObjects(leitern, schlangen, objectArr) + //draw board objects + drawBoardObjects(canvasPointArr, objectArr) +} + +function buildBoardArray(){ + let boardArr = []; + let width = 10; + let height = 10; + let count = 100 + let direction = 0 //0 = gerade reihe (links nach Rechts) ; 1 = ungerade reihe (rechts nach Links) + // construct board array + 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) + } + return boardArr; + } + + + + 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.push([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.push([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"); + //neue Reihen mit schleife einfugen + 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 ); + // neue zellen für jede Reihe anknopfen + for(let x = 0; x < arr[y].length; x++){ + + let cell = document.createElement("td"); + cell.id = "cell" + arr[y][x]; + row.appendChild(cell); + cell.innerHTML = (arr[y][x]); + } + } +} + +//spielbrett objekt Struktur +function boardObject(){ + + this.type; + this.start; + this.end; +} + + + + + +//fullen des Objekts array +function generateBoardObjects (leitern, schlangen, objectArray){ + + + for(let i = 0; i < leitern.length; i++){ + let leiter = new boardObject; + leiter.type = "l"; + leiter.start = leitern[i][0]; + leiter.end = leitern[i][1]; + objectArray.push(leiter); + } + + for(let i = 0; i < schlangen.length; i++){ + let schlange = new boardObject; + schlange.type = "s"; + schlange.start = schlangen[i][0]; + schlange.end = schlangen[i][1]; + objectArray.push(schlange); + } +} + + + +//make a canvas the same size as the playbaord +function drawCanavas(){ + var ctx = document.getElementById("canvas"); + var board = document.getElementById("board"); + //get the tables measurements + tableMes = board.getBoundingClientRect() + tableWidth = tableMes.width; + tableHight = tableMes.height; + console.log(tableHight, tableWidth) + //set canavas to table size + ctx.height = tableHight + ctx.width = tableHight +} + +//canvas point objects +function canvasPoint () { + this.cell + this.x + this.y +} + +function canvasPointGen(canvasPointArr){ + + //get canvas size + var canvas = document.getElementById("canvas"); + //get induvidual cell sizes + canvasWidth = canvas.width + cellSize = canvas.width/10 + + halfCell = cellSize/2 + //starting position for both axises + yTracker = halfCell + //tracker for cell count + let count = 100 + + //flip to account for direction change + let direction = true // 0 is left to right, 1 is right to left + + //for each cell create a point at its center + for(let y = 0; y < 10; y++){ + + if (direction == true){ + var xTracker = halfCell + + for(let x = 0; x < 10; x++ ){ + //create new cell point obj + let newPoint = new canvasPoint() + //set new objects variables + newPoint.cell = count + newPoint.x = xTracker + newPoint.y = yTracker + console.log("new point"+ newPoint.cell, newPoint.x, newPoint.y) + canvasPointArr.push(newPoint) + //advance x and count + xTracker += cellSize + //toggle direction + count-- + } + } + else if (direction == false){ + var xTracker = (canvasWidth - halfCell) + for(let x = 10; x > 0; x--){ + //create new cell point obj + let newPoint = new canvasPoint() + //set new objects variables + newPoint.cell = count + newPoint.x = xTracker + newPoint.y = yTracker + console.log("new point"+ newPoint.cell, newPoint.x, newPoint.y) + canvasPointArr.push(newPoint) + //advance x and count + xTracker -= cellSize + //toggle direction + count-- + } + } + + yTracker += cellSize + if (direction == true){ + direction = false + } + else if (direction == false){ + direction = true + } + } + + +} + + + +//function that draws a line using x,y cooridinates of two points +function drawLine(type, startPosX, startPosY, endPosX, endPosY){ + + var c = document.getElementById("canvas"); + var ctx = c.getContext("2d"); + ctx.beginPath(); + ctx.moveTo(startPosX, startPosY); + ctx.lineTo(endPosX, endPosY); + ctx.stroke(); + ctx.lineWidth = 10; + switch (type) { + case "l": + ctx.strokeStyle = '#0FD71D'; + break; + + case "s": + ctx.strokeStyle = '#E21317'; + break; + default: + } + ctx.strokeStyle; + +} + +function drawBoardObjects(canvasPointArr, objectArray){ + //loop through all object in the object array + for(let i = 0; i < objectArray.length; i++){ + + let startCell = objectArray[i].start + let endCell = objectArray[i].end + + let startPointX; + let startPointY; + let endPointX; + let endPointY; + + //find cell points on canvas + for(let j = 0; j < canvasPointArr.length; j++){ + + + if(canvasPointArr[j].cell == startCell){ + startPointX = canvasPointArr[j].x; + startPointY =canvasPointArr[j].y + console.log("start found") + } + + else if(canvasPointArr[j].cell == endCell){ + endPointX = canvasPointArr[j].x; + endPointY =canvasPointArr[j].y + + console.log(endPointX, endPointX) + } + + + + + drawLine(objectArray[i].type,startPointX, startPointY, endPointX, endPointY) + } + } +} + + + +function continueGame (){ + + changeElement("continue", "none") + changeElement("replay", "none") + changeElement("würfelBereich", "grid") + + } + +function hideStartMenu(){ + //playerselect versctecken + changeElement("2Player", "none") + changeElement("3Player", "none") + changeElement("4Player", "none") + changeElement("isUp", "block") + + // Wufelbereich zeigen. + changeElement("würfelBereich", "grid") +} + + + + + + function changeElement(id, style){ + + document.getElementById(id).style.display = style; + } + + + + + + module.exports = { + buildBoardArray, + generateBoardObjects, + boardObject, + dynamicObjects +} \ No newline at end of file diff --git a/schlangen_und_leitern/js/diceHandling.js b/schlangen_und_leitern/js/diceHandling.js index 047be71..6ce9cc9 100644 --- a/schlangen_und_leitern/js/diceHandling.js +++ b/schlangen_und_leitern/js/diceHandling.js @@ -10,21 +10,28 @@ let faces = ["img/wurfle/f(1).png", //dice function rollDice() { - //Max und Min Zahl geben - max = 6; - min = 1 - // min und max als .Math variable umwandeln - min = Math.ceil(min); - max = Math.floor(max); - //ein zufällige zahl zwischen min und max generieren - roll = Math.floor(Math.random() * (max - min + 1) + min); - - + let roll = generateNumber() // Wurfelbild ändern document.querySelector("#face").src= faces[roll-1]; console.log(roll); - - - + return roll; } + +function generateNumber(){ + let num; + //Max und Min Zahl geben + max = 6; + min = 1 + // min und max als .Math variable umwandeln + min = Math.ceil(min); + max = Math.floor(max); + //ein zufällige zahl zwischen min und max generieren + num = Math.floor(Math.random() * (max - min + 1) + min); + return num; + + } + + + module.exports = generateNumber; + diff --git a/schlangen_und_leitern/js/makeBoard.js b/schlangen_und_leitern/js/makeBoard.js deleted file mode 100644 index e871dbf..0000000 --- a/schlangen_und_leitern/js/makeBoard.js +++ /dev/null @@ -1,141 +0,0 @@ -//ein Array erstellen, aus dem die Spilebrett gezeichnet werden kann -function buildBoard(){ - - let boardArr = []; - let width = 10; - let height = 10; - let count = 100 - let direction = 0 //0 = gerade reihe (links nach Rechts) ; 1 = ungerade reihe (rechts nach Links) - - 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){ - //Tabele Element auswählen - let tbl = document.getElementById("board"); - //neue Reihen mit schleife einfugen - 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 ); - // neue zellen für jede Reihe anknopfen - for(let x = 0; x < arr[y].length; x++){ - - let cell = document.createElement("td"); - cell.id = "cell" + arr[y][x]; - row.appendChild(cell); - cell.innerHTML = (arr[y][x]); - } - } -} - -//spielbrett objekt Struktur -function boardObject(){ - - this.type; - this.start; - this.end; -} - - -//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]]; - - - -//fullen des Objekts array -function generateBoardObjects (leitern, schlangen, objectArray){ - - - for(let i = 0; i < leitern.length; i++){ - let leiter = new boardObject; - leiter.type = "l"; - leiter.start = leitern[i][0]; - leiter.end = leitern[i][1]; - objectArray.push(leiter); - } - - for(let i = 0; i < schlangen.length; i++){ - let schlange = new boardObject; - schlange.type = "s"; - schlange.start = schlangen[i][0]; - schlange.end = schlangen[i][1]; - objectArray.push(schlange); - } -} -//function that draws a line using x,y cooridinates of two points -function drawLine(type, count, ax,ay, bx, by){ - if(ay>by) - { - bx=ax+bx; - ax=bx-ax; - bx=bx-ax; - by=ay+by; - ay=by-ay; - by=by-ay; - } - var calc=Math.atan((ay-by)/(bx-ax)); - calc=calc*180/Math.PI; - var length=Math.sqrt((ax-bx)*(ax-bx)+(ay-by)*(ay-by)); - console.log(length) - - - let newLine = document.createElement("div") - newLine.id = type + count; - - let style = "height:" + length + "px;width:5px;background-color:black;position:absolute;top:" + (ay) + "px;left:" + (ax) + "px;transform:rotate(" + calc + "deg);-ms-transform:rotate(" + calc + "deg);transform-origin:0% 0%;-moz-transform:rotate(" + calc + "deg);-moz-transform-origin:0% 0%;-webkit-transform:rotate(" + calc + "deg);-webkit-transform-origin:0% 0%;-o-transform:rotate(" + calc + "deg);-o-transform-origin:0% 0%;" - - newLine.style.cssText = style; - document.getElementById("board").appendChild(newLine); - } - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/schlangen_und_leitern/js/playerHandling.js b/schlangen_und_leitern/js/playerHandling.js index 947a317..8906dec 100644 --- a/schlangen_und_leitern/js/playerHandling.js +++ b/schlangen_und_leitern/js/playerHandling.js @@ -4,7 +4,8 @@ function PlayerBuilder(){ this.position = 1; - this.newPosition; + this.newPosition = 1; + this.won = false } @@ -18,7 +19,7 @@ function buildPlayerArray(playerCount, boardArr){ for(let i = 1; i <= playerCount; i++){ let player = new PlayerBuilder(i); playerArray.push(player) - console.log("player added to array!") + } return playerArray; @@ -32,50 +33,32 @@ function movePlayer(player, roll){ player.newPosition = player.position + roll; if(player.newPosition >= 100){ player.newPosition = 100; - //TODO gewinn aufrufen. } - //TODO Schlange oder Leiter fehlschalg. + + } + + function initialise(count, playerArray){ + //player id ie. count 0 = + var id = count +1; + var Token = document.createElement("div") + let position = playerArray[count].position + var boardPostition = boardLocation(position); + Token.classList.add("p"+ id); + Token.id = "p" + id; + document.getElementById(boardPostition).appendChild(Token) + } + //Draw Spieler bei Spiele init function playerInit(playerArray){ for(let i = 0; i < playerArray.length; i++){ - var Token = document.createElement("div") - if(i == 0){ - let position = playerArray[i].position - var boardPostition = boardLocation(position); - Token.classList.add("p1"); - Token.id = "p1"; - document.getElementById(boardPostition).appendChild(Token) - } - else if(i == 1){ - let position = playerArray[i].position - var boardPostition = boardLocation(position); - Token.classList.add("p2"); - Token.id = "p2"; - document.getElementById(boardPostition).appendChild(Token) - } - else if(i == 2){ - let position = playerArray[i].position - var boardPostition = boardLocation(position); - Token.classList.add("p3"); - Token.id = "p3"; - document.getElementById(boardPostition).appendChild(Token) - } - else if(i == 3){ - let position = playerArray[i].position - var boardPostition = boardLocation(position); - Token.classList.add("p4"); - Token.id = "p4"; - document.getElementById(boardPostition).appendChild(Token) - } - - + initialise(i,playerArray) } - } +} - //Stele Zeichnen +//Stele Zeichnen function drawNew(playerArray, turnCount){ // wo muss es hin var player = playerArray[turnCount-1]; @@ -131,7 +114,7 @@ function movePlayer(player, roll){ } } - //TODO zeichen für jede Spieler + @@ -166,4 +149,76 @@ function movePlayer(player, roll){ console.log("Error: Zellenummer " + tableId + " nicht gefunden"); //Druckt Errror auf dem console wenn unerfolgereich } } - + + + + +function turnIndicater(turnCount){ + + let isUp =document.getElementById("isUp") + + switch (turnCount) { + case 1: + isUp.innerHTML = "Spieler 1 ist dran" + break; + + case 2: + isUp.innerHTML = "Spieler 2 ist dran" + break; + case 3: + isUp.innerHTML = "Spieler 3 ist dran" + break; + + case 4: + isUp.innerHTML = "Spieler 4 ist dran" + break; + + default: + break; + } +} + + + +function checkPosition(player, objectArr){ + + //above 100 + over100(player) + //win condition + if(player.newPosition == 100){ + player.won = true; + changeElement("continue", "block") + changeElement("replay", "block") + changeElement("würfelBereich", "none") + } + //snake condition + //ladder condition + landedOn(player,objectArr) + +} + +function over100(player){ + + if(player.newPosition > 100){ + player.newPosition = 100; + } +} +function landedOn (player, objectArr){ + + for(let i = 0; i < (objectArr.length) ; i++){ + if(player.newPosition == objectArr[i].start){ + player.newPosition = objectArr[i].end; + } + } +} + + + +module.exports = { + PlayerBuilder, + buildPlayerArray, + movePlayer, + over100, + landedOn, + resetNewOld +} \ No newline at end of file diff --git a/schlangen_und_leitern/tests/boardHandling.test.js b/schlangen_und_leitern/tests/boardHandling.test.js new file mode 100644 index 0000000..498be54 --- /dev/null +++ b/schlangen_und_leitern/tests/boardHandling.test.js @@ -0,0 +1,114 @@ +//Importieren der FUnktionen +const { + buildBoardArray, + generateBoardObjects, + boardObject, + dynamicObjects + } = require('../js/boardHandling'); + + +let boardEx =[ + [100, 99, 98, 97, 96, 95, 94, 93, 92, 91], + [81, 82, 83, 84, 85, 86, 87, 88, 89, 90], + [80, 79, 78, 77, 76, 75, 74, 73, 72, 71], + [61, 62, 63, 64, 65, 66, 67, 68, 69, 70], + [60, 59, 58, 57, 56, 55, 54, 53, 52, 51], + [41, 42, 43, 44, 45, 46, 47, 48, 49, 50], + [40, 39, 38, 37, 36, 35, 34, 33, 32, 31], + [21, 22, 23, 24, 25, 26, 27, 28, 29, 30], + [20, 19, 18, 17, 16, 15, 14, 13, 12, 11], + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + ]; + +//leiter Objekte +let leitern = [[7, 33],[24, 44],[31, 55],[60, 77]]; +//schlange Objekte +let schlangen = [[99, 56], [86, 64],[60, 28],[18,1]]; + + + +describe('tests ob das boardArray rigtig aufgebaut würde', () => { + + + it('past zum beispiel array', ()=>{ + + expect(buildBoardArray()).toEqual(boardEx); + + }); + +}) + +describe('testen ob alle board objekte korrekt initialisiert wird', () => { + + + it('testen, ob die Anzahl der Elemente im Array korrekt ist ', ()=>{ + let objectArr = [] ; + generateBoardObjects(leitern, schlangen, objectArr); + expect(objectArr.length).toBe((leitern.length + schlangen.length)); + }); + + + it('prüfen, ob jedem Objekt der richtige Typ zugeordnet ist', ()=>{ + let obArr = [] ; + generateBoardObjects(leitern, schlangen, obArr); + + for(let i = 0; i < obArr.length; i++){ + //prüfen leitern + if(obArr[i].start < obArr[i].end) + expect(obArr[i].type).toBe("l"); + //prüfen schlange + else { + expect(obArr[i].type).toBe("s"); + } + } + }); + +}) + + + + +describe('prüfen, ob dynamische Objekte korrekt erstellt wurden', () => { + + + it('Testen, ob genügend Objekte gemacht wurden', ()=>{ + + let leitern = []; + let schlangen = []; + dynamicObjects(leitern,schlangen) + expect(leitern.length).toBe(5); + expect(schlangen.length).toBe(5); + + }); + + it('Prüfun, ob alle Schlangenobjekte niedriger enden als sie beginnenn', ()=>{ + + + let leitern = []; + let schlangen = []; + dynamicObjects(leitern, schlangen) + for(let i = 0; i < schlangen.length; i++){ + + expect(schlangen[i][1]).toBeLessThan(schlangen[i][0]); + } + + + }); + + it('Prüfen, ob alle Leiterobjekte höher enden als sie beginnen', ()=>{ + + + let leitern = []; + let schlangen = []; + dynamicObjects(leitern, schlangen) + for(let i = 0; i < leitern.length; i++){ + + expect(leitern[i][0]).toBeLessThan(leitern[i][1]); + } + + + }); + +}) + + diff --git a/schlangen_und_leitern/tests/diceHandling.test.js b/schlangen_und_leitern/tests/diceHandling.test.js new file mode 100644 index 0000000..72f79ee --- /dev/null +++ b/schlangen_und_leitern/tests/diceHandling.test.js @@ -0,0 +1,14 @@ +const generateNumber = require('../js/diceHandling'); + +describe('würfel testen', ()=>{ + + it('testen ob es von 100 Versuche immer eine zahl zwischen 1 und 6 zurück gibt',()=>{ + for(let i = 0; i <= 100; i++) + { + expect(generateNumber()).toBeGreaterThanOrEqual(1); + expect(generateNumber()).toBeLessThan(7); + } + }) + + +}) diff --git a/schlangen_und_leitern/tests/playerHandling.test.js b/schlangen_und_leitern/tests/playerHandling.test.js new file mode 100644 index 0000000..2889182 --- /dev/null +++ b/schlangen_und_leitern/tests/playerHandling.test.js @@ -0,0 +1,129 @@ +//Importieren der FUnktionen +const { +PlayerBuilder, +buildPlayerArray, +movePlayer, +over100, +landedOn, +resetNewOld +} = require('../js/playerHandling'); + +let player = {"position": 1, "newPosition": 1, "won": false}; +//example player array +let playerArr2 = [ + {"position": 1, "newPosition": 1, "won": false}, + {"position": 1, "newPosition": 1, "won": false} + +] +let playerArr3 = [ + {"position": 1, "newPosition": 1, "won": false}, + {"position": 1, "newPosition": 1, "won": false}, + {"position": 1, "newPosition": 1, "won": false}, + +] +let playerArr4 = [ + {"position": 1, "newPosition": 1, "won": false}, + {"position": 1, "newPosition": 1, "won": false}, + {"position": 1, "newPosition": 1, "won": false}, + {"position": 1, "newPosition": 1, "won": false} + +] +let rolls =[1,2,3,4,5,6]; + + +let ladderArr = ( +{start: 7, end: 33}, +{start: 24, end: 44}, +{start: 31, end: 55}, +{start: 60, end: 77} +) +let snakeArr = ( +{start: 99, end: 56}, +{start: 86, end: 64}, +{start: 60, end: 28}, +{start: 18, end: 1} +) + +//Eine Test Gruppe, mit mehreren Tests +describe('Testen, ob für jede mögliche Spieleranzahl die richtige Anzahl von Spielerobjekten erzeugt wird', () => { + + it('testen ob 2 Spieler Objekte generiert wird ', ()=>{ + expect(buildPlayerArray(2)).toEqual(playerArr2); + }); + it('testen ob 3 Spieler Objekte generiert wird ', ()=>{ + expect(buildPlayerArray(3)).toEqual(playerArr3); + }); + it('testen ob 4 Spieler Objekte generiert wird ', ()=>{ + expect(buildPlayerArray(4)).toEqual(playerArr4); + }); +}) + +describe('Testen ob das movePlayer funkction:', () => { + + it('die Position bei jeder möglichen Rolle korrekt ändert', ()=>{ + for(let i = 0; i { + + + it('101 bis 130 wieder auf 100 setzen', ()=>{ + for(let i = 1; i <30; i++){ + let player = {"position": 1, "newPosition": 100, "won": false}; + over100(player); + expect(player).toEqual({"position": 1, "newPosition": 100, "won": false}); + } + + }); + +}) + + +describe('tests testen', () => { + + + it('testen alle leitern', ()=>{ + for(let i = 0; i <= ladderArr.length; i++) + { + landedOn(player, ladderArr[i]); + expect(player).toBe({"position": 1, "newPosition": ladderArr[i].end, "won": false}); + } + }); + it('testen alle schlange', ()=>{ + + for(let i = 0; i <= snakeArr.length; i++) + { + landedOn(player, snakeArr[i]); + expect(player).toBe({"position": 1, "newPosition": ladderArr[i].end, "won": false}); + } + }); + +}) + + +describe('testen, ob die neue Position die aktuelle Position ersetzt ', () => { + + + it('mit einer Zufallszahl von 1 bis 100 zwanzigmal', ()=>{ + for(let i = 0; i <= 20; i++) + { + min = Math.ceil(1); + max = Math.floor(100); + let position = Math.floor(Math.random() * (max - min + 1) + min) + + let player = {"position": 1, "newPosition": position, "won": false} + resetNewOld(player); + expect(player).toEqual({"position": position, "newPosition": 0, "won": false}); + } + + }); + +}) \ No newline at end of file