Commit ef3e76f4 authored by Chingiz's avatar Chingiz 💻

Fixed showing style.

parent acc87532
...@@ -2,9 +2,11 @@ export class Card { ...@@ -2,9 +2,11 @@ export class Card {
get rank(): string { get rank(): string {
return this._rank; return this._rank;
} }
get suite(): string { get suite(): string {
return this._suite; return this._suite;
} }
private _rank: string; private _rank: string;
private _suite: string; private _suite: string;
...@@ -12,7 +14,8 @@ export class Card { ...@@ -12,7 +14,8 @@ export class Card {
this._rank = rank; this._rank = rank;
this._suite = suit; this._suite = suit;
} }
show(i:number){
return(` _____\n${i} | ${this._rank} ${this._suite} |\n -----`) show(i: number) {
return (` _____\n${i} | ${this._rank} ${this._suite} |\n -----`)
} }
} }
\ No newline at end of file
...@@ -7,7 +7,19 @@ export class Checker { ...@@ -7,7 +7,19 @@ export class Checker {
private _checkMethods: ({ private _checkMethods: ({
message: string, message: string,
check: (sameRanks: { [key: string]: number }) => boolean check: (sameRanks: { [key: string]: number }) => boolean
})[] = [{ message: "You've got Four of a Kind! 8 points", check: this.isFourOfKind }, {message: "You've got Full House! 7 points", check: this.isFullHouse}, {message: "You've got Street! 5 points", check: this.isStreet}, {message: "You've got Royal Three of a Kind! 4 points", check: this.isThreeOfKind}, {message: "You've got Two Pare! 3 points", check: this.isTwoPare}, {message: "You've got Pare! 2 points", check: this.isPare}] })[] = [{
message: "You've got Four of a Kind! 8 points",
check: this.isFourOfKind
}, {message: "You've got Full House! 7 points", check: this.isFullHouse}, {
message: "You've got Street! 5 points",
check: this.isStreet
}, {
message: "You've got Royal Three of a Kind! 4 points",
check: this.isThreeOfKind
}, {message: "You've got Two Pare! 3 points", check: this.isTwoPare}, {
message: "You've got Pare! 2 points",
check: this.isPare
}]
constructor(cards: Card[]) { constructor(cards: Card[]) {
for (const card of cards) { for (const card of cards) {
...@@ -18,7 +30,7 @@ export class Checker { ...@@ -18,7 +30,7 @@ export class Checker {
} }
sortSameRanks() { sortSameRanks() {
if (this._cards){ if (this._cards) {
this._sameRanks = this._cards.reduce((acc: { [key: string]: number }, card: { this._sameRanks = this._cards.reduce((acc: { [key: string]: number }, card: {
rank: string, rank: string,
suite: string suite: string
...@@ -32,7 +44,7 @@ export class Checker { ...@@ -32,7 +44,7 @@ export class Checker {
} }
sortSameSuites() { sortSameSuites() {
if (this._cards){ if (this._cards) {
this._sameSuites = this._cards.reduce((acc: { [key: string]: number }, card: { this._sameSuites = this._cards.reduce((acc: { [key: string]: number }, card: {
rank: string, rank: string,
suite: string suite: string
...@@ -40,7 +52,7 @@ export class Checker { ...@@ -40,7 +52,7 @@ export class Checker {
acc[card.suite] = (acc[card.suite]++ || 0) + 1; acc[card.suite] = (acc[card.suite]++ || 0) + 1;
return acc; return acc;
}, {}); }, {});
}else { } else {
console.log("NO cards!") console.log("NO cards!")
} }
} }
...@@ -146,15 +158,25 @@ export class Checker { ...@@ -146,15 +158,25 @@ export class Checker {
} }
checkCombination() { checkCombination() {
if (this.isRoyalFlush(this._sameRanks, this._sameSuites)) return console.log("You've got Royal Flash! 10 point!"); if (this.isRoyalFlush(this._sameRanks, this._sameSuites)) return this.congratMessage("You've got Royal Flash! 10 point!");
if (this.isFlush(this._sameSuites)) return console.log("You've got Flash! 6 point!"); if (this.isFlush(this._sameSuites)) return this.congratMessage("You've got Flash! 6 point!");
for (const combination of this._checkMethods) { for (const combination of this._checkMethods) {
if (combination.check(this._sameRanks)) { if (combination.check(this._sameRanks)) {
return console.log(combination.message);
return this.congratMessage(combination.message);
} }
} }
return console.log("You've got High Card! 1 point!") return this.congratMessage("You've got High Card! 1 point!");
} }
congratMessage(message: string) {
return console.log(`
==================================================
* *
* ${message}!!!
* *
===================================================
`)
}
} }
\ No newline at end of file
...@@ -5,7 +5,7 @@ export class Deck { ...@@ -5,7 +5,7 @@ export class Deck {
private RANKS: string[] = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"] private RANKS: string[] = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
private SUITS: string[] = ["♠", "♥", "♦", "♣"] private SUITS: string[] = ["♠", "♥", "♦", "♣"]
private _cards: Card[] = []; private _cards: Card[] = [];
private tookCardIndexes: number[] =[]; private tookCardIndexes: number[] = [];
constructor() { constructor() {
for (let i = 0; i < this.RANKS.length; i++) { for (let i = 0; i < this.RANKS.length; i++) {
...@@ -13,6 +13,7 @@ export class Deck { ...@@ -13,6 +13,7 @@ export class Deck {
this._cards.push(new Card(this.RANKS[i], this.SUITS[j])) this._cards.push(new Card(this.RANKS[i], this.SUITS[j]))
} }
} }
console.log("New deck has been created.")
} }
shuffleDeck() { shuffleDeck() {
...@@ -20,12 +21,15 @@ export class Deck { ...@@ -20,12 +21,15 @@ export class Deck {
const j = Math.floor(Math.random() * (i + 1)); const j = Math.floor(Math.random() * (i + 1));
[this._cards[i], this._cards[j]] = [this._cards[j], this._cards[i]]; [this._cards[i], this._cards[j]] = [this._cards[j], this._cards[i]];
} }
console.log("The deck has been shuffled!")
} }
takeACardFromDeck(){
takeACardFromDeck() {
const ranNum: number = getRandomNumber(0, this._cards.length + 1); const ranNum: number = getRandomNumber(0, this._cards.length + 1);
if(!this.tookCardIndexes.includes(ranNum)){ if (!this.tookCardIndexes.includes(ranNum)) {
return this._cards[ranNum]; return this._cards[ranNum];
this.tookCardIndexes.push(ranNum); this.tookCardIndexes.push(ranNum);
console.log(`You took ${this.tookCardIndexes.length} from the deck`)
} }
this.takeACardFromDeck(); this.takeACardFromDeck();
} }
......
import {Card} from "./Card"; import {Card} from "./Card";
export class Hand { export class Hand {
set holdingCards(value: Card[]) {
this._holdingCards = value;
}
get holdingCards(): Card[] { get holdingCards(): Card[] {
return this._holdingCards; return this._holdingCards;
} }
private _limit: number = 5; private _limit: number = 5;
private _holdingCards: Card[] = []; private _holdingCards: Card[] = [];
private _changeCounter: number = 5; private _changeCounter: number = 5;
...@@ -11,23 +16,26 @@ export class Hand { ...@@ -11,23 +16,26 @@ export class Hand {
receiveCard(card: Card) { receiveCard(card: Card) {
if (this._holdingCards.length < this._limit) { if (this._holdingCards.length < this._limit) {
this._holdingCards.push(card) this._holdingCards.push(card)
console.log(`You received ${this._holdingCards.length} cards.`)
} else { } else {
console.log("Hand is full!") console.log("Hand is full!")
} }
} }
showCards(){
for (let i = 0; i < this._holdingCards.length; i++){ showCards() {
console.log(this._holdingCards[i].show(i+1)) for (let i = 0; i < this._holdingCards.length; i++) {
console.log(this._holdingCards[i].show(i + 1))
console.log("==============") console.log("==============")
} }
} }
changeCard(newCard: Card, i: number){
changeCard(newCard: Card, i: number) {
if (i > 6 && i > 0) return console.log("Card number can not be more then 5 and less then 1") if (i > 6 && i > 0) return console.log("Card number can not be more then 5 and less then 1")
if (this._changeCounter){ if (this._changeCounter) {
this._holdingCards[i-1] = newCard; this._holdingCards[i - 1] = newCard;
console.log("Card has been changed!") console.log("Card has been changed!")
this._changeCounter-- this._changeCounter--
}else{ } else {
console.log("You've already changed 5 times.") console.log("You've already changed 5 times.")
} }
} }
......
import {Deck} from "./Modules/Deck"; import {Application} from "./Modules/Application";
import {Card} from "./Modules/Card";
const deck = new Deck();
deck.shuffleDeck();
let card: Card | undefined;
card = deck.takeACard()
console.log(card ? card.show() : card)
const game = new Application();
game.startGame()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment