Raphael Elström 2 years ago
parent
commit
74a478f438
  1. 13
      schlangen_und_leitern/HTML.html
  2. 63
      schlangen_und_leitern/css/style.css
  3. 63
      schlangen_und_leitern/js/Bridge.js
  4. 377
      schlangen_und_leitern/js/boardHandling.js
  5. 21
      schlangen_und_leitern/js/diceHandling.js
  6. 141
      schlangen_und_leitern/js/makeBoard.js
  7. 123
      schlangen_und_leitern/js/playerHandling.js
  8. 114
      schlangen_und_leitern/tests/boardHandling.test.js
  9. 14
      schlangen_und_leitern/tests/diceHandling.test.js
  10. 129
      schlangen_und_leitern/tests/playerHandling.test.js

13
schlangen_und_leitern/HTML.html

@ -6,10 +6,9 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="text/javascript" src="js/diceHandling.js"></script> <script type="text/javascript" src="js/diceHandling.js"></script>
<script type= "text/javascript" src="js/makeBoard.js"></script>
<script type= "text/javascript" src="js/boardHandling.js"></script>
<script type= "text/javascript" src="js/Bridge.js"></script> <script type= "text/javascript" src="js/Bridge.js"></script>
<script type= "text/javascript" src="js/playerHandling.js"></script> <script type= "text/javascript" src="js/playerHandling.js"></script>
<title>Document</title> <title>Document</title>
@ -27,11 +26,19 @@
<div calss = container id="playArea" > <div calss = container id="playArea" >
<div id="leftSide">
<canvas id= "canvas"></canvas>
<table id = "board"></table> <table id = "board"></table>
</div>
<div id = "rightSide">
<p id ="isUp">Spieler 1 ist dran</p>
<input type="button" id ="replay" onclick="document.location.reload(true)" value = "Neustart" style="display: none;">
<input type="button" id ="continue" onclick= "continueGame()" value = "Spiel weiter"style="display: none;">
<div class = container id="würfelBereich" style="display: none;" > <div class = container id="würfelBereich" style="display: none;" >
<img src="/img/wurfle/f(1).png" id="face"> <img src="/img/wurfle/f(1).png" id="face">
<input type = "button" id="rollButton" value = "Würfeln"> <input type = "button" id="rollButton" value = "Würfeln">
</div>
</div> </div>
</div> </div>

63
schlangen_und_leitern/css/style.css

@ -1,7 +1,8 @@
h1{ h1{
text-align: center; text-align: center;
font-size: 50px;
font-size: 65px;
text-decoration: underline;
} }
.playerSelect{ .playerSelect{
@ -10,13 +11,20 @@ h1{
justify-content: center; justify-content: center;
}
.playerButton{
outline: auto;
} }
#würfelBereich{ #würfelBereich{
height: 170px; height: 170px;
width: 170px; width: 170px;
background-color: rgb(255, 255, 255);
display: grid; display: grid;
grid-template-columns: 30px 100px 30px; grid-template-columns: 30px 100px 30px;
grid-template-rows: 30px 100px 30px; grid-template-rows: 30px 100px 30px;
@ -49,9 +57,9 @@ width: auto;
} }
.th, td{ .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%; border-radius: 50%;
background-color: rgb(255, 221, 0); 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 {
} }

63
schlangen_und_leitern/js/Bridge.js

