diff options
author | Anthony Wang | 2021-05-06 20:22:13 -0500 |
---|---|---|
committer | Anthony Wang | 2021-05-06 20:22:13 -0500 |
commit | 3ae7abec050a18d54aa1e1f78a03a714b4ee4308 (patch) | |
tree | ce86d7f767630018f4f3ab8a31ce953ad6347612 | |
parent | a74f0a6e2867dca4c838f427be667466a22b28d7 (diff) |
Remove unnecessary stuff from core
-rw-r--r-- | core/src/canPlay.ts | 72 | ||||
-rw-r--r-- | core/src/cmpCard.ts | 8 | ||||
-rw-r--r-- | core/src/index.ts | 3 | ||||
-rw-r--r-- | core/src/realRank.ts | 5 |
4 files changed, 0 insertions, 88 deletions
diff --git a/core/src/canPlay.ts b/core/src/canPlay.ts deleted file mode 100644 index fa5e286..0000000 --- a/core/src/canPlay.ts +++ /dev/null @@ -1,72 +0,0 @@ -import Card from './Card'; -import cmpCard from './cmpCard'; -import realRank from './realRank'; - -class Hand { - size: number; - overrideSize: boolean; - matchFunc: (cards: Card[]) => Card | null; - constructor(size: number, overrideSize: boolean, matchFunc: (cards: Card[]) => Card | null) { - this.size = size; - this.overrideSize = overrideSize; - this.matchFunc = matchFunc; - } - match(cards: Card[]) { - return cards.length === this.size && this.matchFunc(cards); - } -} - -const chkFlush = (cards: Card[]) => cards.every((card: Card) => card.suit === cards[0].suit); -const chkStraight = (cards: Card[]) => { - let ok = true; - for (let i = 1; i < 5; ++i) - ok = ok && realRank(cards[0]) + i === realRank(cards[i]); - if (ok) return true; - // 3 4 5 6 2 - ok = cards[4].rank === 2; - for (let i = 0; i < 4; ++i) - ok = ok && cards[0].rank === 3 + i; - if (ok) return true; - return false; -}; - -const hands = [ - // Straight Flush - new Hand(5, true, (cards: Card[]) => chkFlush(cards) && chkStraight(cards) ? cards[4] : null), - // Four of a Kind - new Hand(5, true, (cards: Card[]) => - (cards[0].rank === cards[3].rank ? cards[3] : null) || - (cards[1].rank === cards[4].rank ? cards[4] : null) - ), - // Full House - new Hand(5, false, (cards: Card[]) => - (cards[0].rank === cards[2].rank && cards[3].rank === cards[4].rank ? cards[2] : null) || - (cards[0].rank === cards[1].rank && cards[2].rank === cards[4].rank ? cards[4] : null) - ), - // Straight - new Hand(5, false, (cards: Card[]) => chkStraight(cards) ? cards[4] : null), - // Flush - new Hand(5, false, (cards: Card[]) => chkFlush(cards) ? cards[4] : null), - // Pair - new Hand(2, false, (cards: Card[]) => cards[0].rank === cards[1].rank ? cards[1] : null), - // Single - new Hand(1, false, (cards: Card[]) => cards[0]) -]; - -const parseHand = (cards: Card[]) => { - for (let i = 0; i < hands.length; ++i) { - const card = hands[i].match(cards); - if (card) return {id: i, card}; - } - return null; -}; - -export default function canPlay(prev: Card[] | null, cur: Card[]) { - const curHand = parseHand(cur); - if (!curHand) return false; - if (!prev) return true; - const prevHand = parseHand(prev)!; - if (!hands[curHand.id].overrideSize && cur.length !== prev.length) return false; - if (curHand.id !== prevHand.id) return curHand.id < prevHand.id; - return cmpCard(prevHand.card, curHand.card) < 0; -}; diff --git a/core/src/cmpCard.ts b/core/src/cmpCard.ts deleted file mode 100644 index 56a74f2..0000000 --- a/core/src/cmpCard.ts +++ /dev/null @@ -1,8 +0,0 @@ -import Card from './Card'; -import realRank from './realRank'; - -export default function cmpCard(a: Card, b: Card) { - if (a.rank !== b.rank) - return realRank(a) - realRank(b); - return a.suit - b.suit; -}; diff --git a/core/src/index.ts b/core/src/index.ts index 962bde9..19a4f3c 100644 --- a/core/src/index.ts +++ b/core/src/index.ts @@ -1,5 +1,2 @@ -export {default as canPlay} from './canPlay'; export {default as Card} from './Card'; -export {default as cmpCard} from './cmpCard'; -export {default as realRank} from './realRank'; export {default as Suit} from './Suit'; diff --git a/core/src/realRank.ts b/core/src/realRank.ts deleted file mode 100644 index f9f588e..0000000 --- a/core/src/realRank.ts +++ /dev/null @@ -1,5 +0,0 @@ -import Card from './Card'; - -export default function realRank(card: Card) { - return (card.rank + 10) % 13; -} |