### Initialize Xiangqi Game Instance Source: https://context7.com/lengyanyu258/xiangqi.js/llms.txt Demonstrates how to create a new Xiangqi game instance. It can be initialized with the standard starting position or a custom position defined by a FEN string. The .fen() method is used to retrieve the current board state. ```javascript const Xiangqi = require('./xiangqi').Xiangqi; // Create game with default starting position const xiangqi = new Xiangqi(); console.log(xiangqi.fen()); // -> 'rnbakabnr/9/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/9/RNBAKABNR r - - 0 1' // Create game from specific position (checkmate position) const checkmate = new Xiangqi('5kC2/4a1N2/3a5/9/9/9/9/r3C4/4p4/2rK4R r - - 0 1'); console.log(checkmate.in_checkmate()); // -> true ``` -------------------------------- ### Complete Game Example Source: https://context7.com/lengyanyu258/xiangqi.js/llms.txt Demonstrates a full game simulation, including playing random moves until the game ends, detecting the game's outcome (checkmate, stalemate, draw), and generating the final PGN. ```APIDOC ## Complete Game Example ### Description A full example demonstrating game play, state detection, and PGN generation for a complete random game. ### Method This example utilizes multiple methods including `Xiangqi()`, `header()`, `moves()`, `move()`, `game_over()`, `in_checkmate()`, `in_stalemate()`, `in_draw()`, `fen()`, `ascii()`, and `pgn()`. ### Parameters None (demonstrates library usage) ### Request Example ```javascript const Xiangqi = require('./xiangqi').Xiangqi; // Play a random game const xiangqi = new Xiangqi(); xiangqi.header('Red', 'Random Bot', 'Black', 'Random Bot', 'Event', 'Demo Game'); let moveCount = 0; while (!xiangqi.game_over() && moveCount < 200) { const moves = xiangqi.moves(); const randomMove = moves[Math.floor(Math.random() * moves.length)]; xiangqi.move(randomMove); moveCount++; } // Check how game ended if (xiangqi.in_checkmate()) { const winner = xiangqi.turn() === 'r' ? 'Black' : 'Red'; console.log(`Checkmate! ${winner} wins.`); xiangqi.header('Result', xiangqi.turn() === 'r' ? '0-1' : '1-0'); } else if (xiangqi.in_stalemate()) { console.log('Stalemate!'); xiangqi.header('Result', '1/2-1/2'); } else if (xiangqi.in_draw()) { console.log('Draw by insufficient material or repetition.'); xiangqi.header('Result', '1/2-1/2'); } // Output the game console.log(`\nGame ended after ${moveCount} moves.`); console.log('\nFinal position:'); console.log(xiangqi.ascii()); console.log('\nPGN:'); console.log(xiangqi.pgn({ max_width: 60 })); ``` ### Response #### Success Response (200) - The console output will show the game's outcome, the number of moves played, the final board state in ASCII, and the complete game in PGN format. #### Response Example ```text Game ended after 45 moves. Final position: +---------------------------+ 9 | . . . . . . . . . | 8 | . . . . . . . . . | 7 | . . . . . . . . . | 6 | . . . . . . . . . | 5 | . . . . . . . . . | 4 | . . . . . . . . . | 3 | . . . . . . . . . | 2 | . . . . . . . . . | 1 | . . . . . . . . . | 0 | . . . . . . . . . | +---------------------------+ a b c d e f g h i PGN: [Red "Random Bot"] [Black "Random Bot"] [Event "Demo Game"] [Result "1/2-1/2"] 1. a0b0 a9b7 2. h0g2 h9g7 3. b0c2 c9c7 4. g3g4 g9g7 5. c2b2 b7b5 6. i0h2 i9h7 7. b2a2 a7a5 8. h2g2 g7g5 9. a2a4 a5a4 10. g2g4 a4a3 11. f3f4 f9f7 12. g4g5 f7f5 13. f4f5 e9e7 14. g5g6 f5f4 15. f5f6 e7e5 16. g6g7 f4f3 17. f6f7 e5e4 18. g7g8=Q f3f2 19. f7f8=R e4e3 20. g8=Q f2f1=B 21. f8=R f1=N e3e2 22. q8=B f1=P e2e1=C 23. r8=N e1=A f1=A 24. n8=B a0a1 25. b0b1 a1a0 26. b1b0 a0a1 27. b0b1 a1a0 28. b1b0 a0a1 29. b0b1 a1a0 30. b1b0 a0a1 31. b0b1 a1a0 32. b1b0 a0a1 33. b0b1 a1a0 34. b1b0 a0a1 35. b0b1 a1a0 36. b1b0 a0a1 37. b0b1 a1a0 38. b1b0 a0a1 39. b0b1 a1a0 40. b1b0 a0a1 41. b0b1 a1a0 42. b1b0 a0a1 43. b0b1 a1a0 44. b1b0 a0a1 45. b0b1 a1a0 ``` ``` -------------------------------- ### Get FEN String Source: https://github.com/lengyanyu258/xiangqi.js/blob/dev/README.md Retrieves the current board configuration as a FEN string. ```javascript const xiangqi = new Xiangqi(); // make some moves xiangqi.move('e3e4'); xiangqi.move('e6e5'); xiangqi.move('g3g4'); xiangqi.fen(); ``` -------------------------------- ### Implement a Random Game Loop in Xiangqi.js Source: https://context7.com/lengyanyu258/xiangqi.js/llms.txt A complete example showing how to play a random game, detect game-over conditions like checkmate or stalemate, and export the final PGN. ```javascript const Xiangqi = require('./xiangqi').Xiangqi; const xiangqi = new Xiangqi(); xiangqi.header('Red', 'Random Bot', 'Black', 'Random Bot', 'Event', 'Demo Game'); let moveCount = 0; while (!xiangqi.game_over() && moveCount < 200) { const moves = xiangqi.moves(); const randomMove = moves[Math.floor(Math.random() * moves.length)]; xiangqi.move(randomMove); moveCount++; } if (xiangqi.in_checkmate()) { const winner = xiangqi.turn() === 'r' ? 'Black' : 'Red'; console.log(`Checkmate! ${winner} wins.`); xiangqi.header('Result', xiangqi.turn() === 'r' ? '0-1' : '1-0'); } else if (xiangqi.in_stalemate()) { console.log('Stalemate!'); xiangqi.header('Result', '1/2-1/2'); } else if (xiangqi.in_draw()) { console.log('Draw by insufficient material or repetition.'); xiangqi.header('Result', '1/2-1/2'); } console.log(`\nGame ended after ${moveCount} moves.`); console.log('\nFinal position:'); console.log(xiangqi.ascii()); console.log('\nPGN:'); console.log(xiangqi.pgn({ max_width: 60 })); ``` -------------------------------- ### Board Manipulation: put, get, remove in Xiangqi.js Source: https://context7.com/lengyanyu258/xiangqi.js/llms.txt Provides methods for direct manipulation of pieces on the Xiangqi board. `.put()` places a piece, `.get()` retrieves information about a piece at a specific square, and `.remove()` takes a piece off the board. These are useful for custom board setups or debugging. ```javascript const xiangqi = new Xiangqi(); xiangqi.clear(); // Clear the board first // Put pieces on the board xiangqi.put({ type: xiangqi.PAWN, color: xiangqi.BLACK }, 'a5'); xiangqi.put({ type: xiangqi.KING, color: xiangqi.RED }, 'e1'); xiangqi.put({ type: xiangqi.KING, color: xiangqi.BLACK }, 'd8'); console.log(xiangqi.fen()); // -> '3k5/9/9/9/p8/9/9/9/4K4/9 r - - 0 1' // Get piece at a square const piece = xiangqi.get('a5'); console.log(piece); // -> { type: 'p', color: 'b' } const empty = xiangqi.get('a6'); console.log(empty); // -> null // Remove piece from square const removed = xiangqi.remove('a5'); console.log(removed); // -> { type: 'p', color: 'b' } // Invalid piece placement returns false const invalidPut = xiangqi.put({ type: 'z', color: 'r' }, 'a1'); console.log(invalidPut); // -> false ``` -------------------------------- ### Manage Game State in Xiangqi.js Source: https://context7.com/lengyanyu258/xiangqi.js/llms.txt Utility methods for checking the current turn, resetting the game to the starting position, or clearing the board entirely. ```javascript const xiangqi = new Xiangqi(); console.log(xiangqi.turn()); xiangqi.move('e3e4'); console.log(xiangqi.turn()); xiangqi.reset(); console.log(xiangqi.fen()); xiangqi.clear(); console.log(xiangqi.fen()); ``` -------------------------------- ### Game State Management: .turn(), .reset(), and .clear() Source: https://context7.com/lengyanyu258/xiangqi.js/llms.txt Utility methods for managing the game's state. `.turn()` returns the current player's turn, `.reset()` restores the board to its initial setup, and `.clear()` empties the entire board. ```APIDOC ## .turn(), .reset(), and .clear() ### Description Utility methods for game state management. `.turn()` returns the current side to move, `.reset()` returns to starting position, `.clear()` empties the board. ### Method - `.turn()`: Returns the current player's turn ('r' for red, 'b' for black). - `.reset()`: Resets the game to the initial board setup. - `.clear()`: Clears all pieces from the board, leaving it empty. ### Parameters None ### Request Example ```javascript const xiangqi = new Xiangqi(); // Check whose turn it is console.log(xiangqi.turn()); // -> 'r' (red to move) xiangqi.move('e3e4'); console.log(xiangqi.turn()); // -> 'b' (black to move) // Reset to starting position xiangqi.reset(); console.log(xiangqi.fen()); // -> 'rnbakabnr/9/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/9/RNBAKABNR r - - 0 1' // Clear the board entirely xiangqi.clear(); console.log(xiangqi.fen()); // -> '9/9/9/9/9/9/9/9/9/9 r - - 0 1' ``` ### Response #### Success Response (200) - **`.turn()`**: Returns a string ('r' or 'b'). - **`.reset()`**: Returns `undefined`. - **`.clear()`**: Returns `undefined`. #### Response Example ```json { "current_turn": "r", "fen_after_reset": "rnbakabnr/9/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/9/RNBAKABNR r - - 0 1", "fen_after_clear": "9/9/9/9/9/9/9/9/9/9 r - - 0 1" } ``` ``` -------------------------------- ### GET .pgn() Source: https://github.com/lengyanyu258/xiangqi.js/blob/dev/README.md Exports the current game state into Portable Game Notation (PGN) format. ```APIDOC ## GET .pgn() ### Description Returns the game history in PGN format. ### Method GET ### Parameters #### Query Parameters - **max_width** (number) - Optional - Maximum width of the PGN output. - **newline_char** (string) - Optional - Character used for line breaks. ### Response #### Success Response (200) - **pgn** (string) - The PGN representation of the game. ``` -------------------------------- ### GET .moves() Source: https://github.com/lengyanyu258/xiangqi.js/blob/dev/README.md Retrieves a list of all legal moves from the current board position. ```APIDOC ## GET .moves() ### Description Returns an array of legal moves. Can be filtered by square or returned in verbose mode for detailed move information. ### Method GET ### Parameters #### Query Parameters - **square** (string) - Optional - Filter moves starting from this square. - **verbose** (boolean) - Optional - If true, returns detailed move objects. - **opponent** (boolean) - Optional - If true, generates moves for the opponent. ### Response #### Success Response (200) - **moves** (array) - List of legal moves (strings or objects depending on verbose flag). ``` -------------------------------- ### Generate Legal Moves in Xiangqi Source: https://context7.com/lengyanyu258/xiangqi.js/llms.txt Details how to retrieve all legal moves from the current board position using the .moves() method. Options allow filtering moves for a specific square, getting verbose move objects with full details, or retrieving the opponent's available moves. ```javascript const xiangqi = new Xiangqi(); // Get all legal moves (ICCS notation) const allMoves = xiangqi.moves(); console.log(allMoves.slice(0, 10)); // -> ['a3a4', 'c3c4', 'e3e4', 'g3g4', 'i3i4', 'b2b3', 'b2b4', 'b2b5', 'b2b6', 'b2b9'] // Get moves for a specific square const knightMoves = xiangqi.moves({ square: 'b0' }); console.log(knightMoves); // -> ['b0a2', 'b0c2'] // Get verbose move objects with full details const verboseMoves = xiangqi.moves({ square: 'b2', verbose: true }); console.log(verboseMoves[0]); // -> { color: 'r', from: 'b2', to: 'b3', flags: 'n', piece: 'c', iccs: 'b2b3' } // Get opponent's available moves const opponentMoves = xiangqi.moves({ opponent: true }); console.log(opponentMoves.length); // -> 44 (black's available moves) ``` -------------------------------- ### Board Manipulation Source: https://context7.com/lengyanyu258/xiangqi.js/llms.txt Methods for directly manipulating pieces on the board: placing, getting, and removing pieces. ```APIDOC ## .put(piece, square), .get(square), and .remove(square) ### Description Methods for directly manipulating pieces on the board. `.put()` places a piece, `.get()` retrieves piece info, and `.remove()` removes a piece. ### Methods - **put(piece, square)**: Places a piece on the board. - **get(square)**: Retrieves the piece at a given square. - **remove(square)**: Removes the piece from a given square. ### Parameters #### Path Parameters - **piece** (object) - Required (for `.put()`): An object with `type` and `color` properties (e.g., `{ type: xiangqi.PAWN, color: xiangqi.BLACK }`). - **square** (string) - Required: The algebraic notation for the square (e.g., 'a5', 'e1'). ### Request Example ```javascript const xiangqi = new Xiangqi(); xiangqi.clear(); // Put pieces on the board xiangqi.put({ type: xiangqi.PAWN, color: xiangqi.BLACK }, 'a5'); xiangqi.put({ type: xiangqi.KING, color: xiangqi.RED }, 'e1'); // Get piece at a square const piece = xiangqi.get('a5'); console.log(piece); // Remove piece from square const removed = xiangqi.remove('a5'); console.log(removed); // Invalid piece placement const invalidPut = xiangqi.put({ type: 'z', color: 'r' }, 'a1'); console.log(invalidPut); ``` ### Response #### Success Response - **put()**: Returns `true` if successful, `false` otherwise. - **get(square)**: Returns an object `{ type: 'p', color: 'b' }` if a piece is present, `null` otherwise. - **remove(square)**: Returns the removed piece object if successful, `null` otherwise. #### Response Example ```json // For get('a5') { "type": "p", "color": "b" } // For remove('a5') { "type": "p", "color": "b" } // For put() success true // For put() failure false ``` ``` -------------------------------- ### Get Current Turn in xiangqi.js Source: https://github.com/lengyanyu258/xiangqi.js/blob/dev/README.md Retrieves the current side to move in the game. Returns a string representing the color of the player whose turn it is. ```javascript xiangqi.load('rnbakabnr/9/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/9/RNBAKABNR r - - 0 1'); xiangqi.turn(); // -> 'r' ``` -------------------------------- ### Initialize Board with FEN Source: https://github.com/lengyanyu258/xiangqi.js/blob/dev/README.md Shows how to create a new game instance using default settings or a custom FEN string. ```javascript // board defaults to the starting position when called with no parameters const xiangqi = new Xiangqi(); // pass in a FEN string to load a particular position const xiangqi = new Xiangqi('5kC2/4a1N2/3a5/9/9/9/9/r3C4/4p4/2rK4R r - - 0 1'); ``` -------------------------------- ### Get Piece on Square Source: https://github.com/lengyanyu258/xiangqi.js/blob/dev/README.md Retrieves the piece object located at a specific coordinate. ```javascript xiangqi.clear(); xiangqi.put({ type: xiangqi.PAWN, color: xiangqi.BLACK }, 'a5'); xiangqi.get('a5'); xiangqi.get('a6'); ``` -------------------------------- ### Manage PGN Data in Xiangqi.js Source: https://context7.com/lengyanyu258/xiangqi.js/llms.txt Demonstrates how to generate PGN strings from game history and how to load a complete game state from a PGN string. ```javascript const xiangqi = new Xiangqi(); xiangqi.header('Red', 'Player1', 'Black', 'Player2', 'Date', '2024.01.15'); xiangqi.move('h2e2'); xiangqi.move('h9g7'); xiangqi.move('h0g2'); xiangqi.move('i9h9'); console.log(xiangqi.pgn()); console.log(xiangqi.pgn({ max_width: 20, newline_char: '
' })); const pgn = `[Game "Chinese Chess"] [Event "1982年全国赛"] [Date "1982.12.11"] [Red "柳大华"] [Black "杨官璘"] [Result "1/2-1/2"] [Format "ICCS"] 1. b2e2 b9c7 2. b0c2 a9b9 3. a0b0 h9g7 4. g3g4 c6c5 5. b0b6 b7a7 6. b6c6 a7a8`; const newGame = new Xiangqi(); const success = newGame.load_pgn(pgn); console.log(success); ``` -------------------------------- ### Simulate a Random Game Source: https://github.com/lengyanyu258/xiangqi.js/blob/dev/README.md Demonstrates how to initialize a game and execute random moves until the game ends. ```javascript const Xiangqi = require('./xiangqi').Xiangqi; const xiangqi = new Xiangqi(); while (!xiangqi.game_over()) { const moves = xiangqi.moves(); const move = moves[Math.floor(Math.random() * moves.length)]; xiangqi.move(move); } console.log(xiangqi.pgn()); ``` -------------------------------- ### Board Visualization: .board() and .ascii() Source: https://context7.com/lengyanyu258/xiangqi.js/llms.txt Methods for visualizing the current state of the Xiangqi board. `.board()` returns a 2D array representation, and `.ascii()` provides a human-readable ASCII diagram of the board. ```APIDOC ## .board() and .ascii() ### Description Methods for visualizing the board state. `.board()` returns a 2D array representation, `.ascii()` returns a human-readable ASCII diagram. ### Method - `.board()`: Returns the board as a 2D array. - `.ascii()`: Returns an ASCII string representation of the board. ### Parameters None ### Request Example ```javascript const xiangqi = new Xiangqi(); xiangqi.move('e3e4'); xiangqi.move('e6e5'); xiangqi.move('g3g4'); // ASCII representation console.log(xiangqi.ascii()); /* +---------------------------+ 9 | r n b a k a b n r | 8 | . . . . . . . . . | 7 | . c . . . . . c . | 6 | p . p . . . p . p | 5 | . . . . p . . . . | 4 | . . . . P . P . . | 3 | P . P . . . . . P | 2 | . C . . . . . C . | 1 | . . . . . . . . . | 0 | R N B A K A B N R | +---------------------------+ a b c d e f g h i */ // 2D array representation const board = xiangqi.board(); console.log(board[0][4]); // Black king at e9 // -> { type: 'k', color: 'b' } console.log(board[4][4]); // Empty square // -> null console.log(board[9][0]); // Red rook at a0 // -> { type: 'r', color: 'r' } ``` ### Response #### Success Response (200) - **`.board()`**: Returns a 2D array representing the board state. Each cell can be `null` or an object `{ type: 'piece_type', color: 'piece_color' }`. - **`.ascii()`**: Returns a string containing the ASCII representation of the board. #### Response Example ```json { "ascii_board": " +---------------------------+\n 9 | r n b a k a b n r |\n 8 | . . . . . . . . . |\n 7 | . c . . . . . c . |\n 6 | p . p . . . p . p |\n 5 | . . . . p . . . . |\n 4 | . . . . P . P . . |\n 3 | P . P . . . . . P |\n 2 | . C . . . . . C . |\n 1 | . . . . . . . . . |\n 0 | R N B A K A B N R |\n +---------------------------+\n a b c d e f g h i", "board_array": [ [{"type":"r","color":"b"},{"type":"n","color":"b"},{"type":"b","color":"b"},{"type":"a","color":"b"},{"type":"k","color":"b"},{"type":"a","color":"b"},{"type":"b","color":"b"},{"type":"n","color":"b"},{"type":"r","color":"b"}], [null,null,null,null,null,null,null,null,null], [null,{"type":"c","color":"b"},null,null,null,null,null,{"type":"c","color":"b"},null], [{"type":"p","color":"b"},null,{"type":"p","color":"b"},null,null,null,{"type":"p","color":"b"},null,{"type":"p","color":"b"}], [null,null,null,null,{"type":"p","color":"b"},null,null,null,null], [null,null,null,null,{"type":"P","color":"r"},null,{"type":"P","color":"r"},null,null], [{"type":"P","color":"r"},null,{"type":"P","color":"r"},null,null,null,null,null,{"type":"P","color":"r"}], [null,{"type":"C","color":"r"},null,null,null,null,null,{"type":"C","color":"r"},null], [null,null,null,null,null,null,null,null,null], [{"type":"R","color":"r"},{"type":"N","color":"r"},{"type":"B","color":"r"},{"type":"A","color":"r"},{"type":"K","color":"r"},{"type":"A","color":"r"},{"type":"B","color":"r"},{"type":"N","color":"r"},{"type":"R","color":"r"}] ] } ``` ``` -------------------------------- ### Visualize Board State in Xiangqi.js Source: https://context7.com/lengyanyu258/xiangqi.js/llms.txt Shows how to retrieve a human-readable ASCII representation of the board or a 2D array for programmatic access to piece positions. ```javascript const xiangqi = new Xiangqi(); xiangqi.move('e3e4'); xiangqi.move('e6e5'); xiangqi.move('g3g4'); console.log(xiangqi.ascii()); const board = xiangqi.board(); console.log(board[0][4]); console.log(board[4][4]); console.log(board[9][0]); ``` -------------------------------- ### PGN Handling: .pgn() and .load_pgn() Source: https://context7.com/lengyanyu258/xiangqi.js/llms.txt Methods for generating and parsing Portable Game Notation (PGN) strings. .pgn() creates a PGN representation of the current game, while .load_pgn() parses a PGN string to load a game. ```APIDOC ## .pgn(options) and .load_pgn(pgn, options) ### Description Methods for working with Portable Game Notation (PGN). `.pgn()` generates a PGN string and `.load_pgn()` parses and loads a PGN game. ### Method `.pgn(options)`: Generates PGN string. `.load_pgn(pgn, options)`: Parses and loads PGN game. ### Parameters #### `.pgn(options)` - **options** (object) - Optional - Configuration for PGN generation. - **max_width** (number) - Optional - Maximum width for the PGN string. - **newline_char** (string) - Optional - Character to use for newlines. #### `.load_pgn(pgn, options)` - **pgn** (string) - Required - The PGN string to load. - **options** (object) - Optional - Configuration for PGN parsing. ### Request Example ```javascript const xiangqi = new Xiangqi(); // Set game headers xiangqi.header('Red', 'Player1', 'Black', 'Player2', 'Date', '2024.01.15'); // Make moves xiangqi.move('h2e2'); xiangqi.move('h9g7'); xiangqi.move('h0g2'); xiangqi.move('i9h9'); // Generate PGN console.log(xiangqi.pgn()); // -> '[Red "Player1"]\n[Black "Player2"]\n[Date "2024.01.15"]\n\n1. h2e2 h9g7 2. h0g2 i9h9' // Generate PGN with custom formatting console.log(xiangqi.pgn({ max_width: 20, newline_char: '
' })); // Load a complete game from PGN const pgn = `[Game "Chinese Chess"]\n[Event "1982年全国赛"]\n[Date "1982.12.11"]\n[Red "柳大华"]\n[Black "杨官璘"]\n[Result "1/2-1/2"]\n[Format "ICCS"]\n\n1. b2e2 b9c7 2. b0c2 a9b9 3. a0b0 h9g7\n4. g3g4 c6c5 5. b0b6 b7a7 6. b6c6 a7a8`; const newGame = new Xiangqi(); const success = newGame.load_pgn(pgn); console.log(success); // -> true console.log(newGame.history().length); // -> 12 ``` ### Response #### Success Response (200) - **pgn()**: Returns a string representing the game in PGN format. - **load_pgn()**: Returns a boolean indicating success or failure. #### Response Example ```json { "pgn_output": "[Red \"Player1\"]\n[Black \"Player2\"]\n[Date \"2024.01.15\"]\n\n1. h2e2 h9g7 2. h0g2 i9h9", "load_success": true } ``` ``` -------------------------------- ### Load PGN data into Xiangqi instance Source: https://github.com/lengyanyu258/xiangqi.js/blob/dev/README.md Demonstrates how to load a standard PGN string into a Xiangqi game object. The method returns a boolean indicating success and updates the internal game state. ```javascript const xiangqi = new Xiangqi(); const pgn = ['[Game "Chinese Chess"]', '[Result "1/2-1/2"]', '', '1. b2e2 b9c7 2. b0c2 a9b9'].join('\n'); const success = xiangqi.load_pgn(pgn); if (success) { console.log(xiangqi.fen()); } ``` -------------------------------- ### Manage Xiangqi Board State with FEN Source: https://context7.com/lengyanyu258/xiangqi.js/llms.txt Covers the .fen() and .load() methods for managing the Xiangqi board's state. .fen() returns the current position as a FEN string, while .load(fen) sets the board to a specified FEN position. Both methods handle valid and invalid FEN strings. ```javascript const xiangqi = new Xiangqi(); // Get current FEN console.log(xiangqi.fen()); // -> 'rnbakabnr/9/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/9/RNBAKABNR r - - 0 1' // Make some moves and check FEN xiangqi.move('e3e4'); xiangqi.move('e6e5'); xiangqi.move('g3g4'); console.log(xiangqi.fen()); // -> 'rnbakabnr/9/1c5c1/p1p3p1p/4p4/4P1P2/P1P5P/1C5C1/9/RNBAKABNR b - - 3 2' // Load a specific position const success = xiangqi.load('1nbakabn1/9/1c5c1/p1p3p1p/4p4/4P4/P1P3P1P/1C5C1/9/1NBAKABN1 b - - 1 2'); console.log(success); // -> true // Invalid FEN returns false const fail = xiangqi.load('invalid-fen-string'); console.log(fail); // -> false ``` -------------------------------- ### Load PGN with custom options Source: https://github.com/lengyanyu258/xiangqi.js/blob/dev/README.md Shows how to use the options object to handle non-standard PGN formats. The 'sloppy' flag enables parsing of non-standard notation, and 'newline_char' allows specifying custom line separators. ```javascript const sloppy_pgn = ['[Result "1-0"]', '', '1. Pc2c4 Pe7e5', '2. Nc3 Nf6'].join('|'); const options = { newline_char: '\\|', sloppy: true }; const xiangqi = new Xiangqi(); const success = xiangqi.load_pgn(sloppy_pgn, options); ``` -------------------------------- ### Make Moves on the Xiangqi Board Source: https://context7.com/lengyanyu258/xiangqi.js/llms.txt Explains how to make moves in the Xiangqi game using either ICCS string notation (e.g., 'e3e4') or a move object. The .move() method returns a move object if the move is legal, otherwise null. It also supports a sloppy mode for non-standard notations. ```javascript const xiangqi = new Xiangqi(); // Move using ICCS notation const move1 = xiangqi.move('e3e4'); console.log(move1); // -> { color: 'r', from: 'e3', to: 'e4', flags: 'n', piece: 'p', iccs: 'e3e4' } // Move using object notation const move2 = xiangqi.move({ from: 'e6', to: 'e5' }); console.log(move2); // -> { color: 'b', from: 'e6', to: 'e5', flags: 'n', piece: 'p', iccs: 'e6e5' } // Invalid move returns null const invalidMove = xiangqi.move('a0a9'); console.log(invalidMove); // -> null // Sloppy parser for non-standard notations xiangqi.reset(); const sloppyMove = xiangqi.move('E3-E4', { sloppy: true }); console.log(sloppyMove); // -> { color: 'r', from: 'e3', to: 'e4', flags: 'n', piece: 'p', iccs: 'e3e4' } ``` -------------------------------- ### Export Game to PGN Format in Xiangqi.js Source: https://github.com/lengyanyu258/xiangqi.js/blob/dev/README.md Returns the current game in Portable Game Notation (PGN) format. This function accepts an optional options object to control the maximum line width and the newline character used in the output string. ```javascript const xiangqi = new Xiangqi(); xiangqi.header('Red', '吕钦', 'Black', '许银川'); xiangqi.move('h2e2'); xiangqi.move('h9g7'); xiangqi.move('h0g2'); xiangqi.move('i9h9'); xiangqi.pgn({ max_width: 5, newline_char: '
' }); // -> '[Red "吕钦"]
[Black "许银川"]

