Skip to content

Instantly share code, notes, and snippets.

@elzup
Created September 25, 2019 07:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elzup/b4a06afc3c2d6693350a342c10a1b843 to your computer and use it in GitHub Desktop.
Save elzup/b4a06afc3c2d6693350a342c10a1b843 to your computer and use it in GitHub Desktop.
trump flowtype
// @flow
type Color = 'red' | 'black';
const RED = 'red';
const BLACK = 'black';
type Mark = {|name: string, color: Color|};
const SPADE: Mark = {name: 'S', color: BLACK};
const HEART: Mark = {name: 'H', color: RED};
const CLUB: Mark = {name: 'C', color: BLACK};
const DIAMOND: Mark = {name: 'D', color: RED};
type TrumpNumber = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13;
class Card {
number: TrumpNumber;
mark: Mark;
constructor(number: TrumpNumber, mark: Mark) {
this.number = number;
this.mark = mark;
}
color(): Color {
return this.mark.color;
}
toString(): string {
const converter = {'1': 'A', '11': 'J', '12': 'Q', '13': 'K'};
const numberStr: string = converter[this.number] || this.number.toString();
return `a Card(${this.mark.name}, ${numberStr})`;
}
isEqualMark(card: Card): boolean {
return this.mark === card.mark;
}
isLargerThan(card: Card): boolean {
if (this.number === 1 && card.number === 13) {
return true;
}
if (this.number === 13 && card.number === 1) {
return false;
}
return this.number > card.number;
}
}
const card1 = new Card(1, SPADE);
const card2 = new Card(2, DIAMOND);
const card3 = new Card(13, SPADE);
console.log(card1.toString());
console.log(card2.toString());
console.log(card3.toString());
console.log(card1.isEqualMark(card2));
console.log(card1.isEqualMark(card3));
console.log(card1.isLargerThan(card2));
console.log(card1.isLargerThan(card3));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment