Commit 188100be authored by Chingiz's avatar Chingiz 💻

Fixed some bugs in Checker.ts

parent 1a7d199c
import {Card} from "./Card"; import {Card} from "./Card";
class Checker { export class Checker {
private _cards: { rank: string, suite: string }[] = []; private _cards: { rank: string, suite: string }[] = [];
private _sameRanks: { [key: string]: number } = {}; private _sameRanks: { [key: string]: number } = {};
private _sameSuites: { [key: string]: number } = {}; private _sameSuites: { [key: string]: number } = {};
private _checkMethods: ({ message: string, check: () => boolean })[] = [{message: "You've got Royal Flash! 10 points", check: this.isRoyalFlash}, {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 Flash! 6 points", check: this.isFlash}, {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} ] private _checkMethods: ({
message: string,
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}]
constructor(cards: Card[]) { constructor(cards: Card[]) {
for (const card of cards) { for (const card of cards) {
this._cards.push({rank: card.rank, suite: card.suite}) this._cards.push({rank: card.rank, suite: card.suite})
} }
this.sortSameRanks();
this.sortSameSuites();
} }
sortSameRanks() { sortSameRanks() {
this._sameRanks = this._cards.reduce((acc: { [key: string]: number }, card: { if (this._cards){
rank: string, this._sameRanks = this._cards.reduce((acc: { [key: string]: number }, card: {
suite: string rank: string,
}) => { suite: string
acc[card.rank] = acc[card.rank]++ || acc[card.rank] + 1; }) => {
return acc; acc[card.rank] = (acc[card.rank]++ || 0) + 1;
}, {}); return acc;
}, {});
} else {
console.log("NO cards!")
}
} }
sortSameSuites() { sortSameSuites() {
this._sameSuites = this._cards.reduce((acc: { [key: string]: number }, card: { if (this._cards){
rank: string, this._sameSuites = this._cards.reduce((acc: { [key: string]: number }, card: {
suite: string rank: string,
}) => { suite: string
acc[card.suite] = acc[card.suite]++ || acc[card.suite] + 1; }) => {
return acc; acc[card.suite] = (acc[card.suite]++ || 0) + 1;
}, {}); return acc;
}, {});
}else {
console.log("NO cards!")
}
} }
isPare() { isPare(sameRanks: { [key: string]: number }) {
const keysOfSameRanks = Object.keys(this._sameRanks); const keysOfSameRanks = Object.keys(sameRanks);
let check: boolean = false; let check: boolean = false;
for (const keysOfSameRank of keysOfSameRanks) { for (const keysOfSameRank of keysOfSameRanks) {
if (this._sameRanks[keysOfSameRank] >= 2) { if (sameRanks[keysOfSameRank] >= 2) {
check = true; check = true;
break; break;
} }
} }
return check; return check;
} }
isTwoPare() {
const keysOfSameRanks = Object.keys(this._sameRanks); isTwoPare(sameRanks: { [key: string]: number }) {
const keysOfSameRanks = Object.keys(sameRanks);
let countOfTwos: number = 0; let countOfTwos: number = 0;
if (keysOfSameRanks.length === 3){ if (keysOfSameRanks.length === 3) {
for (const keysOfSameRank of keysOfSameRanks) { for (const keysOfSameRank of keysOfSameRanks) {
if (this._sameRanks[keysOfSameRank] === 2) { if (sameRanks[keysOfSameRank] === 2) {
countOfTwos++ countOfTwos++
} }
} }
} }
return (countOfTwos === 2); return (countOfTwos === 2);
} }
isThreeOfKind() {
const keysOfSameRanks = Object.keys(this._sameRanks); isThreeOfKind(sameRanks: { [key: string]: number }) {
const keysOfSameRanks = Object.keys(sameRanks);
let check: boolean = false; let check: boolean = false;
if (keysOfSameRanks.length === 2) { if (keysOfSameRanks.length === 2) {
for (const keysOfSameRank of keysOfSameRanks) { for (const keysOfSameRank of keysOfSameRanks) {
...@@ -66,8 +83,9 @@ class Checker { ...@@ -66,8 +83,9 @@ class Checker {
} }
return check; return check;
} }
isFullHouse() {
const keysOfSameRanks = Object.keys(this._sameRanks); isFullHouse(sameRanks: { [key: string]: number }) {
const keysOfSameRanks = Object.keys(sameRanks);
let check: boolean = false; let check: boolean = false;
if (keysOfSameRanks.length === 2) { if (keysOfSameRanks.length === 2) {
for (const keysOfSameRank of keysOfSameRanks) { for (const keysOfSameRank of keysOfSameRanks) {
...@@ -79,56 +97,64 @@ class Checker { ...@@ -79,56 +97,64 @@ class Checker {
} }
return check; return check;
} }
isFourOfKind() {
const keysOfSameRanks = Object.keys(this._sameRanks); isFourOfKind(sameRanks: { [key: string]: number }) {
const keysOfSameRanks = Object.keys(sameRanks);
let check: boolean = false; let check: boolean = false;
for (const keysOfSameRank of keysOfSameRanks) { for (const keysOfSameRank of keysOfSameRanks) {
if (this._sameRanks[keysOfSameRank] === 4) { if (sameRanks[keysOfSameRank] === 4) {
check = true; check = true;
break; break;
} }
} }
return check; return check;
} }
isFlash(){
const keysOfSameSuites = Object.keys(this._sameSuites); isFlush(sameSuites: { [key: string]: number }) {
const keysOfSameSuites = Object.keys(sameSuites);
let check: boolean = false; let check: boolean = false;
for (const keysOfSameSuite of keysOfSameSuites) { for (const keysOfSameSuite of keysOfSameSuites) {
if (this._sameSuites[keysOfSameSuite] === 5){ if (this._sameSuites[keysOfSameSuite] === 5) {
check = true; check = true;
} }
} }
return check; return check;
} }
isStreet(){
const streetCombinations = ['23456', '34567', '45678', '56789', '678910', '78910J','8910JQ', '910JQK'] isStreet(sameRanks: { [key: string]: number }) {
const keysOfSameRanks = Object.keys(this._sameRanks).sort(); const streetCombinations = ['23456', '34567', '45678', '56789', '678910', '78910J', '8910JQ', '910JQK']
const keysOfSameRanks = Object.keys(sameRanks).sort();
let check: boolean = false; let check: boolean = false;
if (keysOfSameRanks.length === 5){ if (keysOfSameRanks.length === 5) {
if (streetCombinations.includes(keysOfSameRanks.join())){ if (streetCombinations.includes(keysOfSameRanks.join())) {
check = true; check = true;
} }
} }
return check; return check;
} }
isRoyalFlash(){
const keysOfSameRanks = Object.keys(this._sameRanks).sort(); isRoyalFlush(sameRanks: { [key: string]: number }, sameSuites: { [key: string]: number }) {
const keysOfSameSuites: string[] = Object.keys(this._sameSuites); const keysOfSameRanks: string[] = Object.keys(sameRanks).sort();
const keysOfSameSuites: string[] = Object.keys(sameSuites);
let check: boolean = false; let check: boolean = false;
if (keysOfSameRanks.length === 5){ if (keysOfSameRanks.length === 5) {
if (keysOfSameRanks.join() === "10JQKA" && keysOfSameSuites.length ===1){ if (keysOfSameRanks.join() === "10JQKA" && keysOfSameSuites.length === 1) {
check = true; check = true;
} }
} }
return check; return check;
} }
checkCombination(){ checkCombination() {
if (this.isRoyalFlush(this._sameRanks, this._sameSuites)) return console.log("You've got Royal Flash! 10 point!");
if (this.isFlush(this._sameSuites)) return console.log("You've got Flash! 6 point!");
for (const combination of this._checkMethods) { for (const combination of this._checkMethods) {
if (combination.check()){ if (combination.check(this._sameRanks)) {
return combination.message; return console.log(combination.message);
} }
} }
return console.log("You've got High Card! 1 point!")
} }
} }
\ No newline at end of file
...@@ -5,13 +5,12 @@ export class Deck { ...@@ -5,13 +5,12 @@ 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 tookedCardIndexes: number[] =[]; private tookCardIndexes: number[] =[];
constructor() { constructor() {
for (let i = 0; i < this.RANKS.length; i++) { for (let i = 0; i < this.RANKS.length; i++) {
for (let j = 0; j < this.SUITS.length; j++) { for (let j = 0; j < this.SUITS.length; j++) {
this._cards.push(new Card(this.RANKS[i], this.SUITS[j])) this._cards.push(new Card(this.RANKS[i], this.SUITS[j]))
console.log(this.RANKS[i], this.SUITS[j])
} }
} }
} }
...@@ -24,9 +23,9 @@ export class Deck { ...@@ -24,9 +23,9 @@ export class Deck {
} }
takeACardFromDeck(){ takeACardFromDeck(){
const ranNum: number = getRandomNumber(0, this._cards.length + 1); const ranNum: number = getRandomNumber(0, this._cards.length + 1);
if(!this.tookedCardIndexes.includes(ranNum)){ if(!this.tookCardIndexes.includes(ranNum)){
return this._cards[ranNum]; return this._cards[ranNum];
this.tookedCardIndexes.push(ranNum); this.tookCardIndexes.push(ranNum);
} }
this.takeACardFromDeck(); this.takeACardFromDeck();
} }
......
import {Card} from "./Card"; import {Card} from "./Card";
export class Hand { export class Hand {
get holdingCards(): Card[] {
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;
......
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