Welcome to the Casino!
+Would you like to play? Press Start Game
+diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 00000000..2698db34 Binary files /dev/null and b/.DS_Store differ diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 5309019f..00000000 --- a/.gitignore +++ /dev/null @@ -1,37 +0,0 @@ -# Compiled source # -################### -*.com -*.class -*.dll -*.exe -*.o -*.so - -# Packages # -############ -# it's better to unpack these files and commit the raw source -# git has its own built in compression methods -*.7z -*.dmg -*.gz -*.iso -*.jar -*.rar -*.tar -*.zip - -# Logs and databases # -###################### -*.log -*.sql -*.sqlite - -# OS generated files # -###################### -.DS_Store -.DS_Store? -._* -.Spotlight-V100 -.Trashes -ehthumbs.db -Thumbs.db \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..494bc33e --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "chrome", + "request": "launch", + "name": "Launch Chrome", + "url": "http://localhost:8080", + "webRoot": "${workspaceFolder}" + }, + { + "type": "node", + "request": "launch", + "name": "Launch Program", + "program": "${file}", + "outFiles": [ + "${workspaceFolder}/**/*.js" + ] + } + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 00000000..fe88541a --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,19 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "2.0.0", + "tasks": [ + { + "type": "typescript", + "tsconfig": "tsconfig.json", + "option": "watch", + "problemMatcher": [ + "$tsc-watch" + ], + "group": { + "kind": "build", + "isDefault": true + } + } + ] +} \ No newline at end of file diff --git a/BlackJack.js b/BlackJack.js new file mode 100644 index 00000000..22cc8412 --- /dev/null +++ b/BlackJack.js @@ -0,0 +1,159 @@ +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; + return function (d, b) { + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +Object.defineProperty(exports, "__esModule", { value: true }); +var player_1 = require("./player"); +var CardGame_1 = require("./CardGame"); +var webElement = document.getElementById("display"); +var BlackJack = /** @class */ (function (_super) { + __extends(BlackJack, _super); + function BlackJack() { + return _super.call(this) || this; + } + BlackJack.prototype.turnPrompt = function () { + var answer = "yes"; + switch (answer) { + case 'Hit': + this.hit(); + break; + case 'Stay': + this.stay(); + break; + } + }; + BlackJack.prototype.roundPrompt = function () { + var game = this; + var answer = "Yes"; + switch (answer) { + case 'Yes': + game.newHand(); + game.start(); + break; + case 'No': + console.log('Peace out'); + break; + } + }; + BlackJack.prototype.start = function () { + this.currentPlayer = 1; + this.addNewPlayer("PLAYER_1"); + this.dealCards(2); + this.gameStartState(); + this.evaluateState(); + }; + BlackJack.prototype.quit = function () { + console.log('Goodbye!'); + }; + BlackJack.prototype.hit = function () { + this.draw(); + this.printLastDraw(); + // console.log(`Your hand value is now: ${this.players[this.currentPlayer].handScore}`); + webElement.innerText += "Your hand value is now: " + this.players[this.currentPlayer].handScore; + this.evaluateState(); + }; + BlackJack.prototype.stay = function () { + this.incrementTurn(); + this.evaluateState(); + }; + // fold() { + // console.log("You folded. Better luck next time!"); + // this.players[this.currentPlayer].hand = []; + // this.players[this.currentPlayer].handScore = this.calculateHandValue(this.players[this.currentPlayer].hand); + // this.evaluateState(); + // } + BlackJack.prototype.draw = function () { + _super.prototype.draw.call(this); + this.players[this.currentPlayer].handScore = this.calculateHandScores(this.players[this.currentPlayer].hand); + }; + BlackJack.prototype.evaluateState = function () { + var winner = new player_1.Player("???"), draw = false; + // If turns have gone back around to the dealer + if (this.currentPlayer === 0) { + // The dealer takes his actions + //console.log(`The dealer has ${this.players[0].handToString()}`); + webElement.innerText += "The dealer has " + this.players[0].handToString(); + while (this.players[0].handScore < 16) { + this.draw(); + this.printLastDraw(); + if (this.players[0].handScore > 21) { + console.log('The Dealer busted!'); + webElement.innerText += 'The Dealer busted!'; + this.players[0].handScore = 0; + break; + } + } + // Then check for a winner + this.players.forEach(function (player) { + if (player.handScore > winner.handScore) { + winner = player; + draw = false; + } + else if (player.handScore === winner.handScore) { + draw = true; + } + }); + if (draw) { + console.log("This round was a draw."); + } + else { + console.log(winner.name + " won the round with " + winner.handScore + "."); + } + this.roundPrompt(); + } + else { + if (this.players[this.currentPlayer].handScore === 21) { + console.log('Blackjack!'); + this.currentPlayer++; + this.roundPrompt(); + } + else if (this.players[this.currentPlayer].handScore > 21) { + console.log('Busted! You lost'); + this.players[this.currentPlayer].handScore = 0; + this.currentPlayer++; + this.roundPrompt(); + } + else { + this.turnPrompt(); + } + } + }; + BlackJack.prototype.calculateHandScores = function (cards) { + var value = 0; + cards.forEach(function (card) { + value += card.value; + if (card.rank == 'A' && value > 21) { + value -= 10; + } + }); + return value; + }; + BlackJack.prototype.gameStartState = function () { + this.printDealerState(); + this.printPlayerState(1); + }; + BlackJack.prototype.printDealerState = function () { + //console.log(`The dealer has ${this.players[0].hand[0].cardToString()}`); + webElement.innerText += "The dealer has " + this.players[0].hand[0].cardToString(); + }; + BlackJack.prototype.printPlayerState = function (playerIndex) { + console.log("Your hand is: " + this.players[playerIndex].handToString()); + console.log("Your hand value is now: " + this.players[playerIndex].handScore); + webElement.innerText += "Your hand is: " + this.players[playerIndex].handToString(); + webElement.innerText += "Your hand value is now: " + this.players[playerIndex].handScore; + }; + BlackJack.prototype.printLastDraw = function () { + console.log(this.players[this.currentPlayer].name + " drew " + this.players[this.currentPlayer].hand[this.players[this.currentPlayer].hand.length - 1]); + }; + return BlackJack; +}(CardGame_1.CardGame)); +exports.BlackJack = BlackJack; +var jack = new BlackJack(); +jack.start(); diff --git a/BlackJack.ts b/BlackJack.ts new file mode 100644 index 00000000..f0a9747d --- /dev/null +++ b/BlackJack.ts @@ -0,0 +1,162 @@ +import { Player } from "./player"; +import { Deck } from "./deck"; +import { CardGame } from "./CardGame"; +import { Dealer } from "./dealer"; +import { Card } from "./card"; + +var webElement = document.getElementById("display"); + +export class BlackJack extends CardGame{ + + constructor(){ + super(); + + } + turnPrompt() { + var answer: string = "yes"; + switch(answer){ + case 'Hit': + this.hit(); + break; + case 'Stay': + this.stay(); + break; + } + + } + roundPrompt() { + var game = this; + var answer: string = "Yes"; + + switch(answer){ + case 'Yes': + game.newHand(); + game.start(); + break; + case 'No': + console.log('Peace out'); + break; + } + + } + start() { + this.currentPlayer = 1; + this.addNewPlayer("PLAYER_1"); + this.dealCards(2); + this.gameStartState(); + this.evaluateState(); + + } + + quit() { + console.log('Goodbye!'); + } + + hit() { + this.draw(); + this.printLastDraw(); + // console.log(`Your hand value is now: ${this.players[this.currentPlayer].handScore}`); + webElement.innerText += `Your hand value is now: ${this.players[this.currentPlayer].handScore}`; + this.evaluateState(); + + } + + stay() { + this.incrementTurn(); + this.evaluateState(); + } + + // fold() { + // console.log("You folded. Better luck next time!"); + // this.players[this.currentPlayer].hand = []; + // this.players[this.currentPlayer].handScore = this.calculateHandValue(this.players[this.currentPlayer].hand); + // this.evaluateState(); + // } + + draw() { + super.draw(); + this.players[this.currentPlayer].handScore = this.calculateHandScores(this.players[this.currentPlayer].hand); + } + + + evaluateState() { + var winner = new Player("???"), draw = false; + // If turns have gone back around to the dealer + if (this.currentPlayer === 0) { + // The dealer takes his actions + //console.log(`The dealer has ${this.players[0].handToString()}`); + webElement.innerText += `The dealer has ${this.players[0].handToString()}`; + while (this.players[0].handScore < 16) { + this.draw(); + this.printLastDraw(); + if(this.players[0].handScore > 21) { + console.log('The Dealer busted!'); + webElement.innerText += 'The Dealer busted!'; + this.players[0].handScore = 0; + break; + } + } + // Then check for a winner + this.players.forEach(function(player){ + if (player.handScore > winner.handScore) { + winner = player; + draw = false; + } + else if(player.handScore === winner.handScore) { + draw = true; + } + }); + if(draw) { + console.log(`This round was a draw.`); + } + else { + console.log(`${winner.name} won the round with ${winner.handScore}.`); + } + this.roundPrompt(); + } else { + if (this.players[this.currentPlayer].handScore === 21) { + console.log('Blackjack!'); + this.currentPlayer++; + this.roundPrompt(); + } + else if (this.players[this.currentPlayer].handScore > 21) { + console.log('Busted! You lost'); + this.players[this.currentPlayer].handScore = 0; + this.currentPlayer++; + this.roundPrompt(); + } else { + this.turnPrompt(); + } + } + } + calculateHandScores(cards: Card[]): number { + var value = 0; + cards.forEach(function(card){ + value += card.value; + if(card.rank == 'A' && value > 21) { + value -= 10; + } + }); + return value; + } + gameStartState() { + this.printDealerState(); + this.printPlayerState(1); + } + printDealerState() { + //console.log(`The dealer has ${this.players[0].hand[0].cardToString()}`); + webElement.innerText += `The dealer has ${this.players[0].hand[0].cardToString()}`; + } + printPlayerState(playerIndex:number) { + console.log("Your hand is: " + this.players[playerIndex].handToString()); + console.log(`Your hand value is now: ${this.players[playerIndex].handScore}`); + webElement.innerText += "Your hand is: " + this.players[playerIndex].handToString(); + webElement.innerText += `Your hand value is now: ${this.players[playerIndex].handScore}`; + } + printLastDraw() { + console.log(`${this.players[this.currentPlayer].name} drew ${this.players[this.currentPlayer].hand[this.players[this.currentPlayer].hand.length-1]}`); + } + +} +var jack = new BlackJack(); +jack.start(); diff --git a/CardGame.js b/CardGame.js new file mode 100644 index 00000000..7013d66b --- /dev/null +++ b/CardGame.js @@ -0,0 +1,50 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +var player_1 = require("./player"); +var deck_1 = require("./deck"); +var dealer_1 = require("./dealer"); +var CardGame = /** @class */ (function () { + function CardGame() { + this.players = []; + this.currentPlayer = 1; + this.players.push(new dealer_1.Dealer("Dealer")); + this.deck = new deck_1.Deck(); + } + CardGame.prototype.draw = function () { + var card = this.deck.draw(); + this.players[this.currentPlayer].hand.push(card); + }; + CardGame.prototype.incrementTurn = function () { + this.currentPlayer++; + if (this.currentPlayer === this.players.length) { + this.currentPlayer = 0; + } + }; + CardGame.prototype.dealCards = function (numOfCards) { + for (var i = 0; i < (numOfCards || 2); i++) { + for (var player = 0; player < this.players.length; player++) { + this.draw(); + this.incrementTurn(); + } + } + }; + CardGame.prototype.addNewPlayer = function (name) { + this.players.push(new player_1.Player((name || "Player " + (this.players.length + 1)))); + }; + CardGame.prototype.showHands = function () { + this.players.forEach(function (player) { + console.log(player.name + "'s hand:"); + player.hand.forEach(function (card) { + console.log(card.cardToString()); + }); + }); + }; + CardGame.prototype.newHand = function () { + for (var player = 0; player < this.players.length; player++) { + this.players[player].hand = []; + this.players[player].handScore = 0; + } + }; + return CardGame; +}()); +exports.CardGame = CardGame; diff --git a/CardGame.ts b/CardGame.ts new file mode 100644 index 00000000..791d68a5 --- /dev/null +++ b/CardGame.ts @@ -0,0 +1,52 @@ +import { Player } from "./player"; +import { Deck } from "./deck"; +import { Dealer } from "./dealer"; +import { Card } from "./card"; + +export class CardGame{ + players: Player[]; + currentPlayer: number; + deck: Deck; + + constructor(){ + this.players = []; + this.currentPlayer = 1; + this.players.push(new Dealer("Dealer")); + this.deck = new Deck(); + } + draw(){ + var card = this.deck.draw(); + this.players[this.currentPlayer].hand.push(card); + } + incrementTurn() { + this.currentPlayer++; + if (this.currentPlayer === this.players.length) { + this.currentPlayer = 0; + } + } + dealCards(numOfCards?: number){ + for (var i = 0; i < (numOfCards||2); i++){ + for (var player = 0; player < this.players.length; player++){ + this.draw(); + this.incrementTurn(); + } + } + } + addNewPlayer(name?: string){ + this.players.push(new Player((name|| `Player ${this.players.length+1}`))); + } + showHands(){ + this.players.forEach(function(player) { + console.log(`${player.name}'s hand:`); + player.hand.forEach(function(card) { + console.log(card.cardToString()); + }); + }); + } + newHand(){ + for (var player = 0; player < this.players.length; player++){ + this.players[player].hand = []; + this.players[player].handScore = 0; + } + } +} diff --git a/card.js b/card.js new file mode 100644 index 00000000..1d37bdd1 --- /dev/null +++ b/card.js @@ -0,0 +1,54 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +var Card = /** @class */ (function () { + function Card(rank, suit) { + this.rank = rank; + this.suit = suit; + switch (rank) { + case 'A': + this.value = 11; + break; + case 'K': + case 'Q': + case 'J': + this.value = 10; + break; + default: + this.value = parseInt(rank); + break; + } + } + Card.prototype.rankToString = function () { + switch (this.rank) { + case 'A': + return "Ace"; + case 'K': + return "King"; + case 'Q': + return "Queen"; + case 'J': + return "Jack"; + default: + return this.value.toString(); + } + }; + Card.prototype.suitToString = function () { + switch (this.suit) { + case '♥': + return "Hearts"; + case '♦': + return "Diamonds"; + case '♣': + return "Clubs"; + case '♠': + return "Spades"; + default: + return this.value.toString(); + } + }; + Card.prototype.cardToString = function () { + return this.rankToString() + " of " + this.suitToString(); + }; + return Card; +}()); +exports.Card = Card; diff --git a/card.ts b/card.ts new file mode 100644 index 00000000..483b15eb --- /dev/null +++ b/card.ts @@ -0,0 +1,54 @@ +export class Card{ + rank: string; + suit: string; + value: number; + + constructor(rank: string, suit: string){ + this.rank = rank; + this.suit = suit; + switch(rank){ + case 'A': + this.value = 11; + break; + case 'K': + case 'Q': + case 'J': + this.value = 10; + break; + default: + this.value = parseInt(rank); + break; + } + } + rankToString(): string { + switch (this.rank){ + case 'A': + return "Ace"; + case 'K': + return "King"; + case 'Q': + return "Queen"; + case 'J': + return "Jack"; + default: + return this.value.toString(); + } + } + suitToString(): string { + switch (this.suit){ + case '♥': + return "Hearts"; + case '♦': + return "Diamonds"; + case '♣': + return "Clubs"; + case '♠': + return "Spades"; + default: + return this.value.toString(); + } + } + cardToString(): string { + return `${this.rankToString()} of ${this.suitToString()}`; + } +} diff --git a/css/style.css b/css/style.css index cc704eef..fda87cec 100644 --- a/css/style.css +++ b/css/style.css @@ -113,7 +113,7 @@ article p { margin-bottom: 20px;} section {margin:10px;} h1 {font-weight: bold; margin-left: 10px;} #main { - + } #display { border: 1px #aaa solid; diff --git a/dealer.js b/dealer.js new file mode 100644 index 00000000..71d5580f --- /dev/null +++ b/dealer.js @@ -0,0 +1,21 @@ +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; + return function (d, b) { + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +Object.defineProperty(exports, "__esModule", { value: true }); +var player_1 = require("./player"); +var Dealer = /** @class */ (function (_super) { + __extends(Dealer, _super); + function Dealer(name) { + return _super.call(this, "Dealer") || this; + } + return Dealer; +}(player_1.Player)); +exports.Dealer = Dealer; diff --git a/dealer.ts b/dealer.ts new file mode 100644 index 00000000..21f1eeab --- /dev/null +++ b/dealer.ts @@ -0,0 +1,9 @@ +import { Player } from "./player"; + +export class Dealer extends Player{ + + constructor(name : string) { + super("Dealer"); + + } +} \ No newline at end of file diff --git a/deck.js b/deck.js new file mode 100644 index 00000000..0ba1da41 --- /dev/null +++ b/deck.js @@ -0,0 +1,38 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +var card_1 = require("./card"); +var Deck = /** @class */ (function () { + function Deck() { + this.cards = []; + this.currentCard = 0; + for (var i = 0; i < 13; i++) { + for (var suit = 0; suit < 4; suit++) { + this.cards.push(new card_1.Card(Deck.RANKS[i], Deck.SUITS[suit])); + } + } + this.shuffle(3); + } + Deck.prototype.shuffle = function (times) { + for (var i = 0; i < (times || 1); i++) { + this.cards.sort(function () { + return (0.5 - Math.random()); + }); + } + }; + Deck.prototype.printDeck = function () { + this.cards.forEach(function (card) { + console.log(card.cardToString()); + }); + }; + Deck.prototype.draw = function () { + if (this.currentCard === this.cards.length - 1) { + this.shuffle(); + this.currentCard = 0; + } + return this.cards[this.currentCard++]; + }; + Deck.SUITS = ['♥', '♦', '♠', '♣']; + Deck.RANKS = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']; + return Deck; +}()); +exports.Deck = Deck; diff --git a/deck.ts b/deck.ts new file mode 100644 index 00000000..7de66f12 --- /dev/null +++ b/deck.ts @@ -0,0 +1,38 @@ +import {Card} from "./card"; + +export class Deck{ + static SUITS = ['♥', '♦', '♠', '♣']; + static RANKS = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']; + + cards: Card[]; + currentCard: number; + + constructor(){ + this.cards = []; + this.currentCard = 0; + for (var i = 0; i < 13; i++){ + for (var suit = 0; suit < 4; suit++){ + this.cards.push(new Card(Deck.RANKS[i], Deck.SUITS[suit])); + } + } + this.shuffle(3); + } + shuffle(times?:number) { + for (var i = 0; i < (times||1); i++) { + this.cards.sort(function(){ + return (0.5 - Math.random()); }); + } + } + printDeck(){ + this.cards.forEach(function(card:Card){ + console.log(card.cardToString()); + }); + } + draw(): Card{ + if (this.currentCard === this.cards.length-1){ + this.shuffle(); + this.currentCard = 0; + } + return this.cards[this.currentCard++]; + } +} diff --git a/index.html b/index.html index d2c3c254..c2efae11 100644 --- a/index.html +++ b/index.html @@ -19,13 +19,26 @@
Welcome to the Casino!
+Would you like to play? Press Start Game
+