Skip to content

Commit 7845442

Browse files
authored
Merge pull request #1 from Goita/movex
support Movex
2 parents b4a139a + 9815f78 commit 7845442

File tree

8 files changed

+184
-40
lines changed

8 files changed

+184
-40
lines changed

.travis.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
language: node_js
22
node_js:
3-
- "6"
4-
- "5"
5-
- "4"
3+
- "8"

package.json

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "goita-core",
3-
"version": "0.1.10",
3+
"version": "0.2.1",
44
"description": "Goita Core Library in JavaScript",
55
"private": false,
66
"main": "lib/index.js",
@@ -28,19 +28,17 @@
2828
"dependencies": {
2929
"@types/random-js": "^1.0.28",
3030
"random-js": "^1.0.8",
31-
"tslib": "^1.2.0"
31+
"tslib": "^1.7.1"
3232
},
3333
"devDependencies": {
34-
"@types/chai": "^3.4.34",
35-
"@types/mocha": "^2.2.38",
36-
"@types/node": "^6.0.0",
37-
"chai": "^3.5.0",
38-
"mocha": "^3.2.0",
39-
"node-inspector": "^0.12.10",
40-
"ts-node": "^1.7.0",
34+
"@types/chai": "^4.0.1",
35+
"@types/mocha": "^2.2.41",
36+
"@types/node": "^8.0.7",
37+
"chai": "^4.0.2",
38+
"mocha": "^3.4.2",
39+
"ts-node": "^3.1.0",
4140
"tslint": "^4.0.2",
42-
"typescript": "^2.0.10",
43-
"v8-profiler": "^5.6.5",
41+
"typescript": "^2.4.1",
4442
"webpack": "^2.2.1"
4543
}
4644
}

src/board.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ export class Board {
293293
}
294294

295295
// remove initial-condition-info. like "12345678,12345679,11112345,11112345,s1,"
296-
let history = this.history.toHiddenString().substring(39);
296+
let history = this.history.toHiddenString(this.history.turn).substring(39);
297297

298298
return new ThinkingInfo(turn, this.history.dealer, hand, fields, hidden, lastAttack, this._yakuInfo, history);
299299
}

src/debug.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import * as goita from "./";
22

3-
let b = goita.Board.createFromString("11244556,12234569,11123378,11113457,s3,371,411,115,2p,3p,4p,145,252,3p,4p,124,242,323");
4-
b.play(goita.Koma.bakko, goita.Koma.kin);
5-
console.log(b);
3+
let b = goita.Board.createFromString("11244556,12234569,11123378,11113457,s3,371,411,115,2p,3p,4p,145,252,3p,4p,124");
4+
console.time("profile");
5+
const solver = new goita.Solver();
6+
const ret = solver.solve(b.toHistoryString());
67

7-
// for (const evm of ret) {
8-
// // tslint:disable-next-line:no-console
9-
// console.log(evm.move.toOpenTextString() + ", " + evm.move.toOpenString() + " ,score: " + evm.score);
10-
// }
8+
for (const evm of ret) {
9+
// tslint:disable-next-line:no-console
10+
console.log(evm.move.toOpenTextString() + ", " + evm.move.toOpenString() + " ,score: " + evm.score);
11+
}
12+
console.timeEnd("profile");
1113

1214
process.exit(0);

src/history.ts

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ export class BoardHistory {
201201
}
202202

