Skip to content

Commit

Permalink
feat: 构造Furo与Mentsu与Tatsu时增加合法性检查
Browse files Browse the repository at this point in the history
  • Loading branch information
ssttkkl committed Feb 9, 2023
1 parent 692f6bb commit 3996141
Show file tree
Hide file tree
Showing 21 changed files with 48 additions and 908 deletions.
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
rootDir: "./src"
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc",
"test": "jest"
"test": "jest "
},
"devDependencies": {
"@types/jest": "^29.4.0",
Expand Down
65 changes: 0 additions & 65 deletions src/hora/coder.js

This file was deleted.

39 changes: 0 additions & 39 deletions src/hora/index.js

This file was deleted.

2 changes: 0 additions & 2 deletions src/hora/models.js

This file was deleted.

77 changes: 0 additions & 77 deletions src/hora/yaku.js

This file was deleted.

120 changes: 0 additions & 120 deletions src/models/Furo.js

This file was deleted.

19 changes: 19 additions & 0 deletions src/models/Furo.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Mentsu, MentsuType } from "./Mentsu";
import { Tile, TileType } from "./Tile";
import { Decoder, Encoder } from "./types";

Expand All @@ -10,6 +11,14 @@ export enum FuroType {

export class Furo {
constructor(readonly type: FuroType, readonly tile: Tile) {
let valid = true
if (this.type === FuroType.Chi) {
valid &&= tile.num >= 1 && tile.num <= 7 && tile.type !== TileType.Z
}

if (!valid) {
throw new Error(`${tile} cannot be the tile of ${type}`)
}
}

static encode: Encoder<Furo> = (data) => {
Expand Down Expand Up @@ -100,7 +109,17 @@ export class Furo {
return `${this.tile.num}${this.tile.num}${this.tile.num}${this.tile.num}${FuroType[this.type]}`.toLowerCase()
case FuroType.Ankan:
return `0${this.tile.num}${this.tile.num}0${FuroType[this.type]}`.toLowerCase()
}
}

asMentsu(): Mentsu {
switch (this.type) {
case FuroType.Chi:
return new Mentsu(MentsuType.Shuntsu, this.tile)
case FuroType.Pon:
case FuroType.Minkan:
case FuroType.Ankan:
return new Mentsu(MentsuType.Kotsu, this.tile)
}
}
}
Loading

0 comments on commit 3996141

Please sign in to comment.