Commit 1a7d199c authored by Chingiz's avatar Chingiz 💻

Checking class has been created!

parent be83115b
export class Card { export class Card {
get rank(): string {
return this._rank;
}
get suite(): string {
return this._suite;
}
private _rank: string; private _rank: string;
private _suite: string; private _suite: string;
......
import {Card} from "./Card";
class Checker {
private _cards: { rank: string, suite: string }[] = [];
private _sameRanks: { [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} ]
constructor(cards: Card[]) {
for (const card of cards) {
this._cards.push({rank: card.rank, suite: card.suite})
}
}
sortSameRanks() {
this._sameRanks = this._cards.reduce((acc: { [key: string]: number }, card: {
rank: string,
suite: string
}) => {
acc[card.rank] = acc[card.rank]++ || acc[card.rank] + 1;
return acc;
}, {});
}
sortSameSuites() {
this._sameSuites = this._cards.reduce((acc: { [key: string]: number }, card: {
rank: string,
suite: string
}) => {
acc[card.suite] = acc[card.suite]++ || acc[card.suite] + 1;
return acc;
}, {});
}
isPare() {
const keysOfSameRanks = Object.keys(this._sameRanks);
let check: boolean = false;
for (const keysOfSameRank of keysOfSameRanks) {
if (this._sameRanks[keysOfSameRank] >= 2) {
check = true;
break;
}
}
return check;
}
isTwoPare() {
const keysOfSameRanks = Object.keys(this._sameRanks);
let countOfTwos: number = 0;
if (keysOfSameRanks.length === 3){
for (const keysOfSameRank of keysOfSameRanks) {
if (this._sameRanks[keysOfSameRank] === 2) {
countOfTwos++
}
}
}
return (countOfTwos === 2);
}
isThreeOfKind() {
const keysOfSameRanks = Object.keys(this._sameRanks);
let check: boolean = false;
if (keysOfSameRanks.length === 2) {
for (const keysOfSameRank of keysOfSameRanks) {
if (this._sameRanks[keysOfSameRank] === 3) {
check = true;
break;
}
}
}
return check;
}
isFullHouse() {
const keysOfSameRanks = Object.keys(this._sameRanks);
let check: boolean = false;
if (keysOfSameRanks.length === 2) {
for (const keysOfSameRank of keysOfSameRanks) {
if (this._sameRanks[keysOfSameRank] === 2 || this._sameRanks[keysOfSameRank] === 3) {
check = true;
break;
}
}
}
return check;
}
isFourOfKind() {
const keysOfSameRanks = Object.keys(this._sameRanks);
let check: boolean = false;
for (const keysOfSameRank of keysOfSameRanks) {
if (this._sameRanks[keysOfSameRank] === 4) {
check = true;
break;
}
}
return check;
}
isFlash(){
const keysOfSameSuites = Object.keys(this._sameSuites);
let check: boolean = false;
for (const keysOfSameSuite of keysOfSameSuites) {
if (this._sameSuites[keysOfSameSuite] === 5){
check = true;
}
}
return check;
}
isStreet(){
const streetCombinations = ['23456', '34567', '45678', '56789', '678910', '78910J','8910JQ', '910JQK']
const keysOfSameRanks = Object.keys(this._sameRanks).sort();
let check: boolean = false;
if (keysOfSameRanks.length === 5){
if (streetCombinations.includes(keysOfSameRanks.join())){
check = true;
}
}
return check;
}
isRoyalFlash(){
const keysOfSameRanks = Object.keys(this._sameRanks).sort();
const keysOfSameSuites: string[] = Object.keys(this._sameSuites);
let check: boolean = false;
if (keysOfSameRanks.length === 5){
if (keysOfSameRanks.join() === "10JQKA" && keysOfSameSuites.length ===1){
check = true;
}
}
return check;
}
checkCombination(){
for (const combination of this._checkMethods) {
if (combination.check()){
return combination.message;
}
}
}
}
\ No newline at end of file
...@@ -8,9 +8,10 @@ export class Deck { ...@@ -8,9 +8,10 @@ export class Deck {
private tookedCardIndexes: number[] =[]; private tookedCardIndexes: number[] =[];
constructor() { constructor() {
for (let i = 0; i < this.SUITS.length; i++) { for (let i = 0; i < this.RANKS.length; i++) {
for (let j = 0; j < this.RANKS.length; j++) { for (let j = 0; j < this.SUITS.length; j++) {
this._cards.push(new Card(this.RANKS[j], this.SUITS[i])) this._cards.push(new Card(this.RANKS[i], this.SUITS[j]))
console.log(this.RANKS[i], this.SUITS[j])
} }
} }
} }
......
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