Skip to content

Commit 99ddbdb

Browse files
committed
basic pgn output
1 parent 4c85131 commit 99ddbdb

File tree

3 files changed

+79
-9
lines changed

3 files changed

+79
-9
lines changed

src/components/Analysis/EngineLines.vue

+14-3
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,15 @@ export default defineComponent({
8181
let chess = new Chess(fen);
8282
8383
for (let i = 0; i < moveIndex + 1; i++) {
84-
chess.move(pv[i].trim());
84+
const move = pv[i].trim();
85+
86+
if (chess.isGameOver()) {
87+
return chess.fen();
88+
}
89+
90+
if (chess.move(move) === null) {
91+
return chess.fen();
92+
}
8593
}
8694
8795
return chess.fen();
@@ -91,7 +99,11 @@ export default defineComponent({
9199
92100
let moves = "";
93101
94-
for (let i = 0; i < moveIndex + 1; i++) {
102+
for (let i = 0; i <= moveIndex; i++) {
103+
if (i >= pv.length) {
104+
break;
105+
}
106+
95107
moves += pv[i].trim() + " ";
96108
}
97109
@@ -130,7 +142,6 @@ export default defineComponent({
130142
<SmallBoard
131143
v-if="showBoard[0] === index && showBoard[1] === indexMove"
132144
:fen="getFenForMove(index, indexMove)"
133-
:key="move"
134145
style="left: 0"
135146
/>
136147
</span>

src/components/Analysis/Pgn.vue

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<template>
2+
<div class="pgn-display">
3+
<pre>{{ formattedPgn }}</pre>
4+
</div>
5+
</template>
6+
7+
<script lang="ts">
8+
export default {
9+
props: {
10+
gamePgn: {
11+
type: String,
12+
required: true,
13+
},
14+
},
15+
computed: {
16+
formattedPgn() {
17+
const moves: string[] = this.gamePgn.split(" ");
18+
console.log(this.gamePgn);
19+
let format = "";
20+
21+
moves.forEach((move, index) => {
22+
if (index > 0 && index % 3 == 0) {
23+
format += "\n";
24+
}
25+
format += move + " ";
26+
});
27+
28+
return format;
29+
},
30+
},
31+
};
32+
</script>
33+
34+
<style scoped>
35+
.pgn-display {
36+
font-family: monospace;
37+
}
38+
</style>

src/views/Analysis.vue

+27-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import EngineStats from "@/components/Analysis/EngineStats.vue";
66
import EngineButtons from "@/components/Analysis/EngineButtons.vue";
77
import Fen from "@/components/Analysis/Fen.vue";
88
import EngineLines from "@/components/Analysis/EngineLines.vue";
9+
import Pgn from "@/components/Analysis/Pgn.vue";
910
1011
import { Chessground } from "chessground";
1112
import { Chess, SQUARES } from "chess.js";
@@ -34,6 +35,7 @@ export default defineComponent({
3435
Fen: Fen,
3536
EngineButtons: EngineButtons,
3637
EngineLines: EngineLines,
38+
Pgn: Pgn,
3739
},
3840
data() {
3941
return {
@@ -81,6 +83,7 @@ export default defineComponent({
8183
8284
startFen: startpos,
8385
currentFen: startpos,
86+
currentPgn: "",
8487
};
8588
},
8689
computed: {
@@ -95,6 +98,7 @@ export default defineComponent({
9598
} else if (this.isEngineAlive) {
9699
status = "READY";
97100
}
101+
98102
if (this.game.isCheckmate()) {
99103
status = "CHECKMATE";
100104
} else if (this.game.isStalemate()) {
@@ -163,21 +167,29 @@ export default defineComponent({
163167
async playMoves(moves: string) {
164168
const movesArray = moves.trim().split(" ");
165169
170+
await this.sendEngineCommand("stop");
171+
this.engineLines.clear();
172+
166173
for (let i = 0; i < movesArray.length; i++) {
174+
if (this.game.isGameOver()) {
175+
this.currentFen = this.game.fen();
176+
this.currentPgn = this.game.pgn();
177+
178+
return;
179+
}
180+
167181
const move = movesArray[i];
168182
169183
if (this.game.move(move) === null) {
184+
this.currentFen = this.game.fen();
185+
this.currentPgn = this.game.pgn();
186+
170187
return;
171188
}
172189
173190
this.moveHistory += move + " ";
174191
}
175192
176-
await this.sendEngineCommand("stop");
177-
await this.sendEngineCommand("go");
178-
179-
this.engineLines.clear();
180-
181193
this.cg?.set({
182194
fen: this.game.fen(),
183195
turnColor: this.toColor(),
@@ -188,7 +200,10 @@ export default defineComponent({
188200
},
189201
});
190202
203+
// await this.sendEngineCommand("go");
204+
191205
this.currentFen = this.game.fen();
206+
this.currentPgn = this.game.pgn();
192207
},
193208
handleKeydown(event: KeyboardEvent) {
194209
event.preventDefault();
@@ -290,6 +305,7 @@ export default defineComponent({
290305
291306
// force update of buttons
292307
this.currentFen = this.game.fen();
308+
this.currentPgn = this.game.pgn();
293309
},
294310
async sendOptions() {
295311
this.activeEngine?.settings.forEach((option: Option) => async () => {
@@ -322,6 +338,8 @@ export default defineComponent({
322338
if (
323339
this.engine_info.pv &&
324340
this.engine_info.pv.length > 0 &&
341+
this.engine_info.pv[0].orig != "" &&
342+
this.engine_info.pv[0].dest != "" &&
325343
this.isEngineAlive
326344
) {
327345
this.drawAnalysisMove(
@@ -448,6 +466,7 @@ export default defineComponent({
448466
449467
this.engineLines.clear();
450468
this.currentFen = this.game.fen();
469+
this.currentPgn = this.game.pgn();
451470
this.moveHistory += move.lan + " ";
452471
453472
// update chessground board
@@ -593,6 +612,7 @@ export default defineComponent({
593612
@send-moves="playMoves"
594613
:engineLines="engineLines"
595614
:fen="currentFen"
615+
:key="currentFen"
596616
:color="toColor()"
597617
/>
598618
<EngineButtons
@@ -603,7 +623,8 @@ export default defineComponent({
603623
/>
604624
</div>
605625
<div class="nav-secondary-content">
606-
<div class="game-pgn"></div>
626+
<!-- <div class="game-pgn"></div> -->
627+
<Pgn :gamePgn="currentPgn" :key="currentFen" />
607628
<div class="analysis-graph"></div>
608629
</div>
609630
</div>

0 commit comments

Comments
 (0)