From 62ce6b7f3041f4671114d9dc716b1e62bbf7d3e1 Mon Sep 17 00:00:00 2001 From: Haysel Santiago Date: Mon, 26 Mar 2018 08:06:14 -0400 Subject: [PATCH 1/2] Stumbling Forward --- tsFiles/card.ts | 68 +++++++++++++++++++++++++++++++++++++++++++++++++ tsFiles/rank.ts | 15 +++++++++++ tsFiles/suit.ts | 6 +++++ 3 files changed, 89 insertions(+) create mode 100644 tsFiles/card.ts create mode 100644 tsFiles/rank.ts create mode 100644 tsFiles/suit.ts diff --git a/tsFiles/card.ts b/tsFiles/card.ts new file mode 100644 index 00000000..5aed10e4 --- /dev/null +++ b/tsFiles/card.ts @@ -0,0 +1,68 @@ +class Card { + suit: string; + value: number; + constructor(i: number){ + var s = "null"; + switch (Math.floor(i / 13)){ // 13 since each type contains 4 + case 0: + s = "Hearts"; break; + case 1: + s = "Diamonds"; break; + case 2: + s = "Spades"; break; + case 3: + s = "Clubs"; break; + } + this.suit = s; + this.value = Math.floor(i % 13); + } + val(): string{ + if (this.value < 9) + return (this.value + 2).toString(); + else + switch(this.value){ + case 9: + return "Jack"; + case 10: + return "Queen"; + case 11: + return "King"; + case 12: + return "Ace"; + } + } + valNum(): number{ + return (this.value < 9) ? this.value + 2 : 10; + } +} + +class Deck { + data: Card[] = []; + currentCard: number = 0; + + constructor(){ + for (var i = 0; i < 52; i++){ + this.data.push(new Card(i)); + } + this.shuffle(); + } + printAll(){ + this.data.forEach(function(c){ + console.log(c.suit + " " + c.value); + }); + } + + shuffle(){ + for(var j, x, i = this.data.length; + i; + j = Math.floor(Math.random() * i), + x = this.data[--i], + this.data[i] = this.data[j], + this.data[j] = x); + this.currentCard = 0; + } + + deal(): Card{ + return this.data[this.currentCard++]; + } +} \ No newline at end of file diff --git a/tsFiles/rank.ts b/tsFiles/rank.ts new file mode 100644 index 00000000..82a16e84 --- /dev/null +++ b/tsFiles/rank.ts @@ -0,0 +1,15 @@ +enum Rank { + ACE, + TWO, + THREE, + FOUR, + FIVE, + SIX, + SEVEN, + EIGHT, + NINE, + TEN, + JACK, + QUEEN, + KING +} \ No newline at end of file diff --git a/tsFiles/suit.ts b/tsFiles/suit.ts new file mode 100644 index 00000000..9363522d --- /dev/null +++ b/tsFiles/suit.ts @@ -0,0 +1,6 @@ +enum Suit { + SPADE, + HEART, + CLUB, + DIAMOND +} \ No newline at end of file From 1ef80d74cc11be664abd18598c6974b15b282fc6 Mon Sep 17 00:00:00 2001 From: Haysel Santiago Date: Mon, 26 Mar 2018 08:10:07 -0400 Subject: [PATCH 2/2] js files --- tsFiles/card.js | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ tsFiles/rank.js | 16 ++++++++++++ tsFiles/suit.js | 7 ++++++ 3 files changed, 90 insertions(+) create mode 100644 tsFiles/card.js create mode 100644 tsFiles/rank.js create mode 100644 tsFiles/suit.js diff --git a/tsFiles/card.js b/tsFiles/card.js new file mode 100644 index 00000000..e3a1efce --- /dev/null +++ b/tsFiles/card.js @@ -0,0 +1,67 @@ +var Card = /** @class */ (function () { + function Card(i) { + var s = "null"; + switch (Math.floor(i / 13)) { + case 0: + s = "Hearts"; + break; + case 1: + s = "Diamonds"; + break; + case 2: + s = "Spades"; + break; + case 3: + s = "Clubs"; + break; + } + this.suit = s; + this.value = Math.floor(i % 13); + } + Card.prototype.val = function () { + if (this.value < 9) + return (this.value + 2).toString(); + else + switch (this.value) { + case 9: + return "Jack"; + case 10: + return "Queen"; + case 11: + return "King"; + case 12: + return "Ace"; + } + }; + Card.prototype.valNum = function () { + return (this.value < 9) ? this.value + 2 : 10; + }; + return Card; +}()); +var Deck = /** @class */ (function () { + function Deck() { + this.data = []; + this.currentCard = 0; + for (var i = 0; i < 52; i++) { + this.data.push(new Card(i)); + } + this.shuffle(); + } + Deck.prototype.printAll = function () { + this.data.forEach(function (c) { + console.log(c.suit + " " + c.value); + }); + }; + Deck.prototype.shuffle = function () { + for (var j, x, i = this.data.length; i; j = Math.floor(Math.random() * i), + x = this.data[--i], + this.data[i] = this.data[j], + this.data[j] = x) + ; + this.currentCard = 0; + }; + Deck.prototype.deal = function () { + return this.data[this.currentCard++]; + }; + return Deck; +}()); diff --git a/tsFiles/rank.js b/tsFiles/rank.js new file mode 100644 index 00000000..55a2c906 --- /dev/null +++ b/tsFiles/rank.js @@ -0,0 +1,16 @@ +var Rank; +(function (Rank) { + Rank[Rank["ACE"] = 0] = "ACE"; + Rank[Rank["TWO"] = 1] = "TWO"; + Rank[Rank["THREE"] = 2] = "THREE"; + Rank[Rank["FOUR"] = 3] = "FOUR"; + Rank[Rank["FIVE"] = 4] = "FIVE"; + Rank[Rank["SIX"] = 5] = "SIX"; + Rank[Rank["SEVEN"] = 6] = "SEVEN"; + Rank[Rank["EIGHT"] = 7] = "EIGHT"; + Rank[Rank["NINE"] = 8] = "NINE"; + Rank[Rank["TEN"] = 9] = "TEN"; + Rank[Rank["JACK"] = 10] = "JACK"; + Rank[Rank["QUEEN"] = 11] = "QUEEN"; + Rank[Rank["KING"] = 12] = "KING"; +})(Rank || (Rank = {})); diff --git a/tsFiles/suit.js b/tsFiles/suit.js new file mode 100644 index 00000000..40de089d --- /dev/null +++ b/tsFiles/suit.js @@ -0,0 +1,7 @@ +var Suit; +(function (Suit) { + Suit[Suit["SPADE"] = 0] = "SPADE"; + Suit[Suit["HEART"] = 1] = "HEART"; + Suit[Suit["CLUB"] = 2] = "CLUB"; + Suit[Suit["DIAMOND"] = 3] = "DIAMOND"; +})(Suit || (Suit = {}));