@ -2,50 +2,68 @@
let turnCount = 1; let turnCount = 1;
function init(playerCount){ 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 // Spielbrett Aufbauen
boardArr = buildBoard();
drawBoard(boardArr);
buildBoard(objectArr,canvasPointArr);
//spielbret objekte genererien //spielbret objekte genererien
let objectArr = [];
generateBoardObjects(leitern, schlangen, objectArr);
// spieler Strukturen aufbauen mit hilfe spieler Anzahl // spieler Strukturen aufbauen mit hilfe spieler Anzahl
let playerArray = buildPlayerArray(playerCount); let playerArray = buildPlayerArray(playerCount);
//Haupt Spielschleife aufrufen //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); playerInit(playerArray);
////////experiemnt
canvasPointGen(canvasPointArr);
drawBoardObjects(canvasPointArr, objectArr)
} }
function gameloop (boardArr, playerArray, playerCount){
function gameloop (playerArray, playerCount, objectArr){
console.log("player " + turnCount + " ist dran"); console.log("player " + turnCount + " ist dran");
player = playerArray[turnCount-1];
if(player.won == false){
//würfle function aufrufen //würfle function aufrufen
let roll = rollDice(); let roll = rollDice();
//spieler auswählen //spieler auswählen
player = playerArray[turnCount-1];
//spieler newPosition mit roll Aktualisierung //spieler newPosition mit roll Aktualisierung
movePlayer(player, roll); movePlayer(player, roll);
//TODO checken ob die Spieler an eine leiter oder Schlange gelandet hat //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 checken ob die Spieler an eine leiter oder Schlange gelandet hat
//alter Stelle löschen
eraseOLd(playerArray,turnCount); eraseOLd(playerArray,turnCount);
// setzen newPostion auf position um und neuPosition leeren // setzen newPostion auf position um und neuPosition leeren
resetNewOld(player); resetNewOld(player);
//TODO spieler Stelle auf dem Brett zeichnen
//spieler Stelle auf dem Brett zeichnen
drawNew(playerArray,turnCount); drawNew(playerArray,turnCount);
//TODO alter Stelle löschen
//TODO prüfen ob jemand gewonnen hat und wenn ja spiele beenden // weiter spielen //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 // Reihezähler inkrementieren bzw. zurücksetzen
if(turnCount >= playerCount){ if(turnCount >= playerCount){
@ -54,5 +72,16 @@ function gameloop (boardArr, playerArray, playerCount){
else { else {
turnCount ++ turnCount ++
} }
//show who is up
turnIndicater(turnCount);
}
else{
if(turnCount >= playerCount){
turnCount = 1;
}
else {
turnCount ++
}
}
} }

377
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
}

21
schlangen_und_leitern/js/diceHandling.js

@ -10,6 +10,16 @@ let faces = ["img/wurfle/f(1).png",
//dice //dice
function rollDice() { function rollDice() {
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 und Min Zahl geben
max = 6; max = 6;
min = 1 min = 1
@ -17,14 +27,11 @@ function rollDice() {
min = Math.ceil(min); min = Math.ceil(min);
max = Math.floor(max); max = Math.floor(max);
//ein zufällige zahl zwischen min und max generieren //ein zufällige zahl zwischen min und max generieren
roll = Math.floor(Math.random() * (max - min + 1) + min);
num = Math.floor(Math.random() * (max - min + 1) + min);
return num;
// Wurfelbild ändern
document.querySelector("#face").src= faces[roll-1];
console.log(roll);
}
module.exports = generateNumber;
return roll;
}

141
schlangen_und_leitern/js/makeBoard.js

@ -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);
}

123
schlangen_und_leitern/js/playerHandling.js

@ -4,7 +4,8 @@
function PlayerBuilder(){ function PlayerBuilder(){
this.position = 1; 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++){ for(let i = 1; i <= playerCount; i++){
let player = new PlayerBuilder(i); let player = new PlayerBuilder(i);
playerArray.push(player) playerArray.push(player)
console.log("player added to array!")
} }
return playerArray; return playerArray;
@ -32,44 +33,26 @@ function movePlayer(player, roll){
player.newPosition = player.position + roll; player.newPosition = player.position + roll;
if(player.newPosition >= 100){ if(player.newPosition >= 100){
player.newPosition = 100; player.newPosition = 100;
//TODO gewinn aufrufen.
} }
//TODO Schlange oder Leiter fehlschalg.
} }
//Draw Spieler bei Spiele init
function playerInit(playerArray){
for(let i = 0; i < playerArray.length; i++){
function initialise(count, playerArray){
//player id ie. count 0 =
var id = count +1;
var Token = document.createElement("div") 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
let position = playerArray[count].position
var boardPostition = boardLocation(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";
Token.classList.add("p"+ id);
Token.id = "p" + id;
document.getElementById(boardPostition).appendChild(Token) document.getElementById(boardPostition).appendChild(Token)
}
}
//Draw Spieler bei Spiele init
function playerInit(playerArray){
for(let i = 0; i < playerArray.length; i++){
initialise(i,playerArray)
} }
} }
@ -131,7 +114,7 @@ function movePlayer(player, roll){
} }
} }
//TODO zeichen für jede Spieler
@ -167,3 +150,75 @@ function movePlayer(player, roll){
} }
} }
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
}

114
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]);
}
});
})

14
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);
}
})
})

129
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 <rolls.length; i++)
{
movePlayer(player, rolls[i])
expect(player).toEqual({"position": 1, "newPosition": 1 +rolls[i], "won": false});
}
});
})
describe('Testen, ob der Spieler in einer Position über 100 sein kann', () => {
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});
}
});
})
Loading…
Cancel
Save