Compare commits
No commits in common. "dc7787707c8c1bc175573fb7a524cc0bcb6f5a31" and "59a0995c29ae748ab7352db49640a282cec66f7a" have entirely different histories.
dc7787707c
...
59a0995c29
4 changed files with 2 additions and 120 deletions
46
card.test.ts
46
card.test.ts
|
|
@ -1,46 +0,0 @@
|
||||||
import { describe, expect, test } from "bun:test"
|
|
||||||
import Card, { Index, Suit } from "./card"
|
|
||||||
|
|
||||||
describe('types', () => {
|
|
||||||
test('has expected indexes', () => {
|
|
||||||
[
|
|
||||||
Index.Joker,
|
|
||||||
Index.One,
|
|
||||||
Index.Two,
|
|
||||||
Index.Three,
|
|
||||||
Index.Four,
|
|
||||||
Index.Five,
|
|
||||||
Index.Six,
|
|
||||||
Index.Seven,
|
|
||||||
Index.Eight,
|
|
||||||
Index.Nine,
|
|
||||||
Index.Ten,
|
|
||||||
Index.Jack,
|
|
||||||
Index.Queen,
|
|
||||||
Index.King,
|
|
||||||
Index.Ace
|
|
||||||
].forEach((current, i) => expect(current).toBe(i))
|
|
||||||
})
|
|
||||||
|
|
||||||
test('has expected suits', () => {
|
|
||||||
[Suit.Diamond, Suit.Club, Suit.Heart, Suit.Spade]
|
|
||||||
.forEach((current, i) => expect(current).toBe(i))
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('Card', () => {
|
|
||||||
test('can read simple data from a Card', () => {
|
|
||||||
const card = new Card(Index.Five, Suit.Diamond)
|
|
||||||
|
|
||||||
expect(card.prettyIndex).toBe('Five')
|
|
||||||
expect(card.prettySuit).toBe('♦')
|
|
||||||
expect(card.pretty).toBe('Five of ♦')
|
|
||||||
})
|
|
||||||
|
|
||||||
test('can read decimal data from a Card', () => {
|
|
||||||
const card = new Card(Index.Five, Suit.Club)
|
|
||||||
|
|
||||||
expect(card.decimalIndex).toBe('101')
|
|
||||||
expect(card.decimalSuit).toBe('01')
|
|
||||||
})
|
|
||||||
})
|
|
||||||
14
card.ts
14
card.ts
|
|
@ -31,24 +31,14 @@ export default class Card {
|
||||||
this.suit = suit
|
this.suit = suit
|
||||||
}
|
}
|
||||||
|
|
||||||
// 000, 001, 010...
|
|
||||||
get decimalIndex() {
|
get decimalIndex() {
|
||||||
const base = 2
|
const base = 2
|
||||||
const digits = 3
|
return this.index.toString(base)
|
||||||
return this
|
|
||||||
.index
|
|
||||||
.toString(base)
|
|
||||||
.padStart(digits, '0')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 00, 01, 10, 11
|
|
||||||
get decimalSuit() {
|
get decimalSuit() {
|
||||||
const base = 2
|
const base = 2
|
||||||
const digits = 2
|
return this.suit.toString(base)
|
||||||
return this
|
|
||||||
.suit
|
|
||||||
.toString(base)
|
|
||||||
.padStart(digits, '0')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get pretty() {
|
get pretty() {
|
||||||
|
|
|
||||||
40
pile.test.ts
40
pile.test.ts
|
|
@ -1,40 +0,0 @@
|
||||||
import { expect, test, spyOn } from "bun:test"
|
|
||||||
import Pile from "./pile"
|
|
||||||
import Card, { Index, Suit } from "./card"
|
|
||||||
|
|
||||||
test('can assign Cards to a Pile', () => {
|
|
||||||
const fiveOfClubs = new Card(Index.Five, Suit.Club)
|
|
||||||
const aceOfSpades = new Card(Index.Ace, Suit.Spade)
|
|
||||||
const pile = new Pile([fiveOfClubs, aceOfSpades])
|
|
||||||
|
|
||||||
expect(pile.cards[0]).toBe(fiveOfClubs)
|
|
||||||
expect(pile.cards[1]).toBe(aceOfSpades)
|
|
||||||
})
|
|
||||||
|
|
||||||
test('can pretty print all Cards in a Pile', () => {
|
|
||||||
const fiveOfClubs = new Card(Index.Five, Suit.Club)
|
|
||||||
const aceOfSpades = new Card(Index.Ace, Suit.Spade)
|
|
||||||
const pile = new Pile([fiveOfClubs, aceOfSpades])
|
|
||||||
|
|
||||||
expect(pile.pretty).toBe(`Five of ♣\nAce of ♠`)
|
|
||||||
})
|
|
||||||
|
|
||||||
test('can shuffle a Pile', () => {
|
|
||||||
spyOn(global.Math, 'random').mockReturnValue(0.123);
|
|
||||||
|
|
||||||
const fiveOfClubs = new Card(Index.Five, Suit.Club)
|
|
||||||
const aceOfSpades = new Card(Index.Ace, Suit.Spade)
|
|
||||||
const queenOfHearts = new Card(Index.Queen, Suit.Heart)
|
|
||||||
const pile = new Pile([fiveOfClubs, aceOfSpades, queenOfHearts])
|
|
||||||
|
|
||||||
pile.shuffle()
|
|
||||||
|
|
||||||
expect(pile.cards.includes(fiveOfClubs)).toBe(true)
|
|
||||||
expect(pile.cards.includes(aceOfSpades)).toBe(true)
|
|
||||||
expect(pile.cards.includes(queenOfHearts)).toBe(true)
|
|
||||||
expect(pile.cards[0]).not.toBe(fiveOfClubs)
|
|
||||||
expect(pile.cards[1]).not.toBe(aceOfSpades)
|
|
||||||
expect(pile.cards[2]).not.toBe(queenOfHearts)
|
|
||||||
|
|
||||||
spyOn(global.Math, 'random').mockRestore();
|
|
||||||
}, { repeats: 9 })
|
|
||||||
|
|
@ -1,22 +0,0 @@
|
||||||
import { expect, test, spyOn } from "bun:test"
|
|
||||||
import { arrayOfLength, arrayShuffle } from './utils';
|
|
||||||
|
|
||||||
test('arrayOfLength', () => {
|
|
||||||
expect(arrayOfLength(3)).toHaveLength(3)
|
|
||||||
})
|
|
||||||
|
|
||||||
test('arrayShuffle', () => {
|
|
||||||
spyOn(global.Math, 'random').mockReturnValue(0.123);
|
|
||||||
|
|
||||||
const unshuffled = [1, 2, 3]
|
|
||||||
const shuffled = arrayShuffle(unshuffled)
|
|
||||||
|
|
||||||
expect(shuffled.includes(unshuffled[0])).toBe(true)
|
|
||||||
expect(shuffled.includes(unshuffled[1])).toBe(true)
|
|
||||||
expect(shuffled.includes(unshuffled[2])).toBe(true)
|
|
||||||
expect(shuffled[0]).not.toBe(unshuffled[0])
|
|
||||||
expect(shuffled[1]).not.toBe(unshuffled[1])
|
|
||||||
expect(shuffled[2]).not.toBe(unshuffled[2])
|
|
||||||
|
|
||||||
spyOn(global.Math, 'random').mockRestore();
|
|
||||||
}, { repeats: 9 })
|
|
||||||
Loading…
Add table
Reference in a new issue