Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // start hier
  2. class Dobbelsteen {
  3.     constructor() {
  4.         this._aantalogen = 1;
  5.     }
  6.  
  7.     get aantalogen() {
  8.         return this._aantalogen;
  9.     }
  10.  
  11.     rol() {
  12.         this._aantalogen = Math.floor(Math.random() * 6) + 1;
  13.     }    
  14. }
  15.  
  16. class Speler {
  17.     constructor(naam) {
  18.         this._naam = naam;
  19.         this._score = 0;
  20.         this._dobbelstenen = [];
  21.         for (let i = 0; i < 5; i++) {
  22.             this._dobbelstenen.push(new Dobbelsteen());
  23.         }
  24.     }
  25.  
  26.     get naam() {
  27.         return this._naam;
  28.     }
  29.  
  30.     get score() {
  31.         return this._score;
  32.     }
  33.  
  34.     get dobbelstenen() {
  35.         return this._dobbelstenen;
  36.     }
  37.    
  38.     speel() {
  39.         for (let dobbelsteen of this._dobbelstenen) {
  40.             dobbelsteen.rol();
  41.             if (dobbelsteen._aantalogen == 1) {
  42.                 this._score += 100;
  43.             }
  44.             else if (dobbelsteen._aantalogen == 5) {
  45.                 this._score += 50;
  46.             }
  47.         }
  48.     }
  49. }
  50.  
  51. class Spel {
  52.     constructor(spelers) {
  53.         this._spelers = spelers;
  54.         this._spelerAanZet = this._spelers[0];
  55.     }
  56.  
  57.     get spelerAanZet() {
  58.         return this._spelerAanZet;
  59.     }
  60.  
  61.     get aantalSpelers() {
  62.         return this._spelers.length;
  63.     }
  64.  
  65.     get heeftWinnaar() {
  66.         return this._spelers.some((value) => value.score >= 1000);
  67.     }
  68.  
  69.     get scoreOverzicht() {
  70.         return this._spelers.reduce((result, value) =>
  71.         {return result += value.naam + ': ' + value.score + '\n'}, '');
  72.     }
  73.  
  74.     speel() {
  75.         if(!this.heeftWinnaar) this._spelerAanZet.speel();
  76.     }
  77.  
  78.     bepaalVolgendeSpeler() {
  79.         if (!this.heeftWinnaar) {
  80.             const index = (this._spelers.findIndex(this._spelerAanZet) + 1) % spelers.length;
  81.             this._spelersAanzet = this._spelerAanZet[index];
  82.         }
  83.     }
  84.    
  85. }
  86.  
  87. function toHtml(spel) {
  88.     document.getElementById("speler").innerHTML = `Speler aan zet is ${spel._spelerAanZet.naam}`;
  89.     document.getElementById("speler").innerHTML = `De score is ${spel._spelerAanZet.score}`;
  90.     for (i = 0; i < spel._spelerAanZet.dobbelstenen.length; i++) {
  91.         document.getElementById(i + 1).src = `images/Dice${spel._spelerAanZet.dobbelstenen[i].aantalogen}.png`;
  92.     }
  93.     if(spel.heeftWinnaar) {
  94.         alert(`Gefeliciteerd ${spel._spelerAanZet.naam}`);
  95.     }    
  96. }
  97.  
  98. function init() {
  99.     const aantalSpelers = parseInt(prompt("Geef het aantal spelers"));
  100.     const arrSpelers = [];
  101.     for(let i = 0; i < aantalSpelers; i++) {
  102.         const naam = prompt(`Geef de naam van speler ${i + 1}`);
  103.         arrSpelers.push(new Speler(naam));
  104.     }
  105.     const spel = new Spel(arrSpelers);
  106.  
  107.     document.getElementById("play").onclick = function() {
  108.         spel.speel();
  109.         toHtml(spel);
  110.     }
  111.     document.getElementById("scorebord").onclick = function() {
  112.         alert(spel.scoreOverzicht);
  113.     }
  114.  
  115. }
  116.  
  117. window.onload = init;
  118. /***************************************************************************************** */
  119. /* ondestaand stukje code heb je pas in de laatste stap van de oefening nodig (zie opgave) */
  120. /***************************************************************************************** */
  121. // if (document.getElementById('play').value === 'Rol dobbelstenen') {
  122. //  document.getElementById('play').value = 'Volgende speler';
  123. //  document.getElementById('play').onclick = function() {
  124. //      spel.bepaalVolgendeSpeler();
  125. //      toHtml(spel);
  126. //  };
  127. // } else {
  128. //  document.getElementById('play').value = 'Rol dobbelstenen';
  129. //  document.getElementById('play').onclick = function() {
  130. //      spel.speel();
  131. //      toHtml(spel);
  132. //  };
  133. // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement