Browse Source

refactoring: Funktionen checkPostion, buildBoard und rollDice in kleinere Funktionen zum Testen aufgeteilt

main
Richard Halsall 2 years ago
parent
commit
0fb99a74a1
  1. 53
      schlangen_und_leitern/js/boardHandling.js
  2. 29
      schlangen_und_leitern/js/diceHandling.js
  3. 30
      schlangen_und_leitern/js/playerHandling.js

53
schlangen_und_leitern/js/boardHandling.js

@ -1,6 +1,20 @@
//ein Array erstellen, aus dem die Spilebrett gezeichnet werden kann
function buildBoard(objectArr,canvasPointArr){
// build board array
let boardArr = buildBoardArray()
// 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;
@ -29,20 +43,8 @@ function buildBoard(objectArr,canvasPointArr){
}
boardArr.push(row)
}
// 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)
}
return boardArr;
}
function drawBoard(arr){
@ -256,30 +258,7 @@ function drawBoardObjects(canvasPointArr, objectArray){
}
}
function checkPosition(player, objectArr){
//above 100
if(player.newPosition > 100){
player.newPosition = 100;
}
//win condition
if(player.newPosition == 100){
player.won = true;
changeElement("continue", "block")
changeElement("replay", "block")
changeElement("würfelBereich", "none")
}
//snake condition
//ladder condition
for(let i = 0; i < (objectArr.length) ; i++){
if(player.newPosition == objectArr[i].start){
player.newPosition = objectArr[i].end;
}
}
}
function continueGame (){

29
schlangen_und_leitern/js/diceHandling.js

@ -10,21 +10,26 @@ 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;
}

30
schlangen_und_leitern/js/playerHandling.js

@ -180,32 +180,34 @@ function turnIndicater(turnCount){
function checkPosition(player, objectArr){
//above 100
if(player.newPosition > 100){
player.newPosition = 100;
}
over100(player)
//win condition
if(player.newPosition == 100){
player.won = true;
changeElement("continue","block")
changeElement("replay","block")
changeElement("würfelBereich","none")
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;
}
}
}
Loading…
Cancel
Save