feat: add game state enum and refactor game components
This commit is contained in:
parent
349e4ce1ec
commit
4b569157aa
10 changed files with 105 additions and 115 deletions
|
@ -0,0 +1,74 @@
|
|||
import { Injectable } from '@angular/core';
|
||||
import { Card } from '../models/blackjack.model';
|
||||
import { GameState } from '../../../../enum/gameState';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class GameControlsService {
|
||||
calculateHandValue(cards: Card[]): number {
|
||||
let sum = 0;
|
||||
let aceCount = 0;
|
||||
|
||||
const rankValues: Record<string, number> = {
|
||||
TWO: 2,
|
||||
THREE: 3,
|
||||
FOUR: 4,
|
||||
FIVE: 5,
|
||||
SIX: 6,
|
||||
SEVEN: 7,
|
||||
EIGHT: 8,
|
||||
NINE: 9,
|
||||
TEN: 10,
|
||||
JACK: 10,
|
||||
QUEEN: 10,
|
||||
KING: 10,
|
||||
ACE: 11,
|
||||
};
|
||||
|
||||
for (const card of cards) {
|
||||
if (!card.hidden) {
|
||||
const value = rankValues[card.rank] || 0;
|
||||
sum += value;
|
||||
if (card.rank === 'ACE') {
|
||||
aceCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
while (sum > 21 && aceCount > 0) {
|
||||
sum -= 10;
|
||||
aceCount--;
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
getStatusText(state: GameState): string {
|
||||
switch (state) {
|
||||
case GameState.IN_PROGRESS:
|
||||
return 'Spiel läuft';
|
||||
case GameState.PLAYER_WON:
|
||||
return 'Gewonnen!';
|
||||
case GameState.PLAYER_LOST:
|
||||
return 'Verloren!';
|
||||
case GameState.DRAW:
|
||||
return 'Unentschieden!';
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
||||
getStatusClass(state: GameState): string {
|
||||
switch (state) {
|
||||
case GameState.PLAYER_WON:
|
||||
return 'text-emerald';
|
||||
case GameState.PLAYER_LOST:
|
||||
return 'text-accent-red';
|
||||
case GameState.DRAW:
|
||||
return 'text-yellow-400';
|
||||
default:
|
||||
return 'text-white';
|
||||
}
|
||||
}
|
||||
}
|
Reference in a new issue