Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // start hier
- class Dobbelsteen {
- constructor() {
- this._aantalogen = 1;
- }
- get aantalogen() {
- return this._aantalogen;
- }
- rol() {
- this._aantalogen = Math.floor(Math.random() * 6) + 1;
- }
- }
- class Speler {
- constructor(naam) {
- this._naam = naam;
- this._score = 0;
- this._dobbelstenen = [];
- for (let i = 0; i < 5; i++) {
- this._dobbelstenen.push(new Dobbelsteen());
- }
- }
- get naam() {
- return this._naam;
- }
- get score() {
- return this._score;
- }
- get dobbelstenen() {
- return this._dobbelstenen;
- }
- speel() {
- for (let dobbelsteen of this._dobbelstenen) {
- dobbelsteen.rol();
- if (dobbelsteen._aantalogen == 1) {
- this._score += 100;
- }
- else if (dobbelsteen._aantalogen == 5) {
- this._score += 50;
- }
- }
- }
- }
- class Spel {
- constructor(spelers) {
- this._spelers = spelers;
- this._spelerAanZet = this._spelers[0];
- }
- get spelerAanZet() {
- return this._spelerAanZet;
- }
- get aantalSpelers() {
- return this._spelers.length;
- }
- get heeftWinnaar() {
- return this._spelers.some((value) => value.score >= 1000);
- }
- get scoreOverzicht() {
- return this._spelers.reduce((result, value) =>
- {return result += value.naam + ': ' + value.score + '\n'}, '');
- }
- speel() {
- if(!this.heeftWinnaar) this._spelerAanZet.speel();
- }
- bepaalVolgendeSpeler() {
- if (!this.heeftWinnaar) {
- const index = (this._spelers.findIndex(this._spelerAanZet) + 1) % spelers.length;
- this._spelersAanzet = this._spelerAanZet[index];
- }
- }
- }
- function toHtml(spel) {
- document.getElementById("speler").innerHTML = `Speler aan zet is ${spel._spelerAanZet.naam}`;
- document.getElementById("speler").innerHTML = `De score is ${spel._spelerAanZet.score}`;
- for (i = 0; i < spel._spelerAanZet.dobbelstenen.length; i++) {
- document.getElementById(i + 1).src = `images/Dice${spel._spelerAanZet.dobbelstenen[i].aantalogen}.png`;
- }
- if(spel.heeftWinnaar) {
- alert(`Gefeliciteerd ${spel._spelerAanZet.naam}`);
- }
- }
- function init() {
- const aantalSpelers = parseInt(prompt("Geef het aantal spelers"));
- const arrSpelers = [];
- for(let i = 0; i < aantalSpelers; i++) {
- const naam = prompt(`Geef de naam van speler ${i + 1}`);
- arrSpelers.push(new Speler(naam));
- }
- const spel = new Spel(arrSpelers);
- document.getElementById("play").onclick = function() {
- spel.speel();
- toHtml(spel);
- }
- document.getElementById("scorebord").onclick = function() {
- alert(spel.scoreOverzicht);
- }
- }
- window.onload = init;
- /***************************************************************************************** */
- /* ondestaand stukje code heb je pas in de laatste stap van de oefening nodig (zie opgave) */
- /***************************************************************************************** */
- // if (document.getElementById('play').value === 'Rol dobbelstenen') {
- // document.getElementById('play').value = 'Volgende speler';
- // document.getElementById('play').onclick = function() {
- // spel.bepaalVolgendeSpeler();
- // toHtml(spel);
- // };
- // } else {
- // document.getElementById('play').value = 'Rol dobbelstenen';
- // document.getElementById('play').onclick = function() {
- // spel.speel();
- // toHtml(spel);
- // };
- // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement