Browse Source
Klasse Game hinzugefügt + UnitTest
Klasse Game hinzugefügt + UnitTest
- generatePool(), zum Generieren des SpielPools mit Kartenmain
Nicolas Fritz
2 years ago
2 changed files with 230 additions and 0 deletions
@ -0,0 +1,50 @@ |
|||||
|
//Imports
|
||||
|
const uno = require("./uno"); |
||||
|
const Card = require("./cards/Card"); |
||||
|
const ChooseColor = require("./cards/special/ChooseColor"); |
||||
|
const Skip = require("./cards/special/Skip"); |
||||
|
const PlusAmount = require("./cards/special/PlusAmount"); |
||||
|
const Reverse = require("./cards/special/Reverse"); |
||||
|
|
||||
|
class Game { |
||||
|
|
||||
|
constructor(playerAmount, rules) { |
||||
|
|
||||
|
this._playerAmount = playerAmount; |
||||
|
this._rules = rules; |
||||
|
|
||||
|
this.initGame(); |
||||
|
} |
||||
|
|
||||
|
initGame(){ |
||||
|
this._cardPool = this.generatePool(); |
||||
|
} |
||||
|
|
||||
|
generatePool(){ |
||||
|
let pool = []; |
||||
|
for (let k = 0; k < 2; k++) { |
||||
|
for (let j = 1; j < (uno.CARD_COLORS.length); j++) { |
||||
|
pool.push(new Reverse("R", uno.CARD_COLORS[j])); |
||||
|
pool.push(new PlusAmount("+2", uno.CARD_COLORS[j])); |
||||
|
pool.push(new Skip("S", uno.CARD_COLORS[j])); |
||||
|
if (k === 0) { |
||||
|
pool.push(new ChooseColor("CC")); |
||||
|
pool.push(new PlusAmount("+4", uno.CARD_COLORS[0])); |
||||
|
} |
||||
|
for (let i = 0; i <= 9; i++) { |
||||
|
|
||||
|
if (k === 1 && i === 0) continue; |
||||
|
pool.push(new Card(i, uno.CARD_COLORS[j])); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
return pool; |
||||
|
} |
||||
|
|
||||
|
get cardPool(){ |
||||
|
return this._cardPool; |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
module.exports = Game; |
@ -0,0 +1,180 @@ |
|||||
|
//Imports
|
||||
|
const uno = require('../js/uno'); |
||||
|
const Game = require('../js/Game'); |
||||
|
|
||||
|
//Instanz von CARD_COLORS aus uno.js
|
||||
|
const CARD_COLORS = uno.CARD_COLORS; |
||||
|
|
||||
|
//Speichert Kartenanzahl der jeweiligen Farbe
|
||||
|
let green, red, yellow, blue; |
||||
|
|
||||
|
//Testet das Generieren von dem Pool mit allen Uno Karten drin
|
||||
|
describe('Pool aus Karten generieren', () => { |
||||
|
|
||||
|
//Game wird erstellt mit 0 Spielern und ohne Regeln
|
||||
|
let game = new Game(0, []); |
||||
|
|
||||
|
//Speichert die Art der Karten
|
||||
|
let reverse = [], |
||||
|
plus2 = [], |
||||
|
plus4 = [], |
||||
|
skip = [], |
||||
|
chooseColor = [], |
||||
|
normalCards = []; |
||||
|
|
||||
|
//Vor jedem durchlauf Kartenanzahl der Farben auf 0 setzen,
|
||||
|
//neues Game erstellen
|
||||
|
beforeEach(() => { |
||||
|
|
||||
|
game = new Game(0, []); |
||||
|
|
||||
|
green = 0; |
||||
|
red = 0; |
||||
|
yellow = 0; |
||||
|
blue = 0; |
||||
|
}); |
||||
|
|
||||
|
//Sortiert alle Karten aus cardPool in Arrays der Art, um später die Überprüfungen auszuführen
|
||||
|
game.cardPool.forEach(function (element){ |
||||
|
|
||||
|
switch (element.name){ |
||||
|
case "R": |
||||
|
reverse.push(element); |
||||
|
break; |
||||
|
case "+2": |
||||
|
plus2.push(element); |
||||
|
break; |
||||
|
case "+4": |
||||
|
plus4.push(element); |
||||
|
break; |
||||
|
case "S": |
||||
|
skip.push(element); |
||||
|
break; |
||||
|
case "CC": |
||||
|
chooseColor.push(element); |
||||
|
break; |
||||
|
default: |
||||
|
normalCards.push(element); |
||||
|
break; |
||||
|
} |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
|
||||
|
it('Reverse: 2x in jeder Farbe (8)', () => { |
||||
|
|
||||
|
//Sortiert reverse nach Farben und speichert in green,red,yellow,blue
|
||||
|
getColorAmounts(reverse); |
||||
|
|
||||
|
//Jede Farbe muss genau 2 Reverse Karten besitzen
|
||||
|
expect(green).toBe(2); |
||||
|
expect(red).toBe(2); |
||||
|
expect(yellow).toBe(2); |
||||
|
expect(blue).toBe(2); |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
it('+2: 2x in jeder Farbe (8)', () => { |
||||
|
|
||||
|
//Sortiert plus2 nach Farben und speichert in green,red,yellow,blue
|
||||
|
getColorAmounts(plus2); |
||||
|
|
||||
|
//Jede Farbe muss genau 2 +2 Karten besitzen
|
||||
|
expect(green).toBe(2); |
||||
|
expect(red).toBe(2); |
||||
|
expect(yellow).toBe(2); |
||||
|
expect(blue).toBe(2); |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
it('Skip: 2x in jeder Farbe (8)', () => { |
||||
|
|
||||
|
//Sortiert skip nach Farben und speichert in green,red,yellow,blue
|
||||
|
getColorAmounts(skip); |
||||
|
|
||||
|
//Jede Farbe muss genau 2 Aussetzer(Skip) Karten besitzen
|
||||
|
expect(green).toBe(2); |
||||
|
expect(red).toBe(2); |
||||
|
expect(yellow).toBe(2); |
||||
|
expect(blue).toBe(2); |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
it('ChooseColor: 4x', () => { |
||||
|
|
||||
|
//Es muss genau 4 Farbwahl-Karten geben
|
||||
|
expect(chooseColor.length).toBe(4); |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
it('+4: 4x', () => { |
||||
|
|
||||
|
//Es muss genau 4 +4-Karten geben
|
||||
|
expect(plus4.length).toBe(4); |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
it('normale Karten: 2x jede 1-9, 4x 0 (76)', () => { |
||||
|
|
||||
|
//nimmt 3 Zahlen und testet diese
|
||||
|
let zero = 0; |
||||
|
let seven = 0; |
||||
|
let one = 0; |
||||
|
|
||||
|
//guckt ob insgesamt 76 normale Karten im Array sind
|
||||
|
expect(normalCards.length).toBe(76); |
||||
|
|
||||
|
//testet die 3 Zahlen
|
||||
|
normalCards.forEach(function (element){ |
||||
|
|
||||
|
//Anzahl 0len in zero
|
||||
|
if(element.name === 0) |
||||
|
zero++; |
||||
|
|
||||
|
//Anzahl 7 - Grün in seven
|
||||
|
else if(element.name === 7 && element.color === CARD_COLORS[2]) |
||||
|
seven++; |
||||
|
|
||||
|
//Anzahl 1sen in one
|
||||
|
else if(element.name === 1) |
||||
|
one++; |
||||
|
}); |
||||
|
|
||||
|
//Testet ob genau 4 0len im Deck sind
|
||||
|
expect(zero).toBe(4); |
||||
|
|
||||
|
//Testet ob 2 Grüne 7ben im Deck sind
|
||||
|
expect(seven).toBe(2); |
||||
|
|
||||
|
//Testet ob 8 1sen im Deck sind
|
||||
|
expect(one).toBe(8); |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
}) |
||||
|
|
||||
|
//Setzt die Kartenanzahl der Farben für array
|
||||
|
function getColorAmounts(array){ |
||||
|
|
||||
|
//Für jedes Element im übergebenen Array wird für dessen Farbe die dazugehörige Variable um eins erhöht
|
||||
|
array.forEach(function (element){ |
||||
|
switch (element.color){ |
||||
|
case "GREEN": |
||||
|
green++; |
||||
|
break; |
||||
|
case "RED": |
||||
|
red++; |
||||
|
break; |
||||
|
case "YELLOW": |
||||
|
yellow++; |
||||
|
break; |
||||
|
case "BLUE": |
||||
|
blue++; |
||||
|
break; |
||||
|
default: |
||||
|
break; |
||||
|
} |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue