Skip to content
This repository has been archived by the owner on Apr 9, 2023. It is now read-only.

Commit

Permalink
Support 00AL for Ikkatsu expression (Closes #69)
Browse files Browse the repository at this point in the history
  • Loading branch information
na2hiro committed Oct 2, 2022
1 parent 6fead1e commit e4d0f0e
Show file tree
Hide file tree
Showing 2 changed files with 242 additions and 38 deletions.
252 changes: 217 additions & 35 deletions src/peg/__tests__/csaParserTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -583,52 +583,234 @@ P-00GI00GI00GI00GI00KE00KE00KE00KE\n\
],
});
});
it("AL", () => {
expect(
parse("\
describe("AL", () => {
it("works with 駒別単独", () => {
expect(
parse("\
V2.2\n\
P+23FU\n\
P+23TO\n\
P-11OU21KE\n\
P+00KI\n\
P-00AL\n\
+\n\
+0022KI\n\
%TSUMI\n")
).toEqual({
header: {},
initial: {
preset: "OTHER",
data: {
board: [
[{color: 1, kind: "OU"}, {}, {}, {}, {}, {}, {}, {}, {}],
[
{color: 1, kind: "KE"},
{},
{color: 0, kind: "FU"},
{},
{},
{},
{},
{},
{},
).toEqual({
header: {},
initial: {
preset: "OTHER",
data: {
board: [
[{color: 1, kind: "OU"}, {}, {}, {}, {}, {}, {}, {}, {}],
[
{color: 1, kind: "KE"},
{},
{color: 0, kind: "TO"},
{},
{},
{},
{},
{},
{},
],
[{}, {}, {}, {}, {}, {}, {}, {}, {}],
[{}, {}, {}, {}, {}, {}, {}, {}, {}],
[{}, {}, {}, {}, {}, {}, {}, {}, {}],
[{}, {}, {}, {}, {}, {}, {}, {}, {}],
[{}, {}, {}, {}, {}, {}, {}, {}, {}],
[{}, {}, {}, {}, {}, {}, {}, {}, {}],
[{}, {}, {}, {}, {}, {}, {}, {}, {}],
],
[{}, {}, {}, {}, {}, {}, {}, {}, {}],
[{}, {}, {}, {}, {}, {}, {}, {}, {}],
[{}, {}, {}, {}, {}, {}, {}, {}, {}],
[{}, {}, {}, {}, {}, {}, {}, {}, {}],
[{}, {}, {}, {}, {}, {}, {}, {}, {}],
[{}, {}, {}, {}, {}, {}, {}, {}, {}],
[{}, {}, {}, {}, {}, {}, {}, {}, {}],
color: 0,
hands: [
{FU: 0, KY: 0, KE: 0, GI: 0, KI: 1, KA: 0, HI: 0},
{FU: 17, KY: 4, KE: 3, GI: 4, KI: 3, KA: 2, HI: 2},
],
},
},
moves: [{}, {move: {to: p(2, 2), piece: "KI"}}, {special: "TSUMI"}],
});
});
});
it("works with 一括表現", () => {
expect(
parse(`P1 * * * * * * * * *
P2 * * * * * * * * *
P3 * * * -FU-FU * * * *
P4-RY * * -OU * -FU * * *
P5 * * +KY * -TO * * * *
P6 * * +RY * * * * * *
P7+KA * * * * * * * *
P8+KA * * * * * * * *
P9 * * * * * * * * *
P-00AL
+
`)
).toMatchInlineSnapshot(`
Object {
"header": Object {},
"initial": Object {
"data": Object {
"board": Array [
Array [
Object {},
Object {},
Object {},
Object {},
Object {},
Object {},
Object {},
Object {},
Object {},
],
color: 0,
hands: [
{FU: 0, KY: 0, KE: 0, GI: 0, KI: 1, KA: 0, HI: 0},
{FU: 17, KY: 4, KE: 3, GI: 4, KI: 3, KA: 2, HI: 2},
Array [
Object {},
Object {},
Object {},
Object {},
Object {},
Object {},
Object {},
Object {},
Object {},
],
Array [
Object {},
Object {},
Object {},
Object {},
Object {},
Object {},
Object {},
Object {},
Object {},
],
Array [
Object {},
Object {},
Object {},
Object {
"color": 1,
"kind": "FU",
},
Object {},
Object {},
Object {},
Object {},
Object {},
],
Array [
Object {},
Object {},
Object {
"color": 1,
"kind": "FU",
},
Object {},
Object {
"color": 1,
"kind": "TO",
},
Object {},
Object {},
Object {},
Object {},
],
Array [
Object {},
Object {},
Object {
"color": 1,
"kind": "FU",
},
Object {
"color": 1,
"kind": "OU",
},
Object {},
Object {},
Object {},
Object {},
Object {},
],
Array [
Object {},
Object {},
Object {},
Object {},
Object {
"color": 0,
"kind": "KY",
},
Object {
"color": 0,
"kind": "RY",
},
Object {},
Object {},
Object {},
],
Array [
Object {},
Object {},
Object {},
Object {},
Object {},
Object {},
Object {},
Object {},
Object {},
],
Array [
Object {},
Object {},
Object {},
Object {
"color": 1,
"kind": "RY",
},
Object {},
Object {},
Object {
"color": 0,
"kind": "KA",
},
Object {
"color": 0,
"kind": "KA",
},
Object {},
],
],
"color": 0,
"hands": Array [
Object {
"FU": 0,
"GI": 0,
"HI": 0,
"KA": 0,
"KE": 0,
"KI": 0,
"KY": 0,
},
Object {
"FU": 14,
"GI": 4,
"HI": 0,
"KA": 0,
"KE": 4,
"KI": 4,
"KY": 3,
},
],
},
},
moves: [{}, {move: {to: p(2, 2), piece: "KI"}}, {special: "TSUMI"}],
});
"preset": "OTHER",
},
"moves": Array [
Object {},
],
}
`);
});
});
it("header", () => {
Expand Down
28 changes: 25 additions & 3 deletions src/peg/csa-parser.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@
"TIME_LIMIT": "持ち時間",
}[key] || key;
}
function unpromote(kind) {
return {
TO: "FU",
NY: "KY",
NK: "KE",
NG: "GI",
UM: "KA",
RY: "HI",
}[kind] || kind;
}
}
kifu = csa2 / csa1

Expand Down Expand Up @@ -68,6 +78,20 @@ initialboard = comment* data:(hirate / ikkatsu / ""{return "NO"}) koma:komabetsu
}else{
data.data.hands = koma.data.hands;
}
var alColor = data.data.hands.indexOf("AL_TO_REPLACE");
if(alColor>=0) {
var rest = {FU:18,KY:4,KE:4,GI:4,KI:4,KA:2,HI:2};
for(var piece in data.data.hands[1-alColor]) {
rest[piece] -= data.data.hands[1-alColor][piece];
}
for(var x=0; x<data.data.board.length; x++) {
for(var y=0; y<data.data.board[x].length; y++) {
var p = data.data.board[x][y];
if(p.kind && (p.kind!="OU")) rest[unpromote(p.kind)]--;
}
}
data.data.hands[alColor] = rest;
}
data.data.color=teban;
return data;
}
Expand Down Expand Up @@ -101,7 +125,6 @@ komabetsu = lines:komabetsuline* {
{FU:0,KY:0,KE:0,GI:0,KI:0,KA:0,HI:0},
{FU:0,KY:0,KE:0,GI:0,KI:0,KA:0,HI:0}
];
var all = {FU:18,KY:4,KE:4,GI:4,KI:4,KA:2,HI:2};
for(var i=0; i<9; i++){
var line=[];
for(var j=0; j<9; j++){
Expand All @@ -115,7 +138,7 @@ komabetsu = lines:komabetsuline* {
if(p.xy.x==0){
// 持ち駒
if(p.piece=="AL"){
hands[lines[i].teban]=all;
hands[lines[i].teban]="AL_TO_REPLACE";
break all;
}
var obj=hands[lines[i].teban];
Expand All @@ -124,7 +147,6 @@ komabetsu = lines:komabetsuline* {
// 盤上
board[p.xy.x-1][p.xy.y-1] = {color: lines[i].teban, kind: p.piece};
}
if(p.piece!="OU") all[p.piece]--;
}
}
return {preset: "OTHER", data: {board: board, hands: hands}}
Expand Down

0 comments on commit e4d0f0e

Please sign in to comment.