Skip to content

Commit

Permalink
WikiDiagram converter:
Browse files Browse the repository at this point in the history
added comment to the diagram generation,
added pieces in hands,
deleted floatright,
added test for pieces in hand.
  • Loading branch information
sleepingchinchilla committed Jan 12, 2024
1 parent 89aed3a commit a5b19ba
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
package com.playshogi.library.shogi.models.formats.wiki;

import com.playshogi.library.shogi.models.Piece;
import com.playshogi.library.shogi.models.formats.usf.UsfUtil;
import com.playshogi.library.shogi.models.PieceType;
import com.playshogi.library.shogi.models.Player;
import com.playshogi.library.shogi.models.formats.kif.KifUtils;
import com.playshogi.library.shogi.models.position.KomadaiState;
import com.playshogi.library.shogi.models.position.ReadOnlyShogiPosition;
import com.playshogi.library.shogi.models.position.ShogiBoardState;
import com.playshogi.library.shogi.models.position.ShogiPosition;

import java.util.Optional;

import static com.playshogi.library.shogi.models.position.ShogiBoardState.FIRST_COLUMN;
import static com.playshogi.library.shogi.models.position.ShogiBoardState.FIRST_ROW;

public class WikiConverter {
public static String toWikiDiagram(final ReadOnlyShogiPosition position, String title) {
public static String toWikiDiagram(final ReadOnlyShogiPosition position, String title, String comment) {

if (title.isEmpty()) {
title = "Put title here!!";
}
if (comment.isEmpty()) {
comment = " Insert Comment Here!";
}

if(title.isEmpty()){ title = "Put title here!!";}

String resultDiagram = "{{shogi diagram \n" +
"| floatright\n" +
"| \n" +
"| '''" + title + "'''\n" +
"| \n" +
"| " + getPiecesInHandInWikiFormat(position, Player.WHITE) + "\n" +
getBoardStateInWikiFormat(position) +
"| \n" +
"|\n" +
"| " + getPiecesInHandInWikiFormat(position, Player.BLACK) + "\n" +
"|" + comment + "\n" +
"}}";

getBoardStateInWikiFormat(position);
Expand All @@ -31,19 +39,35 @@ public static String toWikiDiagram(final ReadOnlyShogiPosition position, String
return resultDiagram;
}

private static String getPiecesInHandInWikiFormat(final ReadOnlyShogiPosition position, Player player) {
KomadaiState komadai = player == Player.BLACK ? position.getSenteKomadai() : position.getGoteKomadai();
String result = "–";
if (!komadai.isEmpty()) {
result = "";
for (PieceType piece : PieceType.STRONGEST_TO_WEAKEST) {
int pieceCount = komadai.getPiecesOfType(piece);
if (pieceCount > 0) result += KifUtils.getJapanesePieceSymbol(piece) + "<sub>" + pieceCount + "</sub> ";
}
}
return result.trim();
}

private static String getBoardStateInWikiFormat(final ReadOnlyShogiPosition position) {
StringBuilder builder = new StringBuilder(600);
builder.append("");
ShogiBoardState shogiBoardState = position.getShogiBoardState();
for (int row = FIRST_ROW; row <= shogiBoardState.getLastRow(); row++) {
for (int column = FIRST_COLUMN; column <= shogiBoardState.getLastColumn(); column++) {
builder.append('|');
Optional<Piece> pieceAt = shogiBoardState.getPieceAt(10 - column, row);
if (pieceAt.isPresent()) {
String pieceToString = " " + pieceToString(pieceAt.get()) + " ";
String pieceToString = " " + pieceToString(pieceAt.get());
// characters piece like pss
builder.append(pieceToString);
if (pieceToString.length() == 3) {
builder.append(' ');
}
} else {
if(shogiBoardState.getLastColumn() != column) {
if (shogiBoardState.getLastColumn() != column) {
builder.append(" ");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.playshogi.library.shogi.models.formats.wiki;

import com.playshogi.library.shogi.models.formats.sfen.SfenConverter;
import com.playshogi.library.shogi.models.shogivariant.Handicap;
import com.playshogi.library.shogi.models.shogivariant.ShogiInitialPositionFactory;
import org.junit.Test;
Expand All @@ -9,10 +10,10 @@
public class WikiConverterTest {

@Test
public void toWikiDiagram() {
String diagram = WikiConverter.toWikiDiagram(ShogiInitialPositionFactory.createInitialPosition(Handicap.SIX_PIECES), "6-Piece Handicap");
public void toWikiDiagramHandicap() {
String diagram = WikiConverter.toWikiDiagram(ShogiInitialPositionFactory.createInitialPosition(Handicap.SIX_PIECES), "6-Piece Handicap", "");
assertEquals( "{{shogi diagram \n" +
"| floatright\n" +
"| \n" +
"| '''6-Piece Handicap'''\n" +
"| –\n" +
"| | | sg | gg | kg | gg | sg | |\n" +
Expand All @@ -25,7 +26,28 @@ public void toWikiDiagram() {
"| | bs | | | | | | rs |\n" +
"| ls | ns | ss | gs | ks | gs | ss | ns | ls \n" +
"| –\n" +
"|\n" +
"| Insert Comment Here!\n" +
"}}", diagram);
}

@Test
public void toWikiDiagramPiecesInHand() {
String diagram = WikiConverter.toWikiDiagram(SfenConverter.fromSFEN("1+B7/1K+B6/1S+P3+R2/P2P1P2G/1+r2g2l1/9/5+p+p2/4g2+sl/7+lk b S2NL9Pgs2n3p 1"), "Kimura vs Toyoshima 2019", " After 285 moves.");
assertEquals( "{{shogi diagram \n" +
"| \n" +
"| '''Kimura vs Toyoshima 2019'''\n" +
"| 金<sub>1</sub> 銀<sub>1</sub> 桂<sub>2</sub> 歩<sub>3</sub>\n" +
"| | hs | | | | | | |\n" +
"| | ks | hs | | | | | |\n" +
"| | ss | ts | | | | ds | |\n" +
"| ps | | | ps | | ps | | | gs \n" +
"| | dg | | | gg | | | lg |\n" +
"| | | | | | | | |\n" +
"| | | | | | tg | tg | |\n" +
"| | | | | gg | | | psg| lg \n" +
"| | | | | | | | plg| kg \n" +
"| 銀<sub>1</sub> 桂<sub>2</sub> 香<sub>1</sub> 歩<sub>9</sub>\n" +
"| After 285 moves.\n" +
"}}", diagram);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ public void onCreateDiagramSVG(final CreateSVGDiagramEvent event) {
public void onCreateDiagramWiki(final CreateWikiDiagramEvent event) {
// TODO figure out a way to put the position Wiki in clipboard
if (position == null) return;
final DialogBox dialogBox = createWikiDiagramDialogBox(WikiConverter.toWikiDiagram(position, ""));
final DialogBox dialogBox = createWikiDiagramDialogBox(WikiConverter.toWikiDiagram(position, "", ""));
dialogBox.center();
dialogBox.show();
}
Expand Down

0 comments on commit a5b19ba

Please sign in to comment.