-
Notifications
You must be signed in to change notification settings - Fork 0
/
Games.ts
176 lines (150 loc) · 4.61 KB
/
Games.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import chalk from "./node_modules/chalk/source/index";
import { Hand } from "./Hand";
import { Deck } from "./Deck";
import { PlayingCard } from "./PlayingCard";
import { DealerHand } from "./blackjack";
import { BlackjackHand } from "./blackjack";
const prompt = require("prompt-sync")();
abstract class Game {
game: string | null = null;
playerWin = 0;
dealerWin = 0;
turn = 0;
playing = false;
start(): void {
/*
start()
Starts the selected game, based off of the class which extends this class.
*/
console.clear();
console.log("------------------------------");
console.log(`🃏 Welcome to ${chalk.green("Lechner Casino")} 🃏`);
console.log("------------------------------");
console.log(`You are playing: ${chalk.yellow(this.game)}\n`);
console.log(`Player ${this.playerWin} - Dealer ${this.dealerWin}`)
prompt("Press Enter to continue");
this.playing = true;
const { deck, dealer, player } = this.initObjects();
while (this.playing) {
this.initiateTurn(deck, dealer, player);
}
}
//Handles each turn and increments the turn count. Overridden by the class that extends
abstract initiateTurn(
deck: Deck,
dealer: DealerHand,
player: BlackjackHand
): void;
protected abstract initObjects(): any;
protected abstract update(obj?: any): void;
endGame(player_win?: boolean): void {
/*
endGame()
Ends the game, displays the winner and increments the win count for them.
*/
if (player_win === undefined) {
console.log(chalk.green("PUSH"));
}
if (player_win) {
console.log(`${chalk.bold("\nWinner:")} ${chalk.green("Player")}`);
this.playerWin++;
} else {
console.log(`${chalk.bold("\nWinner:")} ${chalk.green("Dealer")}`);
this.dealerWin++;
}
console.log("Thanks for playing!\n");
this.playing = false;
process.exit();
}
}
export class Blackjack extends Game {
game = "Blackjack";
initiateTurn(deck: Deck, dealer: DealerHand, player: BlackjackHand): void {
if (this.turn === 0) {
dealer.hit(deck);
dealer.hit(deck);
player.hit(deck);
player.hit(deck);
} else {
const input = this.hitOrStand();
if (input === "s") {
const finalVal =
player.getTotalHand()[1] <= 21
? Math.max(...player.getTotalHand())
: player.getTotalHand()[0];
let maxDealerHand =
dealer.getTotalHand()[1] <= 21
? Math.max(...dealer.getTotalHand())
: dealer.getTotalHand()[0];
console.log(`${player.name} stands at ${chalk.yellow(finalVal)}`);
while (maxDealerHand < 17) {
dealer.hit(deck);
maxDealerHand =
dealer.getTotalHand()[1] <= 21
? Math.max(...dealer.getTotalHand())
: dealer.getTotalHand()[0];
}
if (finalVal > maxDealerHand) {
this.endGame(true);
} else if (finalVal === maxDealerHand) {
this.endGame();
} else {
this.endGame(false);
}
} else {
player.hit(deck);
}
}
this.turn++;
}
protected hitOrStand() {
console.log("");
let response = prompt("Hit or Stand? (h/s): ");
console.log("");
switch (response) {
default:
console.log(chalk.red("Invalid response. Try again!"));
response = this.hitOrStand();
break;
case "h":
case "s":
}
return response;
}
protected checkValue(player: BlackjackHand): void {
/*
checkValue()
Takes in the player who just hit from the deck and their opponent as parameters.
Checks to see if the player hit a Blackjack or busted.
*/
if (player.getTotalHand()[0] === 21 || player.getTotalHand()[1] === 21) {
console.log(
`${player.name} got ${chalk.green(
this.turn === 0 ? "Blackjack!" : "21"
)}`
);
this.endGame(player.name === "Player" ? true : false);
} else
console.log(
`${player.name} showing ${chalk.yellow(player.getTotalHandString())}`
);
if (player.getTotalHand()[0] > 21) {
console.log(chalk.red(`${player.name} Bust!`));
this.endGame(player.name === "Dealer" ? true : false);
}
}
protected initObjects(): any {
this.turn = 0;
const deck = new Deck();
deck.shuffle();
console.log(chalk.gray("Shuffling deck..."));
const dealer = DealerHand.create();
const player = BlackjackHand.create();
dealer.addObserver(this);
player.addObserver(this);
return { deck, dealer, player };
}
protected update(player: BlackjackHand): void {
this.checkValue(player);
}
}