203203
public get lastAttackMove(): Move {
204-
for (let i = (this.moveStack.length - 1)|0; i >= 0; i = (i - 1)|0) {
204+
for (let i = (this.moveStack.length - 1) | 0; i >= 0; i = (i - 1) | 0) {
205205
let m = this.moveStack[i];
206206
if (!m.pass) {
207207
return m;
@@ -260,7 +260,7 @@ export class BoardHistory {
260260
let historyArray = history.split(Define.historyStringDelimiter);
261261

262262
let tegomas = new Array<string>();
263-
for (let i = 0; i < (Define.maxPlayers|0); i = (i+1)|0) {
263+
for (let i = 0; i < (Define.maxPlayers | 0); i = (i + 1) | 0) {
264264
tegomas.push(historyArray[i]);
265265
}
266266

@@ -287,7 +287,7 @@ export class BoardHistory {
287287
let attacker: number = Number(moveHistory[0].charAt(0)) - 1;
288288

289289
for (let m of moveHistory) {
290-
if(!m || m.length === 0) {
290+
if (!m || m.length === 0) {
291291
continue;
292292
}
293293
let move = Move.fromStr(m, attacker);
@@ -301,7 +301,7 @@ export class BoardHistory {
301301

302302
public toString(): string {
303303
let str = new Array<string>();
304-
for (let i = 0; i < (Define.maxPlayers|0); i = (i+1)|0) {
304+
for (let i = 0; i < (Define.maxPlayers | 0); i = (i + 1) | 0) {
305305
const hand = KomaArray.createFrom(this.hands[i]).sort();
306306
str.push(KomaArray.toString(hand));
307307
}
@@ -312,25 +312,69 @@ export class BoardHistory {
312312
return str.join(Define.historyStringDelimiter);
313313
}
314314

315-
public toHiddenString(): string {
315+
/** describe history in the view of given player no */
316+
public toHiddenString(no: number): string {
316317
let str = new Array<string>();
317-
for (let i = 0; i < (Define.maxPlayers|0); i = (i+1)|0) {
318-
if(i === this.turn){
318+
for (let i = 0; i < (Define.maxPlayers | 0); i = (i + 1) | 0) {
319+
if (i === no) {
319320
const hand = KomaArray.createFrom(this.hands[i]).sort();
320321
str.push(KomaArray.toString(hand));
321-
} else{
322-
str.push("xxxxxxxx");
322+
} else {
323+
str.push(null); //empty for the space
323324
}
324325
}
325326
str.push(Define.dealerChar + (this.dealer + 1));
326327
for (const move of this.moveStack) {
327-
if(move.no === this.turn) {
328+
if (move.no === no) {
328329
str.push(move.toOpenString());
329-
}else{
330+
} else {
330331
// put hidden info
331332
str.push(move.toString());
332333
}
333334
}
335+
336+
// fill other players hand
337+
const hands = [] as Koma[][];
338+
for (let i = 0; i < 4; i++) {
339+
hands[i] = [];
340+
}
341+
for (const move of this.moveStack) {
342+
if (move.no === no || move.pass) {
343+
continue;
344+
}
345+
346+
if (!move.block.isHidden && !move.faceDown) {
347+
hands[move.no].push(move.block);
348+
}
349+
350+
if (!move.attack.isHidden) {
351+
hands[move.no].push(move.attack);
352+
}
353+
}
354+
355+
356+
for (let i = 0; i < 4; i++) {
357+
if (i === no) { continue; }
358+
359+
const hand = hands[i];
360+
// fill goshi hand
361+
const initHand = KomaArray.createFrom(this.hands[i]);
362+
const shiCount = KomaArray.count(initHand, Koma.shi);
363+
if (shiCount >= 5) {
364+
while (KomaArray.count(hand, Koma.shi) < shiCount) {
365+
hand.push(Koma.shi);
366+
}
367+
}
368+
369+
// fill hidden
370+
while (KomaArray.getLength(hand) < Define.maxFieldLength) {
371+
hand.push(Koma.hidden);
372+
}
373+
374+
// replace
375+
KomaArray.sortAsc(hand);
376+
str[i] = KomaArray.toString(hand);
377+
}
334378
return str.join(Define.historyStringDelimiter);
335379
}
336380
}

test/board.spec.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Board, Koma, KomaArray } from '../src';
1+
import { Board, Koma, KomaArray, Move } from '../src';
22
import * as Chai from "chai";
33
import { Yaku } from '../src/define';
44

@@ -10,7 +10,7 @@ describe('Board', () => {
1010
testBoard = Board.createFromString("12345678,12345679,11112345,11112345,s1");
1111
});
1212

13-
describe('#constructor', () => {
13+
describe('#createRandomly', () => {
1414
it("create object", () => {
1515
let dealer = 2;
1616
let table = Board.createRandomly(dealer);
@@ -21,6 +21,25 @@ describe('Board', () => {
2121
});
2222
});
2323

24+
describe('#createFromString', () => {
25+
it("create test", () => {
26+
let dealer = 0;
27+
let table = Board.createFromString("12345678,12345679,11112345,11112345,s1");
28+
expect(table.turnPlayer.no).to.equal(dealer);
29+
expect(table.history.lastAttacker).to.equal(dealer);
30+
expect(table.history.turn).to.equal(dealer);
31+
expect(table.history.moveStack.length).to.equal(0);
32+
});
33+
it("create initial state with hidden", () => {
34+
let dealer = 0;
35+
let table = Board.createFromString("12345678,xxxxxxxx,xxxxxxxx,xxxxxxxx,s1");
36+
expect(table.turnPlayer.no).to.equal(dealer);
37+
expect(table.history.lastAttacker).to.equal(dealer);
38+
expect(table.history.turn).to.equal(dealer);
39+
expect(table.history.moveStack.length).to.equal(0);
40+
});
41+
});
42+
2443
describe('#play', () => {
2544
it("face down", () => {
2645
testBoard.play(Koma.shi, Koma.bakko);
@@ -34,8 +53,24 @@ describe('Board', () => {
3453
expect(testBoard.history.lastMove.toOpenString()).to.equal("236");
3554
});
3655
});
56+
57+
describe("#playMove", () => {
58+
it("replay hidden history", () => {
59+
// "3xxxxxxx,12345679,xxxxxxxx,123xxxxx,s1,1x3,2p,3p,431,1p,2p,3p,4x2"
60+
const board = Board.createFromString("3xxxxxxx,12345679,xxxxxxxx,123xxxxx,s1");
61+
board.playMove(Move.ofFaceDown(0, Koma.hidden, Koma.bakko));
62+
board.playMove(Move.ofPass(1));
63+
board.playMove(Move.ofPass(2));
64+
board.playMove(Move.ofMatch(3, Koma.bakko, Koma.shi));
65+
board.playMove(Move.ofPass(0));
66+
board.playMove(Move.ofPass(1));
67+
board.playMove(Move.ofPass(2));
68+
board.playMove(Move.ofFaceDown(3, Koma.hidden, Koma.gon));
69+
expect(board.toHistoryString()).to.equal("3xxxxxxx,12345679,xxxxxxxx,123xxxxx,s1,1x3,2p,3p,431,1p,2p,3p,4x2");
70+
});
71+
});
3772
describe('#play: kingFallback', () => {
38-
beforeEach(()=>{
73+
beforeEach(() => {
3974
testBoard.kingFallbacks = true;
4075
});
4176
it("gyoku -> ou", () => {

test/history.spec.ts

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ describe('Move', () => {
2626
expect(move.no).to.equal(2);
2727
expect(move.pass).to.equal(true);
2828
});
29+
it("parses Hidden move string", () => {
30+
let move = Move.fromStr("3x2", 2);
31+
expect(move.no).to.equal(2);
32+
expect(move.block).to.equal(Koma.hidden);
33+
expect(move.attack).to.equal(Koma.gon);
34+
expect(move.pass).to.equal(false);
35+
expect(move.faceDown).to.equal(true);
36+
});
2937
});
3038

3139
describe('#toString', () => {
@@ -41,6 +49,10 @@ describe('Move', () => {
4149
let move = Move.ofPass(2);
4250
expect(move.toString()).to.equal("3p");
4351
});
52+
it("describes Hidden-faceDown move to string", () => {
53+
let move = Move.ofFaceDown(0, Koma.hidden, Koma.gin);
54+
expect(move.toString()).to.equal("1x4");
55+
});
4456
});
4557

4658
describe('#toOpenString', () => {
@@ -56,6 +68,10 @@ describe('Move', () => {
5668
let move = Move.ofPass(2);
5769
expect(move.toOpenString()).to.equal("3p");
5870
});
71+
it("describes Hidden-faceDown move to string", () => {
72+
let move = Move.ofFaceDown(0, Koma.hidden, Koma.gin);
73+
expect(move.toOpenString()).to.equal("1x4");
74+
});
5975
});
6076

6177
describe('#toScore', () => {
@@ -99,7 +115,7 @@ describe('BoardHistory', () => {
99115
});
100116
describe('#parseMoveHistory', () => {
101117
it("parse empty", () => {
102-
let array = "".split(",");
118+
let array = [] as string[];
103119
let moves = BoardHistory.parseMoveHistory(array);
104120
expect(moves.length).to.equal(0);
105121
});
@@ -112,6 +128,15 @@ describe('BoardHistory', () => {
112128
expect(moves[2].toOpenString()).to.equal("3p");
113129
expect(moves[3].toOpenString()).to.equal("431");
114130
});
131+
it("parse moves containing hidden", () => {
132+
let array = "1x3,2p,3p,431,1p,2p,315".split(",");
133+
let moves = BoardHistory.parseMoveHistory(array);
134+
expect(moves.length).to.equal(7);
135+
expect(moves[0].toOpenString()).to.equal("1x3");
136+
expect(moves[1].toOpenString()).to.equal("2p");
137+
expect(moves[2].toOpenString()).to.equal("3p");
138+
expect(moves[3].toOpenString()).to.equal("431");
139+
});
115140
});
116141

117142
describe('#toString', () => {
@@ -124,8 +149,21 @@ describe('BoardHistory', () => {
124149
});
125150

126151
describe('#toHiddenString', () => {
127-
it("describes hidden history in string format", () => {
128-
expect(sampleHistory.toHiddenString()).to.equal("xxxxxxxx,xxxxxxxx,xxxxxxxx,11112345,s1,1x3,2p,3p,431,1p,2p,315");
152+
it("describes hidden history #0", () => {
153+
const h = BoardHistory.fromString("12345678,12345679,11112345,11112345,s1,113,2p,3p,431,1p,2p,3p,412");
154+
expect(h.toHiddenString(0)).to.equal("12345678,xxxxxxxx,xxxxxxxx,123xxxxx,s1,113,2p,3p,431,1p,2p,3p,4x2");
155+
});
156+
it("describes hidden history #1", () => {
157+
const h = BoardHistory.fromString("12345678,12345679,11112345,11112345,s1,113,2p,3p,431,1p,2p,3p,412");
158+
expect(h.toHiddenString(1)).to.equal("3xxxxxxx,12345679,xxxxxxxx,123xxxxx,s1,1x3,2p,3p,431,1p,2p,3p,4x2");
159+
});
160+
it("describes hidden history #2", () => {
161+
const h = BoardHistory.fromString("12345678,12345679,11112345,11112345,s1,113,2p,3p,431,1p,2p,3p,412");
162+
expect(h.toHiddenString(2)).to.equal("3xxxxxxx,xxxxxxxx,11112345,123xxxxx,s1,1x3,2p,3p,431,1p,2p,3p,4x2");
163+
});
164+
it("describes hidden history #3", () => {
165+
const h = BoardHistory.fromString("12345678,12345679,11112345,11112345,s1,113,2p,3p,431,1p,2p,3p,412");
166+
expect(h.toHiddenString(3)).to.equal("3xxxxxxx,xxxxxxxx,xxxxxxxx,11112345,s1,1x3,2p,3p,431,1p,2p,3p,412");
129167
});
130168
});
131169

@@ -157,4 +195,4 @@ describe('BoardHistory', () => {
157195
expect(sampleHistory.moveStack.length).to.equal(preLength + 1);
158196
});
159197
});
160-
});
198+
});

0 commit comments

Comments
 (0)