|
|
@ -6,6 +6,7 @@ import PlusAmount from "./cards/special/PlusAmount.js"; |
|
|
|
import Reverse from "./cards/special/Reverse.js"; |
|
|
|
import Player from "./Player.js"; |
|
|
|
import {CARD_COLORS} from "./uno.js"; |
|
|
|
import Style from "./Style.js"; |
|
|
|
|
|
|
|
//Um generatePool zu exportieren, muss es in eine Klasse konvertiert werden
|
|
|
|
export default class Game { |
|
|
@ -22,10 +23,13 @@ export default class Game { |
|
|
|
this._playerAmount = playerAmount; //Anzahl der Spieler
|
|
|
|
this._rules = rules; //Array mit Regeln für das Spiel
|
|
|
|
|
|
|
|
//Für HTML
|
|
|
|
this._style = new Style(this); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
//Richtet das Spiel ein
|
|
|
|
initGame(){ |
|
|
|
initGame() { |
|
|
|
|
|
|
|
//CardPool wird generiert
|
|
|
|
this.cardPool = this.generatePool(); |
|
|
@ -36,35 +40,27 @@ export default class Game { |
|
|
|
} |
|
|
|
|
|
|
|
//Startet das Spiel
|
|
|
|
start(){ |
|
|
|
if(this.currentPlayer !== -1) return; |
|
|
|
start() { |
|
|
|
if (this.currentPlayer !== -1) return; |
|
|
|
|
|
|
|
//Wenn das Spiel noch nicht initialisiert wurde, initialisiere es
|
|
|
|
if (this.cardPool.length === 0 || this.players.length === 0) |
|
|
|
this.initGame(); |
|
|
|
if (this.cardPool.length === 0 || this.players.length === 0) this.initGame(); |
|
|
|
|
|
|
|
|
|
|
|
let firstCardIndex = 0; |
|
|
|
let boolFirstSpecial = false; |
|
|
|
|
|
|
|
if (this.rules !== null){ |
|
|
|
if('startCards' in this.rules) |
|
|
|
for (let i = 0; i < this.players.length; i++) |
|
|
|
this.players[i].drawCard(this.rules.startCards); |
|
|
|
|
|
|
|
if('firstPlaySpecial' in this.rules) |
|
|
|
boolFirstSpecial = this.rules.firstPlaySpecial; |
|
|
|
|
|
|
|
if (this.rules !== null) { |
|
|
|
if ('startCards' in this.rules) for (let i = 0; i < this.players.length; i++) this.players[i].drawCard(this.rules.startCards, false, false); |
|
|
|
|
|
|
|
if ('firstPlaySpecial' in this.rules) boolFirstSpecial = this.rules.firstPlaySpecial; |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (!boolFirstSpecial){ |
|
|
|
for (let i = 0; i < this.cardPool.length; i++){ |
|
|
|
if (!(this.cardPool[i].name === 'R' || this.cardPool[i].name === 'S' |
|
|
|
|| this.cardPool[i].name === 'CC' || this.cardPool[i].name === '+2' |
|
|
|
|| this.cardPool[i].name === '+4')){ |
|
|
|
if (!boolFirstSpecial) { |
|
|
|
for (let i = 0; i < this.cardPool.length; i++) { |
|
|
|
if (!(this.cardPool[i].name === 'R' || this.cardPool[i].name === 'S' || this.cardPool[i].name === 'CC' || this.cardPool[i].name === '+2' || this.cardPool[i].name === '+4')) { |
|
|
|
firstCardIndex = i; |
|
|
|
break; |
|
|
|
} |
|
|
@ -73,15 +69,19 @@ export default class Game { |
|
|
|
|
|
|
|
//Die Erste Karte wird auf den Tisch gelegt
|
|
|
|
this.cardOnDeck = this.cardPool[firstCardIndex]; |
|
|
|
this.cardPool.splice(firstCardIndex,1); |
|
|
|
this.cardPool.splice(firstCardIndex, 1); |
|
|
|
|
|
|
|
//Karten Funktion der Karte auf dem Deck ausführen
|
|
|
|
this.cardOnDeck.putSelf(); |
|
|
|
|
|
|
|
|
|
|
|
//HTML
|
|
|
|
|
|
|
|
this.style.refreshHtml(); |
|
|
|
} |
|
|
|
|
|
|
|
//Gibt ein Array zurück mit allen Karten, die in einem Uno Spiel sind
|
|
|
|
generatePool(){ |
|
|
|
generatePool() { |
|
|
|
|
|
|
|
//Array wird erstellt, welches später zurückgegeben wird und alle Karten beinhaltet
|
|
|
|
let pool = []; |
|
|
@ -116,43 +116,53 @@ export default class Game { |
|
|
|
} |
|
|
|
|
|
|
|
//Mischt das Array
|
|
|
|
pool.sort(()=> Math.random() - 0.5); |
|
|
|
pool.sort(() => Math.random() - 0.5); |
|
|
|
|
|
|
|
//Array mit Karten wird zurückgegeben
|
|
|
|
return pool; |
|
|
|
} |
|
|
|
|
|
|
|
//Fügt die Spieler hinzu
|
|
|
|
createPlayers(playerAmount){ |
|
|
|
createPlayers(playerAmount) { |
|
|
|
|
|
|
|
//Erstelle so viele Spieler, wie bei Erstellung des Spiels übergeben wurden
|
|
|
|
for (let i = 0; i < playerAmount; i++){ |
|
|
|
for (let i = 0; i < playerAmount; i++) { |
|
|
|
this.players.push(new Player("Player" + (i + 1), this)); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
//Beendet den Zug des aktuellen Spielers und beginnt den Zug des nächsten Spielers
|
|
|
|
nextTurn(){ |
|
|
|
nextTurn() { |
|
|
|
let delay = 0; |
|
|
|
|
|
|
|
//Testet, ob Spiel Gewonnen
|
|
|
|
for (let i = 0; i < this.players.length; i++){ |
|
|
|
if(this.players[i].hand.length <= 0){ |
|
|
|
for (let i = 0; i < this.players.length; i++) { |
|
|
|
if (this.players[i].hand.length <= 0) { |
|
|
|
|
|
|
|
//Breche den Loop ab
|
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//Wenn Zug nicht der Erste vom ganzen Spiel
|
|
|
|
if(this.currentPlayer !== -1){ |
|
|
|
if (this.currentPlayer !== -1) { |
|
|
|
|
|
|
|
if (this.currentPlayerInstanz.mustSayUno === true) { |
|
|
|
this.currentPlayerInstanz.drawCard(2, false, true); |
|
|
|
delay += 1500; |
|
|
|
this.currentPlayerInstanz.mustSayUno = false; |
|
|
|
} |
|
|
|
|
|
|
|
//Aktuellen Spieler kann, darf nicht mehr Spielen
|
|
|
|
this.players[this.currentPlayer].canPlay = false; |
|
|
|
this.players[this.currentPlayer].turn = false; |
|
|
|
//Aktuellen Spieler kann, darf nicht mehr Spielen
|
|
|
|
this.players[this.currentPlayer].canPlay = false; |
|
|
|
this.players[this.currentPlayer].turn = false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//nächster Spieler wird gesetzt
|
|
|
|
this.currentPlayer = this.nextPlayer(); |
|
|
|
|
|
|
@ -161,21 +171,24 @@ export default class Game { |
|
|
|
//Aktualisiere das Deck des aktuellen Spielers, welche Karten er legen kann
|
|
|
|
this.refreshCanPutCard(); |
|
|
|
|
|
|
|
//HTML
|
|
|
|
|
|
|
|
setTimeout(()=>{ |
|
|
|
this.style.refreshHtml(); |
|
|
|
}, delay) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
//Testet alle Karten des aktuellen Spielers in seiner Hand, ob er sie legen kann
|
|
|
|
refreshCanPutCard(){ |
|
|
|
refreshCanPutCard() { |
|
|
|
//Deck des aktuellen Spielers
|
|
|
|
let currentPlayerCards = this.currentPlayerInstanz.hand; |
|
|
|
|
|
|
|
//Gehe alle Karten vom Deck durch
|
|
|
|
for(let i = 0; i < currentPlayerCards.length; i++){ |
|
|
|
for (let i = 0; i < currentPlayerCards.length; i++) { |
|
|
|
|
|
|
|
//Wenn Farbe oder Zahl gleich oder eine Karte, die keine Farbe hat
|
|
|
|
if(this.cardOnDeck.name.toString() === currentPlayerCards[i].name.toString() || |
|
|
|
this.cardOnDeck.color === currentPlayerCards[i].color || |
|
|
|
currentPlayerCards[i].color === CARD_COLORS[0] || |
|
|
|
this.cardOnDeck.color === CARD_COLORS[0]) { |
|
|
|
if (this.cardOnDeck.name.toString() === currentPlayerCards[i].name.toString() || this.cardOnDeck.color === currentPlayerCards[i].color || currentPlayerCards[i].color === CARD_COLORS[0] || this.cardOnDeck.color === CARD_COLORS[0]) { |
|
|
|
|
|
|
|
//Aktualisiere den Wert der Karte, sodass sie gelegt werden kann
|
|
|
|
this.players[this.currentPlayer].hand[i].canPut = true; |
|
|
@ -192,70 +205,75 @@ export default class Game { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
//Uno sagen testen
|
|
|
|
|
|
|
|
if (this.currentPlayerInstanz.hand.length <= 2 && this.currentPlayerInstanz.canPlay) this.currentPlayerInstanz.mustSayUno = true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
//Errechne, wer der nächste Spieler ist
|
|
|
|
nextPlayer(){ |
|
|
|
if(this.currentPlayer === -1) |
|
|
|
return 0; |
|
|
|
nextPlayer() { |
|
|
|
if (this.currentPlayer === -1) return 0; |
|
|
|
|
|
|
|
//Anhand der Spielrichtung errechnen
|
|
|
|
if(this._direction === 1) |
|
|
|
return (this._currentPlayer === this._players.length - 1) ? 0 : this._currentPlayer + 1; //bei normaler Richtung
|
|
|
|
else |
|
|
|
return (this._currentPlayer === 0) ? this._players.length - 1 : this._currentPlayer - 1; //bei Invertierter Richtung
|
|
|
|
if (this._direction === 1) return (this._currentPlayer === this._players.length - 1) ? 0 : this._currentPlayer + 1; //bei normaler Richtung
|
|
|
|
else return (this._currentPlayer === 0) ? this._players.length - 1 : this._currentPlayer - 1; //bei Invertierter Richtung
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
//Gib den Pool mit allen UnoKarten zurück
|
|
|
|
get cardPool(){ |
|
|
|
get cardPool() { |
|
|
|
return this._cardPool; |
|
|
|
} |
|
|
|
|
|
|
|
set cardPool(pool){ |
|
|
|
set cardPool(pool) { |
|
|
|
this._cardPool = pool; |
|
|
|
} |
|
|
|
|
|
|
|
//Gibt das Array mit allen Spielern des Spiels zurück
|
|
|
|
get players(){ |
|
|
|
get players() { |
|
|
|
return this._players; |
|
|
|
} |
|
|
|
|
|
|
|
//Gibt die aktuelle Karte auf dem Tisch zurück
|
|
|
|
get cardOnDeck(){ |
|
|
|
get cardOnDeck() { |
|
|
|
return this._cardOnDeck; |
|
|
|
} |
|
|
|
|
|
|
|
//Setzt die aktuelle Karte auf dem Tisch
|
|
|
|
set cardOnDeck(card){ |
|
|
|
set cardOnDeck(card) { |
|
|
|
this._cardOnDeck = card; |
|
|
|
} |
|
|
|
|
|
|
|
//Gibt den Index des aktuellen Spielers im players Array zurück
|
|
|
|
get currentPlayer(){ |
|
|
|
get currentPlayer() { |
|
|
|
return this._currentPlayer; |
|
|
|
} |
|
|
|
|
|
|
|
//Gibt das Objekt des aktuellen Spielers zurück
|
|
|
|
get currentPlayerInstanz(){ |
|
|
|
get currentPlayerInstanz() { |
|
|
|
return this._players[this.currentPlayer]; |
|
|
|
} |
|
|
|
|
|
|
|
set currentPlayer(player){ |
|
|
|
set currentPlayer(player) { |
|
|
|
this._currentPlayer = player |
|
|
|
} |
|
|
|
|
|
|
|
//Gibt die aktuelle Ricktung zurück 1 = normal 2 = Invertiert
|
|
|
|
get direction(){ |
|
|
|
get direction() { |
|
|
|
return this._direction; |
|
|
|
} |
|
|
|
|
|
|
|
set direction(direction){ |
|
|
|
set direction(direction) { |
|
|
|
this._direction = direction; |
|
|
|
} |
|
|
|
|
|
|
|
get rules(){ |
|
|
|
get rules() { |
|
|
|
return this._rules; |
|
|
|
} |
|
|
|
|
|
|
|
get style() { |
|
|
|
return this._style; |
|
|
|
} |
|
|
|
|
|
|
|
} |