Add initial code
This commit is contained in:
		
				commit
				
					
						5e60c2deb1
					
				
			
		
					 7 changed files with 250 additions and 0 deletions
				
			
		
							
								
								
									
										41
									
								
								.gitignore
									
										
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								.gitignore
									
										
									
									
										vendored
									
									
										Normal file
									
								
							|  | @ -0,0 +1,41 @@ | |||
| target/ | ||||
| !.mvn/wrapper/maven-wrapper.jar | ||||
| !**/src/main/**/target/ | ||||
| !**/src/test/**/target/ | ||||
| 
 | ||||
| ### IntelliJ IDEA ### | ||||
| .idea/modules.xml | ||||
| .idea/jarRepositories.xml | ||||
| .idea/compiler.xml | ||||
| .idea/libraries/ | ||||
| *.iws | ||||
| *.iml | ||||
| *.ipr | ||||
| 
 | ||||
| ### Eclipse ### | ||||
| .apt_generated | ||||
| .classpath | ||||
| .factorypath | ||||
| .project | ||||
| .settings | ||||
| .springBeans | ||||
| .sts4-cache | ||||
| 
 | ||||
| ### NetBeans ### | ||||
| /nbproject/private/ | ||||
| /nbbuild/ | ||||
| /dist/ | ||||
| /nbdist/ | ||||
| /.nb-gradle/ | ||||
| build/ | ||||
| !**/src/main/**/build/ | ||||
| !**/src/test/**/build/ | ||||
| 
 | ||||
| ### VS Code ### | ||||
| .vscode/ | ||||
| 
 | ||||
| ### Mac OS ### | ||||
| .DS_Store | ||||
| 
 | ||||
| /tmp/ | ||||
| *.png | ||||
							
								
								
									
										10
									
								
								.idea/.gitignore
									
										
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								.idea/.gitignore
									
										
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							|  | @ -0,0 +1,10 @@ | |||
| # Default ignored files | ||||
| /shelf/ | ||||
| /workspace.xml | ||||
| # Editor-based HTTP Client requests | ||||
| /httpRequests/ | ||||
| # Datasource local storage ignored files | ||||
| /dataSources/ | ||||
| /dataSources.local.xml | ||||
| 
 | ||||
| /discord.xml | ||||
							
								
								
									
										7
									
								
								.idea/encodings.xml
									
										
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								.idea/encodings.xml
									
										
									
										generated
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,7 @@ | |||
| <?xml version="1.0" encoding="UTF-8"?> | ||||
| <project version="4"> | ||||
|   <component name="Encoding"> | ||||
|     <file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" /> | ||||
|     <file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" /> | ||||
|   </component> | ||||
| </project> | ||||
							
								
								
									
										14
									
								
								.idea/misc.xml
									
										
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								.idea/misc.xml
									
										
									
										generated
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,14 @@ | |||
| <?xml version="1.0" encoding="UTF-8"?> | ||||
| <project version="4"> | ||||
|   <component name="ExternalStorageConfigurationManager" enabled="true" /> | ||||
|   <component name="MavenProjectsManager"> | ||||
|     <option name="originalFiles"> | ||||
|       <list> | ||||
|         <option value="$PROJECT_DIR$/pom.xml" /> | ||||
|       </list> | ||||
|     </option> | ||||
|   </component> | ||||
|   <component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="homebrew-23" project-jdk-type="JavaSDK"> | ||||
|     <output url="file://$PROJECT_DIR$/out" /> | ||||
|   </component> | ||||
| </project> | ||||
							
								
								
									
										6
									
								
								.idea/vcs.xml
									
										
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								.idea/vcs.xml
									
										
									
										generated
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,6 @@ | |||
| <?xml version="1.0" encoding="UTF-8"?> | ||||
| <project version="4"> | ||||
|   <component name="VcsDirectoryMappings"> | ||||
|     <mapping directory="$PROJECT_DIR$" vcs="Git" /> | ||||
|   </component> | ||||
| </project> | ||||
							
								
								
									
										42
									
								
								pom.xml
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								pom.xml
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,42 @@ | |||
| <?xml version="1.0" encoding="UTF-8"?> | ||||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||||
|          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||||
|          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||||
|     <modelVersion>4.0.0</modelVersion> | ||||
| 
 | ||||