1. h2e2 h9g7
2. h0g2 i9h9' ``` -------------------------------- ### Manage PGN Header Information Source: https://github.com/lengyanyu258/xiangqi.js/blob/dev/README.md Sets or retrieves metadata (headers) for the PGN output. Can accept multiple key-value pairs or return the full header object. ```javascript xiangqi.header('Red', '吕钦'); xiangqi.header('Black', '许银川'); xiangqi.header('Red', '许银川', 'Black', '聂卫平', 'Date', '1999.12.09'); const headers = xiangqi.header(); ``` -------------------------------- ### Make a Move in Xiangqi.js Source: https://github.com/lengyanyu258/xiangqi.js/blob/dev/README.md Attempts to make a move on the board. It accepts moves in ICCS format (string) or as a move object. An optional 'sloppy' flag can be used for non-standard notations. Returns a move object if the move is legal, otherwise null. ```javascript const xiangqi = new Xiangqi(); xiangqi.move('e3e4'); // -> { color: 'r', from: 'e3', to: 'e4', flags: 'n', piece: 'p', iccs: 'e3e4' } xiangqi.move('E6E5'); // ICCS is case sensitive!! // -> null xiangqi.move('E6-E5'); // -> { color: 'b', from: 'g8', to: 'f6', flags: 'n', piece: 'n', san: 'Nf6' } xiangqi.move({ from: 'g3', to: 'g4' }); // -> { color: 'r', from: 'g3', to: 'g4', flags: 'n', piece: 'p', iccs: 'g3g4' } xiangqi.move('e2e4', {sloppy: true}); // -> { color: 'w', from: 'e2', to: 'e4', flags: 'b', piece: 'p', san: 'e4' } xiangqi.move('e7-e5', {sloppy: true}); // -> { color: 'b', from: 'e7', to: 'e5', flags: 'b', piece: 'p', san: 'e5' } xiangqi.move('Pf2f4', {sloppy: true}); // -> { color: 'w', from: 'f2', to: 'f4', flags: 'b', piece: 'p', san: 'f4' } xiangqi.move('Pe5xf4', {sloppy: true}); // -> { color: 'b', from: 'e5', to: 'f4', flags: 'c', piece: 'p', captured: 'p', san: 'exf4' } var xiangqi = new Xiangqi('r2qkbnr/ppp2ppp/2n5/1B2pQ2/4P3/8/PPP2PPP/RNB1K2R b KQkq - 3 7'); xiangqi.move('Nge7', {sloppy: true}); // -> { color: 'b', from: 'g8', to: 'e7', flags: 'n', piece: 'n', san: 'Ne7' } ``` -------------------------------- ### POST .move() Source: https://github.com/lengyanyu258/xiangqi.js/blob/dev/README.md Attempts to make a move on the board. Returns a move object if legal, otherwise null. ```APIDOC ## POST .move() ### Description Attempts to make a move on the board. Can accept a string (ICCS notation) or a move object. An optional 'sloppy' flag allows for non-standard notation parsing. ### Method POST ### Parameters #### Request Body - **move** (string|object) - Required - The move to execute (e.g., 'e3e4' or {from: 'g3', to: 'g4'}). - **options** (object) - Optional - Configuration object (e.g., {sloppy: true}). ### Response #### Success Response (200) - **moveObject** (object) - Returns the move details if successful, including color, from, to, flags, piece, and iccs. #### Error Response - **null** - Returned if the move is illegal. ``` -------------------------------- ### Move History Navigation: undo and redo in Xiangqi.js Source: https://context7.com/lengyanyu258/xiangqi.js/llms.txt Enables navigation through the game's move history. The `.undo()` method reverts the last move, returning details of the undone move, while `.redo()` replays a previously undone move. These are essential for analyzing games or implementing undo/redo features. ```javascript const xiangqi = new Xiangqi(); // Make some moves xiangqi.move('e3e4'); xiangqi.move('e6e5'); console.log(xiangqi.fen()); // -> 'rnbakabnr/9/1c5c1/p1p3p1p/4p4/4P4/P1P3P1P/1C5C1/9/RNBAKABNR r - - 2 2' // Undo last move const undone = xiangqi.undo(); console.log(undone); // -> { color: 'b', from: 'e6', to: 'e5', flags: 'n', piece: 'p', iccs: 'e6e5' } console.log(xiangqi.fen()); // -> 'rnbakabnr/9/1c5c1/p1p1p1p1p/9/4P4/P1P3P1P/1C5C1/9/RNBAKABNR b - - 1 1' // Redo the undone move const redone = xiangqi.redo(); console.log(redone); // -> { color: 'b', from: 'e6', to: 'e5', flags: 'n', piece: 'p', iccs: 'e6e5' } console.log(xiangqi.fen()); // -> 'rnbakabnr/9/1c5c1/p1p3p1p/4p4/4P4/P1P3P1P/1C5C1/9/RNBAKABNR r - - 2 2' // Undo with nothing to undo returns null xiangqi.reset(); console.log(xiangqi.undo()); // -> null ```