Created
May 9, 2018 13:16
-
-
Save abeforgit/d915e8384a3b58f5ecf2801248c90add to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
class Kaart { | |
constructor(shape, color, number) { | |
this.vorm = shape; | |
this.kleur = color; | |
this.getal = number; | |
if (!Kaart.checkParams(shape, color, number)) { | |
throw "AssertionError: ongeldige kaart" | |
} | |
} | |
toString() { | |
return this.vorm.charAt(0).toUpperCase() | |
+ this.kleur.charAt(0).toUpperCase() | |
+ this.getal.toString(); | |
} | |
static checkParams(shape, color, number) { | |
const shapes = ["cirkel", "driehoek", "vierkant", "kruis"]; | |
const colors = ["groen", "oranje", "blauw", "rood"]; | |
const nums = [1, 2, 3, 4]; | |
return shapes.includes(shape) | |
&& colors.includes(color) | |
&& nums.includes(number); | |
} | |
} | |
class Lijn { | |
constructor(...kaarten) { | |
this.kaarten = kaarten; | |
this.colors = []; | |
this.shapes = []; | |
this.nums = []; | |
this.kaarten.forEach(card => { | |
this.colors.push(card.kleur); | |
this.shapes.push(card.vorm); | |
this.nums.push(card.getal); | |
}); | |
if (!this.validate(this.colors, this.shapes, this.nums) || this.kaarten.length === 0) { | |
throw "AssertionError: ongeldige lijn"; | |
} | |
} | |
toString() { | |
return this.kaarten.join(' - '); | |
} | |
aanleggen(kaart, front = false) { | |
let newcols = this.colors.concat(kaart.kleur); | |
let newshaps = this.shapes.concat(kaart.vorm); | |
let newnums = this.nums.concat(kaart.getal); | |
if (!this.validate(newcols, newshaps, newnums) || newcols.length > 4) { | |
throw "AssertionError: ongeldige lijn"; | |
} | |
if (front) { | |
this.kaarten.unshift(kaart); | |
} else { | |
this.kaarten.push(kaart); | |
} | |
return this; | |
} | |
validate(cols, shapes, nums) { | |
if (this.isSame(cols) && this.isSame(shapes) && this.isSame(nums)) { | |
return false; | |
} | |
return (this.isSame(cols) || this.isDiff(cols)) && (this.isSame(shapes) || this.isDiff(shapes)) | |
&& (this.isSame(nums) || this.isDiff(nums)); | |
} | |
isSame(list) { | |
let set = new Set(list); | |
return set.size === 1; | |
} | |
isDiff(list) { | |
let set = new Set(list); | |
return set.size === list.length; | |
} | |
kwatro() { | |
const shapes = ["cirkel", "driehoek", "vierkant", "kruis"]; | |
const colors = ["groen", "oranje", "blauw", "rood"]; | |
const nums = [1, 2, 3, 4]; | |
if (this.kaarten.length !== 3) { | |
throw "AssertionError: lijn moet uit drie kaarten bestaan" | |
} | |
let allcards = []; | |
for (let color of colors) { | |
for (let num of nums) { | |
for (let shape of shapes) { | |
allcards.push(new Kaart(shape, color, num)); | |
} | |
} | |
} | |
for (let card of allcards) { | |
try { | |
let lijn = new Lijn(...this.kaarten.concat(card)); | |
let rslt = card; | |
} catch (e) { | |
continue; | |
} | |
return card; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment