|
|
@ -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,6 +23,9 @@ 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
|
|
|
@ -40,31 +44,23 @@ export default class Game { |
|
|
|
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 ('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 (!(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; |
|
|
|
} |
|
|
@ -78,6 +74,10 @@ export default class Game { |
|
|
|
//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
|
|
|
@ -134,6 +134,7 @@ export default class Game { |
|
|
|
|
|
|
|
//Beendet den Zug des aktuellen Spielers und beginnt den Zug des nächsten Spielers
|
|
|
|
nextTurn() { |
|
|
|
let delay = 0; |
|
|
|
|
|
|
|
//Testet, ob Spiel Gewonnen
|
|
|
|
for (let i = 0; i < this.players.length; i++) { |
|
|
@ -144,15 +145,24 @@ export default class Game { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//Wenn Zug nicht der Erste vom ganzen Spiel
|
|
|
|
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; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//nächster Spieler wird gesetzt
|
|
|
|
this.currentPlayer = this.nextPlayer(); |
|
|
|
|
|
|
@ -161,6 +171,12 @@ 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
|
|
|
@ -172,10 +188,7 @@ export default class Game { |
|
|
|
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,18 +205,19 @@ 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; |
|
|
|
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
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
@ -258,4 +272,8 @@ export default class Game { |
|
|
|
return this._rules; |
|
|
|
} |
|
|
|
|
|
|
|
get style() { |
|
|
|
return this._style; |
|
|
|
} |
|
|
|
|
|
|
|
} |