|     <groupId>moe.wah</groupId> | ||||
|     <artifactId>PGN-ator</artifactId> | ||||
|     <version>1.0-SNAPSHOT</version> | ||||
| 
 | ||||
|     <properties> | ||||
|         <maven.compiler.source>21</maven.compiler.source> | ||||
|         <maven.compiler.target>21</maven.compiler.target> | ||||
|         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||||
|     </properties> | ||||
| 
 | ||||
|     <repositories> | ||||
|         <repository> | ||||
|             <id>jitpack.io</id> | ||||
|             <url>https://jitpack.io</url> | ||||
|         </repository> | ||||
|     </repositories> | ||||
| 
 | ||||
|     <dependencies> | ||||
|         <dependency> | ||||
|             <groupId>com.github.bhlangonijr</groupId> | ||||
|             <artifactId>chesslib</artifactId> | ||||
|             <version>1.3.4</version> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>com.github.alexandreroman</groupId> | ||||
|             <artifactId>chessimage</artifactId> | ||||
|             <version>1.0.0</version> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>com.sksamuel.scrimage</groupId> | ||||
|             <artifactId>scrimage-core</artifactId> | ||||
|             <version>4.3.1</version> | ||||
|         </dependency> | ||||
|     </dependencies> | ||||
| 
 | ||||
| </project> | ||||
							
								
								
									
										130
									
								
								src/main/java/moe/wah/Main.java
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										130
									
								
								src/main/java/moe/wah/Main.java
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,130 @@ | |||
| package moe.wah; | ||||
| 
 | ||||
| import com.github.alexandreroman.chessimage.ChessRenderer; | ||||
| import com.github.alexandreroman.chessimage.ChessThemeLibrary; | ||||
| import com.github.bhlangonijr.chesslib.Board; | ||||
| import com.github.bhlangonijr.chesslib.game.Game; | ||||
| import com.github.bhlangonijr.chesslib.move.Move; | ||||
| import com.github.bhlangonijr.chesslib.move.MoveList; | ||||
| import com.github.bhlangonijr.chesslib.pgn.PgnHolder; | ||||
| import com.sksamuel.scrimage.ImmutableImage; | ||||
| import com.sksamuel.scrimage.nio.StreamingGifWriter; | ||||
| import com.sksamuel.scrimage.nio.StreamingGifWriter.GifStream; | ||||
| 
 | ||||
| import javax.imageio.ImageIO; | ||||
| import java.awt.image.BufferedImage; | ||||
| import java.io.File; | ||||
| import java.io.FileOutputStream; | ||||
| import java.io.IOException; | ||||
| import java.nio.file.Files; | ||||
| import java.nio.file.Paths; | ||||
| import java.time.Duration; | ||||
| import java.time.Instant; | ||||
| import java.util.ArrayList; | ||||
| import java.util.Arrays; | ||||
| import java.util.List; | ||||
| 
 | ||||
