Skip to content

Commit c3156be

Browse files
committed
functional
1 parent 6d1874f commit c3156be

File tree

5 files changed

+105
-13
lines changed

5 files changed

+105
-13
lines changed

src/app/deal-page/main-area/main-area.component.html

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,25 @@
44
<h3 class="identity">Dealer ({{this.cardData.dealerScore}})</h3>
55
<div class="row flexBox">
66
<ng-container *ngFor="let dealerCard of this.cardData.dealerCards">
7-
<app-card [cardURL]="'assets/cards/' + dealerCard + '.png'"></app-card>
7+
<app-card [cardURL]="'./assets/cards/' + dealerCard + '.png'"></app-card>
88
</ng-container>
99
</div>
1010
<br>
1111
<h3 class="identity">You ({{this.cardData.userScore}})</h3>
1212
<div class="row flexBox">
1313
<ng-container *ngFor="let userCard of this.cardData.userCards">
14-
<app-card [cardURL]="'assets/cards/' + userCard + '.png'"></app-card>
14+
<app-card [cardURL]="'./assets/cards/' + userCard + '.png'"></app-card>
1515
</ng-container>
1616
</div>
1717
<br>
1818
<br>
1919
<div class="row flexBox">
20-
<button (click)="this.hitCard()" nz-button nzType="default">Hit</button>
21-
<button (click)="this.stayCard()" nz-button nzType="default">Stay</button>
22-
<button nz-button nzType="default">Reset</button>
20+
<button [disabled]="this.cardData.gameResult !== ''" (click)="hit()" nz-button nzType="default">Hit</button>
21+
<button [disabled]="this.cardData.gameResult !== ''" (click)=stay() nz-button nzType="default">Stay</button>
22+
<button [disabled]="this.cardData.gameResult === ''" (click)=reset() nz-button nzType="default">Reset</button>
23+
</div>
24+
<br>
25+
<div class="flexBox">
26+
{{this.cardData.gameResult}}
2327
</div>
2428
</div>

src/app/deal-page/main-area/main-area.component.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,40 @@ export class MainAreaComponent implements OnInit {
1313
ngOnInit() {
1414
}
1515

16+
hit(){
17+
this.cardData.getRandomCard('user');
18+
if(this.cardData.userScore > 21){
19+
this.cardData.gameResult = "Bust";
20+
//this.cardData.reset();
21+
}
22+
}
23+
24+
stay(){
25+
while (this.cardData.dealerScore <= 17){
26+
this.cardData.getRandomCard('dealer');
27+
}
28+
if(this.cardData.dealerScore < 22 && this.cardData.dealerScore > this.cardData.userScore){
29+
this.cardData.gameResult = "You lose!";
30+
31+
}
32+
else if(this.cardData.dealerScore < 22 && this.cardData.dealerScore < this.cardData.userScore){
33+
this.cardData.gameResult = "You win!";
34+
this.cardData.wallet += this.cardData.bet * 2;
35+
}
36+
else if(this.cardData.dealerScore < 22 && this.cardData.dealerScore === this.cardData.userScore){
37+
this.cardData.gameResult = "Draw!";
38+
this.cardData.wallet += this.cardData.bet;
39+
}
40+
else if(this.cardData.dealerScore > 21){
41+
this.cardData.gameResult = "You win!";
42+
this.cardData.wallet += this.cardData.bet * 2;
43+
}
44+
45+
46+
}
47+
48+
reset(){
49+
this.cardData.reset();
50+
}
51+
1652
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<div class="row flexBox">
22
<div class="col-4">
33
<h6 class="wallet">Wallet</h6>
4-
<span [textContent]="1000 | currency" class="currAmountSpan"></span>
4+
<span [textContent]="this.cardData.wallet | currency" class="currAmountSpan"></span>
55
</div>
66
<div class="col-4">
77
<h6 class="wallet">Current Bet</h6>
8-
<span [textContent]="10 | currency" class="currAmountSpan"></span>
8+
<span [textContent]="this.cardData.bet | currency" class="currAmountSpan"></span>
99
</div>
1010
<div class="col-4">
11-
<button (click)="this.placeBet()" nz-button nzGhost nzType="default">Place Bet</button>
11+
<button [disabled]="this.cardData.gameActive" (click)="this.placeBet()" nz-button nzGhost nzType="default">Place Bet</button>
1212
</div>
1313
</div>

src/app/deal-page/wallet/wallet.component.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,19 @@ export class WalletComponent implements OnInit {
1414
}
1515

1616
placeBet(){
17-
18-
this.cardData.userCards.push(this.cardData.availableCards.splice(Math.floor(Math.random()*this.cardData.availableCards.length),1));
19-
this.cardData.dealerCards.push(this.cardData.availableCards.splice(Math.floor(Math.random()*this.cardData.availableCards.length),1));
20-
this.cardData.userCards.push(this.cardData.availableCards.splice(Math.floor(Math.random()*this.cardData.availableCards.length),1));
2117
this.cardData.gameActive = true;
18+
this.cardData.getRandomCard('user');
19+
this.cardData.getRandomCard('dealer');
20+
this.cardData.getRandomCard('user');
21+
22+
this.cardData.wallet -= this.cardData.bet;
23+
if(this.cardData.userScore > 21){
24+
this.cardData.gameResult = "Bust";
25+
26+
this.cardData.reset();
27+
}
2228
}
2329

30+
31+
2432
}

src/app/services/card-data.service.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ export class CardDataService {
2525
userScore = 0;
2626
dealerScore = 0;
2727
availableCards= [];
28+
gameResult = "";
29+
wallet = 1000;
30+
bet = 10;
2831

2932
reset(){
3033
this.availableCards = this.allCards.slice();
@@ -33,10 +36,51 @@ export class CardDataService {
3336
this.dealerCards = [];
3437
this.userScore = 0;
3538
this.dealerScore = 0;
36-
this.availableCards= [];
39+
this.gameResult = "";
3740
}
3841

3942
constructor() {
4043
this.availableCards = this.allCards.slice();
4144
}
45+
46+
getRandomCard(identity){
47+
let randomCard = this.availableCards.splice(Math.floor(Math.random()*this.availableCards.length),1)[0];
48+
let cardScore:number = this.getCardScore(randomCard, identity);
49+
if(identity === 'dealer'){
50+
this.dealerCards.push(randomCard);
51+
this.dealerScore += cardScore;
52+
}
53+
else{
54+
this.userCards.push(randomCard);
55+
this.userScore += cardScore;
56+
}
57+
58+
}
59+
60+
getCardScore(cardID, identity){
61+
let score = 0;
62+
let cardValue = 0;
63+
if(identity === 'dealer'){
64+
score = this.dealerScore;
65+
}
66+
else{
67+
score = this.userScore;
68+
}
69+
let royalCards = ['JC', 'JD', 'JH', 'JS',
70+
'QC', 'QD', 'QH', 'QS',
71+
'KC', 'KD', 'KH', 'KS',
72+
'AC', 'AD', 'AH', 'AS'];
73+
if(royalCards.includes(cardID)){
74+
cardValue = 10;
75+
if(score + cardValue > 21 && ['AC', 'AD', 'AH', 'AS'].includes(cardID)){
76+
cardValue = 1;
77+
}
78+
}
79+
else{
80+
console.log(cardID);
81+
cardValue = parseInt(cardID.substring(0, cardID.length-1));
82+
}
83+
//score = score + cardValue;
84+
return cardValue;
85+
}
4286
}

0 commit comments

Comments
 (0)