| public class Main { | ||||
|     public static void main(String[] args) throws Exception { | ||||
|         Files.createDirectories(Paths.get("./temp/")); | ||||
|         // Load PGN from file | ||||
|         PgnHolder pgn = new PgnHolder("game.pgn"); | ||||
|         pgnToGif(pgn); | ||||
|     } | ||||
|     private static void pgnToGif(PgnHolder pgn) throws Exception { | ||||
|         ChessRenderer renderer = new ChessRenderer(ChessThemeLibrary.GREEN_THEME, 60); | ||||
|         Game game = pgnToGame(pgn); | ||||
|         String boardID = String.valueOf(Instant.now().toEpochMilli()); | ||||
|         game.loadMoveText(); | ||||
|         System.out.println(game.getTermination()); | ||||
| 
 | ||||
|         MoveList moves = game.getHalfMoves(); | ||||
|         Board board = new Board(); | ||||
|         int index = 1; | ||||
|         for (Move move : moves) { | ||||
|             board.doMove(move); | ||||
|             renderBoard(board, renderer, boardID, index); | ||||
| 
 | ||||
|             // Add 5 extra frames of final board | ||||
|             if (index == moves.size()) { | ||||
|                 for (int x = 0; x < 5; x++) { | ||||
|                     index++; | ||||
|                     renderBoard(board, renderer, boardID, index); | ||||
|                 } | ||||
|             } | ||||
|             index++; | ||||
|         } | ||||
| 
 | ||||
|         System.out.println("Rendered " + moves.size() + " moves"); | ||||
| 
 | ||||
|         boardToGif(boardID); | ||||
|     } | ||||
| 
 | ||||
|     private static void renderBoard(Board board, ChessRenderer renderer, String boardID, int index) { | ||||
|         String index_padded = String.format("%03d", index); | ||||
|         File targetFile = new File("./temp/", "board_" + boardID + "_" + index_padded + ".png"); | ||||
|         try (FileOutputStream out = new FileOutputStream(targetFile)) { | ||||
|             renderer.render(board.getFen(), out); | ||||
|         } catch (IOException e) { | ||||
|             System.out.println("Error rendering move " + index_padded + ", " + e.getMessage()); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     private static Game pgnToGame(PgnHolder pgn) throws Exception { | ||||
|         pgn.loadPgn(); | ||||
|         return pgn.getGames().getFirst(); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Converts a chess board to a GIF | ||||
|      * Output file is board_x.gif where x is the boardID | ||||
|      * @param boardID   ID of the board (UNIX epoch) | ||||
|      */ | ||||
|     public static void boardToGif(String boardID) throws Exception { | ||||
|         List<File> boardFiles = getBoardFiles(new File("./temp/"), boardID); | ||||
|         if (boardFiles.isEmpty()) { | ||||
|             throw new RuntimeException("No PNG files found for board with ID: " + boardID); | ||||
|         } | ||||
|         List<BufferedImage> images = new ArrayList<>(); | ||||
|         for (File file : boardFiles) { | ||||
|             BufferedImage image = ImageIO.read(file); | ||||
|             images.add(image); | ||||
|         } | ||||
|         System.out.println("There are " + (long) images.size() + " frames for board " + boardID); | ||||
|         String gif_name = "board_" + boardID + ".gif"; | ||||
|         StreamingGifWriter writer = new StreamingGifWriter(Duration.ofMillis(500), true, false); | ||||
|         GifStream gif = writer.prepareStream(gif_name, BufferedImage.TYPE_INT_ARGB); | ||||
|         for (BufferedImage image : images) { | ||||
|             gif.writeFrame(ImmutableImage.fromAwt(image)); | ||||
|         } | ||||
|         gif.close(); | ||||
|         System.out.println("Rendered " + gif_name); | ||||
|         for (File file : boardFiles) { | ||||
|             boolean delete_success = file.delete(); | ||||
|             if (!delete_success) { | ||||
|                 System.out.println("Failed to delete " + file.getName() + "!"); | ||||
|             } | ||||
|         } | ||||
|         System.out.println("Removed " + boardFiles.size() + " temp files"); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Gets the PNG files for the specified board | ||||
|      * @param directory Directory to search | ||||
|      * @param boardID   Board ID (UNIX epoch) | ||||
|      * @return A list of board Files | ||||
|      */ | ||||
|     private static List<File> getBoardFiles(File directory, String boardID) { | ||||
|         List<File> boardFiles = new ArrayList<>(); | ||||
|         File[] files = directory.listFiles(); | ||||
|         assert files != null; | ||||
|         Arrays.sort(files); | ||||
|         for (File file : files) { | ||||
|             if (file.isFile() && file.getName().toLowerCase().endsWith(".png") | ||||
|                     && file.getName().startsWith("board_" + boardID)) { | ||||
|                 boardFiles.add(file); | ||||
|             } | ||||
|         } | ||||
|         return boardFiles; | ||||
|     } | ||||
| } | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue